Solved

File Copy - DELETE


Badge +21

Is it possible to DELETE files using the WRITER "File Copy" ?

 

 

UPDATE:

I have created a custom transformer that uses the solution (FeatureWriter in MOVE to null) and uploaded to https://hub.safe.com/publishers/sigtill/transformers/filedeleter

Feel free to send any feedback related to this.

icon

Best answer by sigtill 15 October 2019, 08:28

View original

15 replies

Userlevel 2
Badge +17
Hi Sig,

 

 

I don't think it's possible, but it's easy to delete a file with a script. Assuming that an attribute called "filepath" stores the file path that should be deleted,

 

-----

 

# Example for PythonCaller

 

import os

 

def removeFile(feature):

 

    file = feature.getAttribute('filepath')

 

    if os.path.exists(file):

 

        os.remove(file)

 

-----

 

# Example for TclCaller

 

proc delFile {} {

 

    file delete [FME_GetAttribute "filepath"]

 

}

 

-----

 

 

Takashi
Badge
Hi Sig,

 

 

I don't think it's possible, but it's easy to delete a file with a script. Assuming that an attribute called "filepath" stores the file path that should be deleted,

 

-----

 

# Example for PythonCaller

 

import os

 

def removeFile(feature):

 

    file = feature.getAttribute('filepath')

 

    if os.path.exists(file):

 

        os.remove(file)

 

-----

 

# Example for TclCaller

 

proc delFile {} {

 

    file delete [FME_GetAttribute "filepath"]

 

}

 

-----

 

 

Takashi
@takashi How would you write this script to delete entire folders instead of files?

 

 

Badge +16

@kam you could just use a system caller and an old fashion dos command for that

Userlevel 2
Badge +17

Hi @kam, the "os.rmdir(path)" method removes an empty folder; the "os.removedirs(path)" method removes a folder and its subfolders recursively if they all are empty; the "shutil.rmtree(path)" method removes a folder and all files and subfolders belonging to the specified folder. See the Python documentation to learn more.

Userlevel 4
Badge +25

You could use FileCopy and move the file to the null folder. \\dev\\null on Linux I think. There is a Windows equivalent, but I can't recall it off hand.

Badge +21

UPDATE: 2020:

This solution seems to be easier than the previous:

Replace the three variables needed (filecopy_XXX, XXX XXX) and the dest_filename to nul (with one l, not two ll). And make sure the FeatureWriter is set to MOVE

 

 

Badge +21

The most recent (and best solution) is to overwrite the 3 parameters: and set filecopy_dest_filename = nul (with one l, not 2 ll).

 

Badge +21

https://hub.safe.com/publishers/sigtill/transformers/filedeleter

Badge

Is there anyone that have used FileDeleter from @Sigbjørn Herstad​ on FME Server? For me it works fine in Workbench but not on FME Server. The log files gives the same message but the file isn't deleted when running on the server. Both desktop and server runs on Windows OS. The server write to this files hare so there might not be an permission issue.

 

Log file:

2020-09-21 11:42:35| 0.7| 0.0|ERROR |File Copy Writer: Cannot create destination folder '\\'

2020-09-21 11:42:35| 0.7| 0.0|INFORM|File Copy Writer: Received move request from '\\\\fileshare1\\folder\\temp\\781af574-ea39-44f9-95d4-ea7f62e050fd.ffs' to 'c:\\temp\\\\\\nul'

Badge +21

Is there anyone that have used FileDeleter from @Sigbjørn Herstad​ on FME Server? For me it works fine in Workbench but not on FME Server. The log files gives the same message but the file isn't deleted when running on the server. Both desktop and server runs on Windows OS. The server write to this files hare so there might not be an permission issue.

 

Log file:

2020-09-21 11:42:35| 0.7| 0.0|ERROR |File Copy Writer: Cannot create destination folder '\\'

2020-09-21 11:42:35| 0.7| 0.0|INFORM|File Copy Writer: Received move request from '\\\\fileshare1\\folder\\temp\\781af574-ea39-44f9-95d4-ea7f62e050fd.ffs' to 'c:\\temp\\\\\\nul'

Unfortunately it does not work on FME Server. I would suggest to use a FeatureWriter and Move files to the $FME_SHAREDRESOURCE_TEMP folder. Then they will be deleted by FME Server in a given interval.

Badge

Unfortunately it does not work on FME Server. I would suggest to use a FeatureWriter and Move files to the $FME_SHAREDRESOURCE_TEMP folder. Then they will be deleted by FME Server in a given interval.

Thank you for your support!

Does this support using the fme_dataset attribute as the file to delete? Also, is it possible to put it after a FeatureWriter so basically I can delete the file after it has been processed?

 

Thanks.

Badge +5

Hello, Can We use *.* with FileDeleter? If not that would be a suggestion for enhancement.

Badge +7

UPDATE: 2020:

This solution seems to be easier than the previous:

Replace the three variables needed (filecopy_XXX, XXX XXX) and the dest_filename to nul (with one l, not two ll). And make sure the FeatureWriter is set to MOVE

 

 

I have been trying to use this method but it's not working. However I notice that you are setting a value for filecopy_dest_dataset although there is nothing in the Attribute Value. So are you setting the value to an empty string? I've been using a FeatureWriter to try and do this for *.png in a particular folder, and only specifying the values for filecopy_source_dataset and filecopy_dest_filename.

 

Or do I need to change some of these parameters?

File Copy Parameters

Userlevel 1
Badge +10

I have been trying to use this method but it's not working. However I notice that you are setting a value for filecopy_dest_dataset although there is nothing in the Attribute Value. So are you setting the value to an empty string? I've been using a FeatureWriter to try and do this for *.png in a particular folder, and only specifying the values for filecopy_source_dataset and filecopy_dest_filename.

 

Or do I need to change some of these parameters?

File Copy Parameters

I've not tried the nul method, i normally use a TempPathName creator, move the file to that location and then it's automatically deleted when the workspace completes. This way it works on both desktop and server

Reply