Home > Net >  Using PowerShell to Append a number to the beginning of a filename when using a reference csv as inp
Using PowerShell to Append a number to the beginning of a filename when using a reference csv as inp

Time:10-21

I'm hoping someone out there already has a handy bit of PowerShell or similar to help me out with this. It's a one-off task, but one that would take days/weeks to run through manually. I'll keep the objective as simple as possible.

I have few thousand files in a directory with the following format: BL_string1_string2.pdf

All the names of these files need changing so that a corresponding unique string and underscore is added to the start of the file name, like so: string3_BL_string1_string2.pdf

The values required for each file are recorded as columns in rows of a SQL db table. I have exported this data in values exported in a csv with headers for all the string1 & string3 values for these files.

How can use the csv as an input to automate the renaming of these files?

Example:

Csv has 2 columns/headers: "string1" and "string3" (the "string2" value is immaterial).

If "string1" for a row is abc123 and"string3" for that row is 456xyz, the corresponding file to be renamed would be renamed from "BL_abc123_blah.pdf" to "456xyz_BL_abc123_blah.pdf

I can get by with PowerShell when required, but the mechanics of this one are a little beyond me I'm afraid. Any help appreciated.

Thanks

CodePudding user response:

Thanks for explaining further in your comment.

If you say you have thousands of files to rename, using a Hashtable for looking up the prefix string would be the fastest way to go.

# Import the csv file and then convert the resulting array of objects into a Hashtable for speed
$csvDtata   = Import-Csv -Path 'X:\SomeWhere\replacements.csv'
$lookupHash = @{}
# each Key will be the string to compare against, the Value is the prefix for the file name
# string1 and string3 are the headers in the csv file
foreach ($item in $csvDtata) { $lookupHash[$item.string1] = $item.string3 }  

# get a list of files from the folder
$allFiles = Get-ChildItem -Path 'X:\PathTo\TheFiles' -Filter 'BL_*_*.pdf' -File
# loop over this list and rename where needed
foreach ($file in $allFiles) {
    $compare = ($file.Name -split '_')[1]  # take the decisive part of the name ('string1')
    if ($lookupHash.ContainsKey($compare)) {
        # we have a match, rename the file. 
        ##### See Note below #####
        $file | Rename-Item -NewName ('{0}_{1}' -f $lookupHash[$compare], $file.Name) -WhatIf
    }
}

Note: I have added -WhatIf to the Rename-Item cmdlet for safety. That switch then only outputs on screen what would be renamed, but nothing will actually happen to the file. Once you are sure that output shows the correct renaming results, remove -WhatIf and run the script again.

As always, first try this out on a couple of test files

  • Related