I have a list of files:
PS S:\temp> dir
Directory: S:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/28/2016 2:07 AM 0 00001_asdfasdfsa df.txt
-a--- 3/28/2016 2:07 AM 0 00002_asdfasdfsa df - Copy (3).txt
-a--- 3/28/2016 2:07 AM 0 00003_asdfasdfsa df - Copy.txt
-a--- 3/28/2016 2:07 AM 0 00004_asdfasdfsa df - Copy (6).txt
-a--- 3/28/2016 2:07 AM 0 00005_asdfasdfsa df - Copy (5).txt
-a--- 3/28/2016 2:07 AM 0 00006_asdfasdfsa df - Copy (4).txt
-a--- 3/28/2016 2:07 AM 0 00007_asdfasdfsa df - Copy (2).txt
-a--- 3/28/2016 2:07 AM 0 700006_asdfasdfsa df - Copy (4) - Copy.txt
PS S:\temp>
I want to renamem those that start with five numbers and an underline. The new name will add a number I specified to the leading numbers of those file names. For example, if I add 10, the new names would be:
PS S:\temp> dir
Directory: S:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/28/2016 2:07 AM 0 00011_asdfasdfsa df.txt
-a--- 3/28/2016 2:07 AM 0 00012_asdfasdfsa df - Copy (3).txt
-a--- 3/28/2016 2:07 AM 0 00013_asdfasdfsa df - Copy.txt
-a--- 3/28/2016 2:07 AM 0 00014_asdfasdfsa df - Copy (6).txt
-a--- 3/28/2016 2:07 AM 0 00015_asdfasdfsa df - Copy (5).txt
-a--- 3/28/2016 2:07 AM 0 00016_asdfasdfsa df - Copy (4).txt
-a--- 3/28/2016 2:07 AM 0 00017_asdfasdfsa df - Copy (2).txt
-a--- 3/28/2016 2:07 AM 0 700006_asdfasdfsa df - Copy (4) - Copy.txt
PS S:\temp>
Now my PowerShell code is:
dir * | ?{$_.name -match '^\d{5}_.+'} | Rename-Item -NewName {$_.name -replace '^(\d{5})(_.+)', ((([convert]::ToInt32('$1', 10) + 12).ToString("00000")) + '$2')} -whatif
I can't find any errors in my code. But when I run it, I got the following error message:
Rename-Item : The input to the script block for parameter 'NewName' failed. Exception calling "ToInt32" with "2" argument(s): "Could not find any recognizable digits."
At line:1 char:62
+ dir * | ?{$_.name -match '^\d{5}_.+'} | Rename-Item -NewName {$_.name -replace ' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (S:\temp\00001_asdfasdfsa df.txt:PSObject) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand
My question is, where I am wrong? Is it because I should not use functions in the replacement part of the replace
operator? If so, how to write complex logic in -newname{}
? Thanks.
You cannot use -replace
and massage the data at the time of the substitution. Your replacement was failing as the string literal $1
cannot be converted to integer. As discussed in a similar question: Passing a function to Powershell's (replace) function you can use a scriptblock and the .nNet regex static method Replace
.
$replaceBlock = {
# Groups 0 contains the whole match. 1 is the first group... and so on.
([convert]::ToInt32($args[0].Groups[1],10) + 12).ToString("00000") + $args[0].Groups[2]
}
Get-ChildItem | Where-Object{$_.name -match '^\d{5}_.+'} | Rename-Item -NewName {
[Regex]::Replace($_.name, '^(\d{5})(_.+)', $replaceBlock)
} -whatif
You can use the script block inline but getting it out makes for a little cleaner code. However I would likely use -match
and the returned $matches
object to do the same thing.
Get-ChildItem | Where-Object{$_.name -match '^(\d{5})(_.+)'} | Rename-Item -NewName {
([int]$Matches[1] + 12).ToString("00000") + $Matches[2]
} -whatif
Note that I updated the where-object
to have capture groups.