Renaming files using list

Questions and answers on how to get the most out of FFAStrans
Post Reply
ring4life70
Posts: 53
Joined: Thu Apr 15, 2021 6:22 am

Renaming files using list

Post by ring4life70 »

Hi,
Tried to find the answer in the forum but didn't find anything sorry if already answer...

i need your help to create a workflow in order to replace old file name of files in a folder using a txt or a csv list containing the new file name mapping information:
I have files available in a directory as:

old_filename_1
old_filename_2
old_filename_3
old_filename_4

I have another file that contains mapping information for how I want to rename each file. This is a CSV file example.

old_filename_1,filename30_6
old_filename_2,filename30_7
old_filename_3,filename60_3
old_filename_4,filename60_4

My files should be renamed to the following.

filename30_6
filename30_7
filename60_3
filename60_4

How can I do this?
thanks
emcodem
Posts: 1652
Joined: Wed Sep 19, 2018 8:11 am

Re: Renaming files using list

Post by emcodem »

Hi,

thats pretty simple, as long as you can guarantee that it is the full original name in the csv, correctly written including upper/lowercase being correct.
in that case, we can just search the line that contains the original name in the csv and extract the part after the comma:
Untitled.png
Untitled.png (47.28 KiB) Viewed 1679 times
The regex i use for finding the line and getting everything "from comma to the end of line":

Code: Select all

$regext("%s_file_readout%","%s_original_name%,(.+?)\r")
Note that this regex assumes you created and saved your csv using windows, which puts a \r\n at the end of every line. If you dont have \r in the file, you could replace \r by \n to indicate line ends.

when you have the part of interest, you can use a simple replace to calculte the full final path:

Code: Select all

%s_new_full% = $replace("%s_source%", "%s_original_name%","%s_new_filename%")
and finally use a cmd processor to move the file:

Code: Select all

cmd /C "move "%s_source%" "%s_new_full%""
Note that i use move instead of rename here because "move" works on all OS and the windows stock "rename" command does not accept full target path.

For Error handling:
1) check if we found something in the csv by checking if s_new_full is not empty
2) probably set s_success to %s_new_filename% at the very end, so you can see the calculated name in the job monitor
3) cmd processor with move cmd should fail if the target exists
emcodem, wrapping since 2009 you got the rhyme?
ring4life70
Posts: 53
Joined: Thu Apr 15, 2021 6:22 am

Re: Renaming files using list

Post by ring4life70 »

Hi emcodem,
thanks for your help, I'm probably doing something wrong because my workflow doesn't work, instead of renaming my 3 files, at the end of the workflow I have only one .txt file, probably the new file names are not read in the csv file, and the workflow creates only one file which overwrites the two more.

for example I don't understand where I have to put this part of the workflow

CODE: SELECT ALL

%s_new_full% = $replace("%s_source%", "%s_original_name%","%s_new_filename%")
emcodem
Posts: 1652
Joined: Wed Sep 19, 2018 8:11 am

Re: Renaming files using list

Post by emcodem »

ring4life70 wrote: Tue Dec 27, 2022 4:57 pm probably the new file names are not read in the csv file,
OK so i'd really like to get you to a point where you are able to debug such stuff on your own.
Are you aware about the options to use either populate proc and set s_success to some variable value OR using the write text file proc that you can use for investigating the actual value of any of your variables?

When we build a workflow that constructs some variable, we first always disconnect first the rest of the workflow and as a very first step make sure that our desired variable was constructed correctly.

If you have troubles with any of that, please start by checking out the tutorial thread, there is a section about how to debug variable values using the write text proc:
viewtopic.php?f=5&t=849#p3566
ring4life70 wrote: Tue Dec 27, 2022 4:57 pm and the workflow creates only one file which overwrites the two more.
Sorry i did not pay attention to the specific behaviour of windows move command. It does in deed overwrite existing files by default (who invented this?). If you don't want to do that, your workflow has to check if the file exists.

