Rename file


I’m trying to rename my files. My file name syntax looks like this: AAA_BBB_CCC.xlsx where
BBB has 4 possibilities ‘football’,‘basketball’,‘volleybal’,‘rugby’.
If the file contains football then I should rename the file as AAA_1_CCC.xlsx.
If the file contains basketball then I should rename the file as AAA_2_CCC.xlsx
If the file contains volleyball then I should rename the file as AAA_3_CCC.xlsx
If the file contains rugby then I should rename my file as AAA_4_CCC.xlsx

Thanks in advance,
Kind regards



You can use the Move File activity to make your renames.

Just to keep it simple, here is some pseudocode to represent the logic used:

For each file in System.IO.Directory.GetFiles(directorypath)
    If file.ToUpper.Contains("FOOTBALL")
        Move File
        //to destination: System.Text.RegularExpressions.Regex.Replace(file, "(f|F)(o|O)(o|O)(t|T)(b|B)(a|A)(l|L)(l|L)", "1")

//... and so on

Alternatively, you can use a Switch instead of many If activities

For each file in System.IO.Directory.GetFiles(directorypath)
    Switch //condition: If(file.ToUpper.Contains("FOOTBALL"),"football",If(file.ToUpper.Contains("RUGBY"),"rugby","default")
        Case1: "football"
                     Move File
                      //to destination: System.Text.RegularExpressions.Regex.Replace(file, "(f|F)(o|O)(o|O)(t|T)(b|B)(a|A)(l|L)(l|L)", "1")
        Case2: "rugby"
                     Move File
                      //to destination: System.Text.RegularExpressions.Regex.Replace(file, "(r|R)(u|U)(g|G)(b|B)(y|Y)", "4")

…I used Regex so you can make it not case-sensition.

I hope this helps and sparks some ideas.



Hi @ClaytonM,

Thanks! Works perfectly (I tried the first option). However, it now created a duplicate of the file (in the same folder) with the new name. I wanted to get rid of the original file. How should I do this?


Are you sure you used Move File and not Copy File? Move File shouldn’t keep a duplicate.

Yes I am using the move file activity.
I just noticed what went wrong, it was because of a conflicting sequence following the moving of files.

Thanks a lot, you saved my day :slight_smile: