Skip to main content
Solved

How to ZIP folders in any path after successful translation?


lazarlubomir
Contributor
Forum|alt.badge.img+7

Hello everyone,

I´m still thinking about issue when after successful translation all folders in some path will be zipped. I have no idea, how to do this via simple workspace. Maybe only one possibility is to do that via Shutdown Python script. Unfortunately I´m not a huge expert in Python, could anybody help me please?

Thank You so much!

Lubo

Best answer by david_r

To delete a directory tree recursively, you can use the following in a PythonCaller:

import shutil
import fmeobjects

def delete_directories(feature):
    directory_to_delete = feature.getAttribute('directory_to_delete')
    if directory_to_delete:
        shutil.rmtree(directory_to_delete)

You will have to provide a single feature with an attribute "directory_to_delete" containing the root of the folder structure to delete.

Example if you send a feature with the following attribute:

directory_to_delete = 'c:\output\temp123'

This will permanently delete ALL the files and subdirectories from c:\output\temp123 and down. Notice that the deleted files will NOT go to the recycle bin, they will be deleted for good. See also the documentation for rmtree.

NOTE: I assume no responsability whatsoever for the use or abuse of the above code! Be careful, you're on your own if something important gets deleted!

View original
Did this help you find an answer to your question?

4 replies

david_r
Celebrity
  • December 8, 2017

I think the easiest solution is probably to replace your writers with FeatureWriters. Connect all the summary ports to a FeatureHolder + Sampler (1st feature only) and then use the ZipArchiver from the FME Hub.

If you're not too comfortable programming in Python, this will make life much easier for you, especially in the long run (maintenance).


lazarlubomir
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • December 8, 2017
david_r wrote:

I think the easiest solution is probably to replace your writers with FeatureWriters. Connect all the summary ports to a FeatureHolder + Sampler (1st feature only) and then use the ZipArchiver from the FME Hub.

If you're not too comfortable programming in Python, this will make life much easier for you, especially in the long run (maintenance).

Great tip! But what if You need to delete all directories which were zipped after zipping process? Is there any way how to remove them please?

 

 


david_r
Celebrity
  • Best Answer
  • December 8, 2017

To delete a directory tree recursively, you can use the following in a PythonCaller:

import shutil
import fmeobjects

def delete_directories(feature):
    directory_to_delete = feature.getAttribute('directory_to_delete')
    if directory_to_delete:
        shutil.rmtree(directory_to_delete)

You will have to provide a single feature with an attribute "directory_to_delete" containing the root of the folder structure to delete.

Example if you send a feature with the following attribute:

directory_to_delete = 'c:\output\temp123'

This will permanently delete ALL the files and subdirectories from c:\output\temp123 and down. Notice that the deleted files will NOT go to the recycle bin, they will be deleted for good. See also the documentation for rmtree.

NOTE: I assume no responsability whatsoever for the use or abuse of the above code! Be careful, you're on your own if something important gets deleted!


lazarlubomir
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • December 8, 2017
david_r wrote:

To delete a directory tree recursively, you can use the following in a PythonCaller:

import shutil
import fmeobjects

def delete_directories(feature):
    directory_to_delete = feature.getAttribute('directory_to_delete')
    if directory_to_delete:
        shutil.rmtree(directory_to_delete)

You will have to provide a single feature with an attribute "directory_to_delete" containing the root of the folder structure to delete.

Example if you send a feature with the following attribute:

directory_to_delete = 'c:\output\temp123'

This will permanently delete ALL the files and subdirectories from c:\output\temp123 and down. Notice that the deleted files will NOT go to the recycle bin, they will be deleted for good. See also the documentation for rmtree.

NOTE: I assume no responsability whatsoever for the use or abuse of the above code! Be careful, you're on your own if something important gets deleted!

Brillant! I have tested your code and it is completely what I have search!

 

 

Thank you @david_r once again!

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings