Skip to main content
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?

Best answer by takashi

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

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

14 replies

erik_jan
Contributor
Forum|alt.badge.img+17
  • Contributor
  • March 22, 2017

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.


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • March 22, 2017

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


pratap
Contributor
Forum|alt.badge.img+11
  • Contributor
  • March 22, 2017

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


zzupljanin
Contributor
Forum|alt.badge.img+4
  • Contributor
  • March 22, 2017

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


nrich
Contributor
Forum|alt.badge.img+5
  • Contributor
  • March 22, 2017

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.


  • Author
  • March 22, 2017
takashi wrote:

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

 

 


Forum|alt.badge.img
  • March 22, 2017

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


Forum|alt.badge.img

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


david_r
Evangelist
  • October 17, 2018
georgefloros wrote:

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.

Forum|alt.badge.img
david_r wrote:
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

 

 


david_r
Evangelist
  • October 18, 2018
georgefloros wrote:
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.

Forum|alt.badge.img
david_r wrote:
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.

 

 


david_r
Evangelist
  • October 23, 2018
georgefloros wrote:
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 :-)

 

 


michielschram
Forum|alt.badge.img
kouri1986 wrote:

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


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