Example workflow with some error handling:
emcodem_rename_use_csv.json
(6.33 KiB) Downloaded 72 times

One more thing to mention:
The last line of the csv is special because it might usually not contain a newline character at the end. So the regex i proposed above would not be able to work with the "last line of the csv". You could just put a newline after the last line or insert some bogus line at the end but we could also handle that using regex:

Code: Select all

$regext("%s_file_readout%","%s_original_name%,(.+?)\r|%s_original_name%,(.+?)$")
Explained: In this case, my regex searches using OR Pattern ("|" Symbol), so it is actually 2 regexes in one:

Code: Select all

%s_original_name%,(.+?)\r
Will match all lines except the last one

Code: Select all

|
this is the OR operator

Code: Select all

%s_original_name%,(.+?)$
This second regex works with the last line only.
emcodem, wrapping since 2009 you got the rhyme?
ring4life70
Posts: 53
Joined: Thu Apr 15, 2021 6:22 am

Re: Renaming files using list

Post by ring4life70 »

Hi Emcodem
Your expletation is as always very clear, i'm sorry but i don't have much confidence with variables world,
i hope to arrive at a point where i'm able to perform these operations by myself.

Your workflow is working as i expected.

Thank you again
momocampo
Posts: 592
Joined: Thu Jun 08, 2017 12:36 pm
Location: France-Paris

Re: Renaming files using list

Post by momocampo »

Hello ring4life70,
Variables are very easy to use, you just have to understand what is their purposes. There are 2 kinds of variables in FFAStrans : static and ....variable :)
Statics never change and can be useful to add a path in a node for example (and avoid to write it again).
Variables can be created by FFASTrans or user and can change each time a job starts.
You have to understand variables if you want to use FFAStrans, it's a "must have" :)

If you need help, feel free to tell us but you can go here too :

https://ffastrans.com/wiki/doku.php?id= ... _variables

Cheers.
ring4life70
Posts: 53
Joined: Thu Apr 15, 2021 6:22 am

Re: Renaming files using list

Post by ring4life70 »

Hi Momocampo,

I agree with you understanding variables is a must, actually my difficulty is not in the variables which are quite simple and explained very well in the link you sent me, but in assigning values to functions such as:

$regext("%s_file_readout%","%s_original_name%,(.+?)\r|%s_original_name%,(.+?)$")

thanks again
emcodem
Posts: 1652
Joined: Wed Sep 19, 2018 8:11 am

Re: Renaming files using list

Post by emcodem »

Yeah regex is something that even some experienced programmers tend to avoid because of it's akward syntax. It is kind of a standalone mini scripting language but one that is very hard to read. The good thing about it is that it allows to do so many things in terms of string operations without writing an actual script. Also, from a programmers perspecitve it is great because it can be used in nearly all existing programming languages and it is always the same, so no matter in which language i script or program, regex is there and it is always the same. Kind of the most common denumerator for all programming :D

If you take a closer look, you will find a pattern that repeats in like 99% of all regex examples that i post here:

Code: Select all

START(.+?)END
This means in regex take everything "between" start and end. E.g.

Code: Select all

Ear(.+?)Ear
Would return "Head" :D
We can also use it for XML parsing:

Code: Select all

<xmltag>(.+?)</xmltag>
Or JSON parsing:

Code: Select all

"field":"(.+?)"
Understanding that will make you pretty mighty when it comes to locate desired values in strings.

Alternatively there is also the $between function in ffastrans which might be easier to understand but is also a lot more limited of course.
emcodem, wrapping since 2009 you got the rhyme?
momocampo
Posts: 592
Joined: Thu Jun 08, 2017 12:36 pm
Location: France-Paris

Re: Renaming files using list

Post by momocampo »

Yep, I should told you too -> Regex is a must have too :)
it will help you so much to select only what you need/want.

Cheers ;)
Post Reply