Renaming files from another files with loop

Hi guys,

I’ve read the related topics, which have helped me get to this point, from where I need your help because I think the issue gets different.

I want to simply rename a list of Excel files. The names are assigned in another Excel file. In the picture is where I have reached so far, with the help of the related topics.

What is different is that I thought I had to use a Loop. However, the workflow only writes the first file with the desired name. So the project works but only for the first file.

I believe I have messed things up with the loops variable. Maybe the upper variable with the name is not looping along the bottom one with the files.

Hi @tazar , you’re not really “renaming” the files, but actually opening the files, reading the content and writing the content to another file. This is quite slow.

And also, your original files still exist after this operation.

I would do the renaming via your OS rename command (ren for windows, mv for Linux/UNIX/MacOS), but I would build the command via Knime.

I’m guessing in your List of file names Excel file, you have an old_name column and a new_name column?

Just generate the OS commands like this:
Windows:
join("ren ", $old_name$, " ", $new_name$)

Linux/UNIX/MacOS:
join("mv ", $old_name$, " ", $new_name$)

And save this to a .bat file under Windows, or to a .sh file under Linux/UNIX/MacOS.

Then run the .bat or .sh file

EDIT: I put something simple together for you that looks like this:
image

My List of file names looks like this:
image

I’ll do the demo for Windows, so I use this:

And it generated these commands:
image

I filter out the other columns and keep the command column only as it’s what I want to write to the file.

I use a CSV Writer here. While there is a File Reader, there is no File Writer. A CSV file is just a text file, so we can use this to write any text file.

The CSV Writer should be configured like this:

Make sure you remove the quotes, and we don’t want to write the Header either.

This generates a file called rename_script.bat with the following commands:

 ren file1.xlsx file01.xlsx 
 ren file2.xlsx file02.xlsx 
 ren file3.xlsx file3_bak.xlsx

Here’s the sample workflow: Rename files from another files.knwf (11.4 KB)

2 Likes

Hey @tazar ,

attached you’ll find a workflow which “renames” a list of files (List Files Folders) with some new names, in my case coming from a Table Creator instead of reading those from an excel file, merging those two tables (both Tables need to have the same length) and using the column Expression node to manipulate the Paths to finally copy those files (in my case to the very same location).

renamingFiles.knwf (49.9 KB)

2 Likes

Nice one @laaaarsi . I actually started building a solution using the Transfer Files (Table), then changed direction towards using the OS rename capability cause I thought it would be faster since it’s doing a real rename/move as oppose to a copy, which the Transfer Files (Table) does. However, it could be more convenient since everything is done via Knime.

2 Likes

Thanks! You have a very cool solution and thanks for your effort in writing your post! Worth mentioning is that you are working with relative paths (would also work with absolute paths) and you have to execute the script where the files are.

2 Likes

Thanks a lot guys for you time. I finally managed it with the Transfer File node.
Thanks @bruno29a and @laaaarsi .

Unfortunately Column Expressions note is restricted to me because of the some internet restrictions of the employer. But I worked it out some other way.
I would not get it right w/o you. Thanks a lot AGAIN.

3 Likes

Thanks @laaaarsi . The relative path was more to have a place to generate the sample script. But indeed, you probably want to generate it where the files are, unless the source and destination in the list already contain the full path of the old files and new files, in which case it would not matter where the script resides.

In my case, I just needed a place to drop it, and also whoever downloads the workflow can just run it (“plug and play”) without having to configure anything to see it run.

1 Like

Is it possible to add dedicated Rename File node? In my case, I need to reorganize document catalog. Files starts with some number following the document name. Names could be misspelled and have extra symbols like “_” so on. If name is already present in the folder, add versioning (2) (3) so on.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.