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
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.
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?