I have 50 files with file names having the format like:
SIGMOID 01012019 - 01082019.XLS
SIGMOID 01012019 - 01022019.XLS
I would like to change it to:
SIGMOID_01012019 - 01082019.XLS
SIGMOID_01012019 - 01022019.XLS
I want to update part of the filename and add an underscore instead of just the first blank space in the name.
Thank you
I tried writing a script in powershell
dir |
Where-Object { $_.name.Contains(" ") } |
Rename-Item -NewName { $_.name -replace " ","_" }
but that updates every blank space.
Suppose you have files like:
SIGMOID 01012019 - 01082019.XLS SIGMOID 01012019 - 01022019.XLS SOMETHINGELSE 01012019 - 02022019.XLS YETANOTHERPREFIX 01012019 - 03022019.XLS
then the following code will rename them:
$sourceFolder = 'THE PATH OF THE ROOTFOLDER THAT STORES THE XLS FILES TO RENAME' #'# enter your rootpath here
Get-ChildItem -Path $sourceFolder -File |
Where-Object { $_.Name -match '^\w+ +\d{8}.*\.xlsx?' } |
ForEach-Object {
$_ | Rename-Item -NewName ($_.Name -replace '^(\w+) +(\d{8}.*\.xlsx?)', '$1_$2') -WhatIf
}
to become
SIGMOID_01012019 - 01082019.XLS SIGMOID_01012019 - 01022019.XLS SOMETHINGELSE_02012019 - 01022019.XLS YETANOTHERPREFIX_03012019 - 01022019.XLS
Note: remove the -WhatIf
if you feel happy with the results
Regex details:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ Match the character “ ” literally
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\. Match the character “.” literally
xls Match the characters “xls” literally
x Match the character “x” literally
? Between zero and one times, as many times as possible, giving back as needed (greedy)
)