Solved

Is there a quick way to rename files?


I have a folder containing hundreds of tif and related extension files e.g. NS26.tif, NS26.aux and NS26.rrd. I want to rename them adding a suffix i.e. NS26_c.tif, NS26_c.aux etc. I can manage this using filecopy to the existing folder but because the filename has changed overwrite won't work so I end up with new and old in same folder. Any suggestions?

icon

Best answer by takashi 22 March 2017, 15:52

View original

14 replies

Userlevel 2
Badge +12

You can use the "Directory and File Path" reader to scan a folder (and subfolders) to retrieve the file names.

Then use the BulkAttributeRenamer to rename the file names and as you mentioned the FileCopy writer to move (or copy) the files to a new location.

Userlevel 2
Badge +17

Hi @chris_fc, try setting MOVE to the File Operation parameter of the File Copy writer.

Badge +2

Using "filecopy" to another folder is best option else using thru command prompt can also can be done

Badge +1

You can also use SystemCaller and type in command line for each tif, aux and rrd file of feature you read.

Badge

I'd break into a piece of python to do this, using pythoncaller - otherwise you're copying the data rather than quite a simple directory registry change?

google renaming file in python

but it's basically one line os.rename, if you're not comfortable in python then create the new name in transformers before you pass it to the pythoncaller.

you might wrap the rename function in a python 'try' stamen to catch any unexpected errrors, the file not being found or locked or something.

Hi @chris_fc, try setting MOVE to the File Operation parameter of the File Copy writer.

Thank you! Changing the File Operation to MOVE did the trick.

 

Chris

 

 

Badge

you can easily do it in python like nrich says.  I do this in my workbenches to help define it being processed

import fme
import fme objects
import os

original_file_name = 'orig_file_name.txt'
new_file_name = "new_file_name.txt"
orig_file_path =" directory path"
new_file_path = 'new_directory_path_if_file_moved"

os.rename(os.path.join(orig_path, original_file_name), os.path.join(orig_path, new_file_name)


You can then add parameters to hold the new_file and orig_file name so its more dynamic.  Also the path you can have as a parameter.  I think python is very easy and quick.  You can always tie the directory path to the file name and get rid of os.path.join() and you would just need the file names for os.rename.  Below is a stackoverflow that is a good example.  

http://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python

Badge

Hello,

A follow up to this question: I am trying to rename X number of folders and I am using the following code:

def changeFolder(feature):

 

origVal=feature.getAttribute('path_windows')

 

fixVal=feature.getAttribute('pathwithoutcomma')

 

os.rename(origVal,fixVal)

 

 

pass

However, there is no change in the folders. The path_windows and pathwithoutcomma are values I have created inside FME.

Any ideas?

Thank you,

George

Userlevel 4

Hello,

A follow up to this question: I am trying to rename X number of folders and I am using the following code:

def changeFolder(feature):

 

origVal=feature.getAttribute('path_windows')

 

fixVal=feature.getAttribute('pathwithoutcomma')

 

os.rename(origVal,fixVal)

 

 

pass

However, there is no change in the folders. The path_windows and pathwithoutcomma are values I have created inside FME.

Any ideas?

Thank you,

George

What's the value of "pathwithoutcomma", is it only a filename or does it also contain a pathname?

 

 

Try using shutil.move() rather than os.rename() and see if that helps.
Badge
What's the value of "pathwithoutcomma", is it only a filename or does it also contain a pathname?

 

 

Try using shutil.move() rather than os.rename() and see if that helps.
Hello David,

 

 

Thank you for your reply. Unfortunately it did not work. I am attaching a screenshot of my code and the inspector to make things more clear.

 

 

Thank you very much for your help.

 

 

Kind regards,

 

 

George

 

 

inspector.jpgcode.jpg

 

 

Userlevel 4
Hello David,

 

 

Thank you for your reply. Unfortunately it did not work. I am attaching a screenshot of my code and the inspector to make things more clear.

 

 

Thank you very much for your help.

 

 

Kind regards,

 

 

George

 

 

inspector.jpgcode.jpg

 

 

The problem seems to be that you have both a function and a class with the same name "changeFolder". Because of the order in which they're defined, it's your class that gets called (which does nothing) and not your function (which renames the file).

 

 

You can delete everything from after the line containing "shutil.move(...)" and down.
Badge
The problem seems to be that you have both a function and a class with the same name "changeFolder". Because of the order in which they're defined, it's your class that gets called (which does nothing) and not your function (which renames the file).

 

 

You can delete everything from after the line containing "shutil.move(...)" and down.
Worked like a charm. Pretty embarrassed for not realizing the basics. Thank you very much for your help!

 

 

Edit: I just realized I downvoted your answer? My impression was I pressed the Like button. This was my intention, your answer was 100% right and solved my problem.

 

 

Userlevel 4
Worked like a charm. Pretty embarrassed for not realizing the basics. Thank you very much for your help!

 

 

Edit: I just realized I downvoted your answer? My impression was I pressed the Like button. This was my intention, your answer was 100% right and solved my problem.

 

 

No worries, glad to help :-)

 

 

Badge

you can easily do it in python like nrich says.  I do this in my workbenches to help define it being processed

import fme
import fme objects
import os

original_file_name = 'orig_file_name.txt'
new_file_name = "new_file_name.txt"
orig_file_path =" directory path"
new_file_path = 'new_directory_path_if_file_moved"

os.rename(os.path.join(orig_path, original_file_name), os.path.join(orig_path, new_file_name)


You can then add parameters to hold the new_file and orig_file name so its more dynamic.  Also the path you can have as a parameter.  I think python is very easy and quick.  You can always tie the directory path to the file name and get rid of os.path.join() and you would just need the file names for os.rename.  Below is a stackoverflow that is a good example.  

http://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python

Working example on what it looks like in a pythonCaller:

 

Sweco_PythonCaller-RenameFiles_20210218 

Reply