Skip to main content
Solved

Is there a quick way to rename files?

  • March 22, 2017
  • 14 replies
  • 1555 views

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.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

14 replies

erik_jan
Contributor
Forum|alt.badge.img+22
  • Contributor
  • 2179 replies
  • 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
Celebrity
  • 7843 replies
  • 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+12
  • Contributor
  • 605 replies
  • 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
  • 120 replies
  • 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_defra
Contributor
Forum|alt.badge.img+5
  • Contributor
  • 61 replies
  • 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
  • 1 reply
  • March 22, 2017

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
  • 52 replies
  • 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
  • 25 replies
  • October 17, 2018

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
Celebrity
  • 8392 replies
  • October 17, 2018

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
  • 25 replies
  • October 18, 2018
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
Celebrity
  • 8392 replies
  • October 18, 2018
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
  • 25 replies
  • October 23, 2018
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
Celebrity
  • 8392 replies
  • October 23, 2018
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
Contributor
Forum|alt.badge.img+6
  • Contributor
  • 16 replies
  • February 18, 2021

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