How to rename files with increasing numbers

Hello,

I’m trying to rename a list of files in a folder in this particular way:

original name new name
ABCxxxxxxx A01
ABCyyyyyyy A02
ABCzzzzzzz A03
DEFrrrrrrrrrr D01
DEFtttttttttt D02

In other words :

  • the first letters of the original name have a fixed new name
  • the file numbering starts at 01 en goes on for the same first letters
  • when the first letters change to an new category, the numbering starts again at 01.

Could someone tell me how to solve this? Please in activities as I’m not familiar with much code.

Thanks in advance,
Riana

@Riana

  1. Use for each file in folder
  2. In that use a filter on filename and use A* in that field
    1. Inside loop use the currentindex to get the current file index and just use rename with index so tht for each file it changes
  3. To make it work for all…use another loop around it with all letters you need as list…and in filter in inner loop use currentitem+"*"

Cheers

Hi @Riana

Here is the skeleton to a solution.

  • According your use case you might want to use temporary directories, etc. instead of renaming directly the files, or use temporary names to avoid pattern clashes, etc.
  • You can also use a datatable instead of a dictionary, for example if you want to maintain the oldName/newName in an excel file; convert it to a dctionary afer load or adapt accordingly.
  • You can keep the logic into the loop in its own process for reuse and clarity. You can organise it better but for the sake of the explanation, I put it all together.

Workflow Overview:

  • Initial Setup: Assign activities to set folder path, dictionary, and regex.
  • Loop Through Files: A For Each activity to process all files.
  • Prefix Handling: Regex match and dictionary lookup with If conditions.
  • File Counting: Use Directory.GetFiles
  • Renaming: Use string formatting.
  • Move Files: Rename files with the Move File activity.

Step-by-step

  1. Initialise the folder path:
    Use an Assign activity:

    folderPath = "C:\Your\Folder\Path"
    
  2. Create a dictionary for prefix-to-new-name mapping:
    Use an Assign activity to initialise a dictionary of type Dictionary(Of String, String):

    prefixToRoot = New Dictionary(Of String, String) From {
        {"ABC", "A"},
        {"DEF", "D"}
    }
    
  3. Get the list of files in the folder:
    Use an Assign activity:

    files = Directory.GetFiles(folderPath)
    

    (Variable type: String[])

  4. Set up a regular expression to extract prefixes:
    Use an Assign activity to initialise the Regex:

    prefixRegex = New System.Text.RegularExpressions.Regex("^[a-zA-Z]+")
    

    (Variable type: Regex)

  5. Iterate over the files:
    Add a For Each activity to loop through the files:

    • Type of item: String
    • Variable name: file
  6. Extract the prefix with Regex:
    Inside the loop:

    • Use an Assign activity to get the file name without the extension:
      fileName = Path.GetFileNameWithoutExtension(file)
      
    • Use another Assign activity to extract the prefix:
      match = prefixRegex.Match(fileName)
      
      (Variable type: Match)
    • Add an If activity to check if the prefix was successfully extracted:
      match.Success
      
  7. Check if the prefix exists in the dictionary:
    Add another If activity to verify:

    prefixToRoot.ContainsKey(match.Value)
    
    • True: Proceed with the following steps.
    • False: Add a Log Message activity to indicate that the file is being skipped.
  8. Count existing files for the prefix in the folder:
    Use an Assign activity to find matching files:

    existingFiles = Directory.GetFiles(folderPath, $"{prefixToRoot(match.Value)}*")
    count = existingFiles.Length + 1
    
  9. Generate the new file name:

    • Use an Assign activity to format the new number with zero-padding:
      newNumber = count.ToString("D2")
      
    • Use another Assign activity to construct the new file name:
      newFileName = $"{prefixToRoot(match.Value)}{newNumber}{Path.GetExtension(file)}"
      
  10. Rename the file:

    • Use an Assign activity to construct the full path for the new file:
      newFilePath = Path.Combine(folderPath, newFileName)
      
    • Add a Move File activity:
      • Source: file
      • Destination: newFilePath
  11. Handle files without valid prefixes or missing mappings:

    • For files that don’t match the Regex or whose prefixes aren’t in the dictionary, add a Log Message activity to skip them. (or fail loudly, or handle it the way you want)

Txs for the help, I’m going to look into it.

Grz,
Riana

1 Like

Whaw, thank you very much. I’m going to try this !

Grz
Riana