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.
Haha, I got an answer.
dir * | ?{$_.name -match '^\d{5}_.+'} | Rename-Item -NewName {(([convert]::toint32($_.name.substring(0, 5), 10) + 12).ToString("00000")) + $_.name.substring(5)} -whatif
Just found that I can use any code in the -newname{} block. But I still don't understand why my -replace way did not work.
Related
I have below files which i am reading using a foreach loop.
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
C:\Script\LF_Batch_1.csv
C:\Script\LF_Batch_10.csv
C:\Script\LF_Batch_11.csv
C:\Script\LF_Batch_12.csv
C:\Script\LF_Batch_13.csv
C:\Script\LF_Batch_14.csv
C:\Script\LF_Batch_15.csv
C:\Script\LF_Batch_16.csv
C:\Script\LF_Batch_17.csv
C:\Script\LF_Batch_18.csv
C:\Script\LF_Batch_19.csv
C:\Script\LF_Batch_2.csv
C:\Script\LF_Batch_20.csv
C:\Script\LF_Batch_21.csv etc...upto LF_Batch_.96.csv
Problem is it is reading the files like above not 1,2,3...and so on.
Please need idea how to read in ordered way
Solved using below approach
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$GetGeneratedFiles = $GetGeneratedFiles | Sort-Object $ToNatural
Thanks #vonPryz for the reference.
Another way. It's funny how I just did another answer similar to this. A numeric sort on the names.
echo hi | set-content (1,2,10,20 | % tostring LF_Batch_0\.csv)
dir | sort {[void]($_ -match '\d+'); [int]$matches.0}
Directory: C:\Users\js\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/17/2020 9:14 AM 4 LF_Batch_1.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_2.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_10.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_20.csv
Using the code below in PowerShell, I'm renaming many pictures. How do I modify the code to manage duplicates? Note; many of the photos have the SAME CreationTime value. As they occur, I need them to be output as:
yyyyMMdd-HHmm-1
yyyyMMdd-HHmm-2
yyyyMMdd-HHmm-3
etc.
Script:
Dir | Rename-Item -NewName {$_.CreationTime.toString("yyyyMMdd-HHmm") + ".jpg"}
Add more to your scriptblock to check paths and cycle through until it finds something unique:
Dir | Rename-Item -NewName {
$NewName=$_.CreationTime.toString("yyyyMMdd-HHmm") + ".jpg";
$i=1
While(Test-Path ".\$NewName"){
$NewName=$_.CreationTime.toString("yyyyMMdd-HHmm") + "-$i.jpg"
$i++
}
$NewName
}
Edit: Hm, I'm not sure why it would do that unless you didn't copy my code right. I ran it against a test folder and ended up with:
PS C:\temp\test> gci
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/12/2016 11:13 AM 35228885 20161012-1113.jpg
-a---- 10/12/2016 11:28 AM 31413221 20161012-1128.jpg
-a---- 10/12/2016 11:37 AM 33243498 20161012-1137.jpg
-a---- 10/12/2016 4:42 PM 2110424 20161012-1642-1.jpg
-a---- 10/12/2016 4:42 PM 3300892 20161012-1642-2.jpg
-a---- 10/12/2016 4:42 PM 3295345 20161012-1642-3.jpg
-a---- 10/12/2016 4:42 PM 101138881 20161012-1642.jpg
Edit2: Ok, fixed the issue where it can't manage more than 10 of a file with the same datetime.
try this, but modify the path C:\temp with your path directory
$listcouple=gci -File -Path "C:\temp" | select name, fullname, #{Name="Datstring";Expression={$_.CreationTime.toString("yyyyMMdd-HHmm") }} | group Datstring
foreach ($item in $listcouple)
{
if ($item.Count -gt 1)
{
$compteur=0
foreach ($value in $item.Group)
{
$compteur++
$newname= $value.Datstring + "-" + $compteur.ToString() + ".jpg"
Rename-Item -path $value.fullname -newname $newname
}
}
else
{
$newname= $item.Group[0].Datstring + ".jpg"
Rename-Item -path $item.Group[0].fullname -newname $newname
}
}
I want to bulk rename the files in my folder, and all of them have the format of FilenameYeara\b.pdf, for example, TestData2001a.pdf, File2015b.pdf. I want to rename all of them to something like [Yeara\b]Filename, such as [2001a]TestData. The problem is that I don't know how can I split my filename into two parts (actually three if we count the extension, .pdf part), such that I put that second part as the first part of the file name.
Get-ChildItem | Rename-Item {$_.name -replace ‘current’, ’old’ }
How can I achieve this?
This does the regex match "anything, four digits, one character, .pdf" and replaces it with those items in the new ordering.
PS D:\t> gci | ren -newname { $_ -replace '(.*)(\d{4})(.)\.pdf', '[$2$3]$1.pdf' }
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:54 0 File2015b.pdf
-a--- 13/05/2016 02:53 0 TestData2001a.pdf
becomes
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:53 0 [2001a]TestData.pdf
-a--- 13/05/2016 02:54 0 [2015b]File.pdf
(Maybe try it with -Whatif before running for real)
This should get you started
$Matches.Clear()
Get-Item | % {
$_.BaseName -match "(\D+)([0-9]{4}[ab])"
Rename-Item -Path $_.FullName -NewName "$($Matches[2])$($Matches[1])$($_.Extension)"
}
Sorry guys..I am new to powershell. Would be great if someone help with the following scenario:
I have couple of files in a folder c:\test
sample.x.x.1
sample.x.x.2
sample.x.x.3
sample.x.x.4
sample.x.x.5
I want to find the name of the file which has the highest number in its name in the given folder. In the above example, 5 is the highest number and the script should return the output filename as sample.x.x.5
Thanks in advance!
Sorting file names with numbers is quite a problem, as there are two ways. The first one sets them to alphabetical order. That is, 0, 1, 11, 111, 2,... The second one uses natural order. That is, 0, 1, 2, 11, 111.... This is surprisingly tricky and about every third programmer is confused with this.
There's a good answer already, which I'll refer like so,
# Create files 1..5
for($i=1;$i -le 5; ++$i) { set-content sample.x.x.$i -Value $null }
# Tricksy! Create file .10 to confuse asciibetic/natural sorting
set-content sample.x.x.10 -Value $null
ls # Let's see the files
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.1
-a---- 2015-09-28 10:29 0 sample.x.x.10
-a---- 2015-09-28 10:29 0 sample.x.x.2
-a---- 2015-09-28 10:29 0 sample.x.x.3
-a---- 2015-09-28 10:29 0 sample.x.x.4
-a---- 2015-09-28 10:29 0 sample.x.x.5
# Define helper as per linked answer
$ToNatural = { [regex]::Replace($_, '\d+$', { $args[0].Value.PadLeft(20,"0") }) }
# Sort with helper and check the output is natural result
gci | sort $ToNatural -Descending | select -First 1
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.10
Alphabetical sorting.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") | sort
sample.x.x.1
sample.x.x.11
sample.x.x.5
Numerical sorting.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") |
sort -Property #{Expression={[Int32]($_ -split '\.' | select -Last 1)}}
sample.x.x.1
sample.x.x.5
sample.x.x.11
Largest number.
PS C:\Users\Gebb> #("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") |
sort -Property #{Expression={[Int32]($_ -split '\.' | select -Last 1)}} |
select -Last 1
sample.x.x.11
I can't get this to work. It doesn't like the "(" char;
how do I fix it?
Dir | Rename-Item -NewName { $_.name -replace "(","" }
How do I handle this type of special character in PowerShell?
You've got a great explanation of the exact cause of your problem from #vonPryze, but there's a much simpler solution. The -replace operator uses regular expressions which need escaping, but the .Replace() string method just uses strings. So if you don't need a regex, just use the method and there's no need to escape anything:
dir | rename-item -NewName { $_.name.Replace("(","") }
Let's try analyzing the problem a bit. First off, create some dummy files that contain a parenthesis like so,
for($i=0;$i -le 4; ++$i) { set-content -path $("file($i).txt" -f $i) -value $i }
Output:
gci
Directory: C:\temp\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 17.2.2014 10:34 3 file(0).txt
-a--- 17.2.2014 10:34 3 file(1).txt
-a--- 17.2.2014 10:34 3 file(2).txt
-a--- 17.2.2014 10:34 3 file(3).txt
-a--- 17.2.2014 10:34 3 file(4).txt
Now that we have some test data, let's try to run the command that provied an error message like so,
Dir | Rename-Item -NewName { $_.name -replace "(","" }
Rename-Item : The input to the script block for parameter 'NewName' failed.
Invalid regular expression pattern: (.
At line:1 char:27
+ Dir | Rename-Item -NewName <<<< { $_.name -replace "(","" }
+ CategoryInfo : InvalidArgument: (file(0).txt:PSObject) [Rename-Item], ParameterBind ingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.Re nameItemCommand
Oops! The error message says that an invalid regular expression was used. That means -replace is not going to just replace strings but it supports regular expressions too!
In order to get -replace to treat the input as literal text instead of a regex, the input needs to be escaped. The simple way is to insert backslashes \, but this gets soon tedious. There luckily is an easier way, as .Net Regex class has a built-in escape method [Regex]::Excape(). Like so,
Dir | Rename-Item -NewName { $_.name -replace [regex]::escape("("),"" }
Output:
ls
Directory: C:\temp\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 17.2.2014 10:34 3 file0).txt
-a--- 17.2.2014 10:34 3 file1).txt
-a--- 17.2.2014 10:34 3 file2).txt
-a--- 17.2.2014 10:34 3 file3).txt
-a--- 17.2.2014 10:34 3 file4).txt
Dir | Rename-Item -NewName { $_.name -replace "\(","" } # This works