Powershell offset number in name of multiple files by a constant - powershell

I have multiple files from camera with names "00010001", "00010002", etc. Those are multiple file types (JPG, CR2, xmp, MOV) in one folder (lets say C:\camera).
I need to add 10000 to number in all file names, so it becomes "00020001", "00020002", etc. I guess this could be done with a simple script in powershell, but I have absolute no experience with it. I would be very grateful if someone helped me with it. Thank you.

If the code should also handle files in subfolders of 'C:\Camera', you can use the -Recurse and -Include parameters:
(Get-ChildItem -Path 'C:\Camera' -File -Include '*.JPG','*.CR2','*.XMP','*.MOV' -Recurse) |
Where-Object { $_.BaseName -match '\d{8}' } |
Rename-Item -NewName { '{0:D8}{1}' -f ([int]$_.BaseName + 10000), $_.Extension } -WhatIf
If however this should NOT go any deeper that the root folder 'C:\Camera', then do this:
(Get-ChildItem -Path 'C:\Camera' -File) |
Where-Object { $_.BaseName -match '\d{8}' -and $_.Extension -match '\.(JPG|CR2|XMP|MOV)$' } |
Rename-Item -NewName { '{0:D8}{1}' -f ([int]$_.BaseName + 10000), $_.Extension } -WhatIf
Once you are satisfied the code would create the new filenames as you wish, remove the safety switch -WhatIf
Explanation:
Get the files in the folder that have BaseNames consisting of 8 digits ($_.BaseName -match '\d{8}') and that have an extension of either JPG, .CR2, .XMP or .MOV
Convert that BaseNaem into an interger number and add 10000
Reassemble the file name with the -f format operator, where the template string {0:D8}{1} makes sure all (new) numbers are prefixed with zeroes up to 8 digits

Related

How to add a symbol in a middle of a file name in PS

I have modified several thousand files with a various things requested by the owners.
Now, I need to add one final thing and I am not 100% on how to do it.
Scenario is as follows - all files have a 10 digit number at the start, I need to add a hyphen after the number. String is a variable but it is always the same length.
1234567890abcdefgh.xls would be an example
I have used GCI to make changes to symbols and static parts but not sure how to call for insertion in a specific place of a file (after the 10th character of a variable string)
Any ideas would be most welcome!
You can use the $matches you get from the capturing groups of a -match comparison:
(Get-ChildItem -Path 'X:\WhereTheFilesAre' -File) |
Where-Object { $_.BaseName -match '^(\d{10})([^-].*)' } |
Rename-Item -NewName { '{0}-{1}{2}' -f $matches[1], $matches[2], $_.Extension }
or by using the Substring() method:
(Get-ChildItem -Path 'X:\WhereTheFilesAre' -File) |
Where-Object { $_.BaseName -match '^\d{10}[^-]' } |
Rename-Item -NewName { $_.Name.Substring(0,10) + '-' + $_.Name.Substring(10) }
or use the regex -replace operator:
(Get-ChildItem -Path 'X:\WhereTheFilesAre' -File) |
Where-Object { $_.BaseName -match '^\d{10}[^-]' } |
Rename-Item -NewName { $_.Name -replace '^(\d{10})', '$1-' }
You can use string.Insert() to insert a string into another at a specific offset:
PS ~> '1234567890abcdefgh.xls'.Insert(10, '-')
1234567890-abcdefgh.xls
To apply to all files in a directory, you could do something like this:
Get-ChildItem -File |Where-Object Name -match '^\d{10}[^-]' |Rename-Item -NewName { $_.Name.Insert(10, '-') }
The regular expression pattern ^\d{10}[^-] will only match file names that start with 10 digits followed by something other than a hyphen (to avoid renaming files that already comply with the naming convention)

How can I use powershell to rename files with padded zeros

I need to limit the files affected to Example*.pdf. The files have 1-3 digits in the names and I need to standardize them. So Example_1.pdf -> Example_001.pdf while Example_100.pdf -> Example_100.pdf
The first part renamed files to Example.1.pdf so I could parse them with a single delimiter, but it gave me errors on the second step (cmdlet rename-item at command pipeline position 1 supply values for the following parameters: path)
Get-ChildItem of* | rename-item -newname { $_.Name -replace '_','.' }
Get-ChildItem of* |
foreach {
$nameArray = $_.Split('.')
$ExampleNumber = $nameArray[1]
rename-item -path $Path -newname $nameArray[0]+$ExampleNumber+$nameArray[2]
}
But if I can get something like this to work then I can play around with $ExampleNumber
Then I tried using regular expressions. Had this worked it would have padded the single digit files and then I could make a second pass for double digit files. But it didn't rename anything.
Get-ChildItem ex* | rename-item -newname { $_ -replace '(.*)(\d{1})\.pdf', 'Example_0$2.pdf'}
Any help is appreciated.
Note the $_ inside the -NewName block is of type FileInfo.
Here's my suggestion:
Get-ChildItem ex* | Rename-Item -NewName {
[void]($_.Name -match "\d+")
$_.Name -replace "\d+", (([int]$Matches[0]).ToString("000"))
}
Or alternatively:
Get-ChildItem ex* |
Rename-Item -NewName {
$_.Name -replace "\d+", (([int][regex]::match($_.Name, "\d+").value).ToString("000"))}

How do I remove Blank Space from File Names

I am trying to remove blank spaces from many file names using PowerShell 3.0. Here is the code that I am working with:
$Files = Get-ChildItem -Path "C:\PowershellTests\With_Space"
Copy-Item $Files.FullName -Destination C:\PowershellTests\Without_Space
Set-Location -Path C:\PowershellTests\Without_Space
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace ' ','' }
For example: the With_Space directory has these files:
Cable Report 3413109.pdf
Control List 3.txt
Test Result Phase 2.doc
The Without_Space directory will need the above file name to be:
CableReport3413109.pdf
ControlList3.txt
TestResultPhase 2.doc
Currently, the script shows no error but it only copies the source files to the destination folder, but doesn't remove the spaces in file names.
Your code should work just fine, but since Get-ChildItem *.txt lists only .txt files the last statement should remove the spaces from just the text files, giving you a result like this:
Cable Report 3413109.pdf
ControlList3.txt
Test Result Phase 2.doc
This should remove spaces from the names of all files in the folder:
Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace ' ','' }
Prior to PowerShell v3 use this to restrict processing to just files:
Get-ChildItem | Where-Object { -not $_.PSIsContainer } |
Rename-Item -NewName { $_.Name -replace ' ','' }
something like this could work
$source = 'C:\temp\new'
$dest = 'C:\temp\new1'
Get-ChildItem $source | % {copy $_.FullName $(join-path $dest ($_.name -replace ' '))}
I think your script should almost work, except $_ isn't going to be defined as anything. By using the for-each cmdlet (%), you assign it and then can use it.
Get-ChildItem *.txt | %{Rename-Item -NewName ( $_.Name -replace ' ','' )}
EDIT:
That interpretation was totally wrong. Some people seem to have found it useful, but as soon as you have something being piped, it appears that $_ references the object currently in the pipe. My bad.

Remove nth character from filename in PowerShell

I have a large number of files in a number of directories with this type of naming convention:
"BU1_KCG_RANDOM_030515.csv", etc.
I need to remove the 16th and 17th characters. So the new file name would be "BU1_KCG_RANDOM_0515.csv".
How can I iterate the multiple directory renaming accordingly?
So far I have got the below, but I am not sure what would come next.
Get-ChildItem -Filter *.csv -Recurse | Rename-Item -NewName {$_.Name..................}
Using -replace:
Get-ChildItem -Filter *.csv -Recurse |
foreach { $_ | rename-item -newname ($_.Name -replace '(.{15})..(.+)','$1$2') }
I much prefer mjolinor's answer, but you can always do things in a different way with PowerShell. Since you can index a string like a char array we can use a little array notation to get the characters you want as well.
$oldname = "BU1_KCG_RANDOM_030515.csv"
$newname = -join $oldname[0..14 + 17..($oldname.Length)]
$newname
BU1_KCG_RANDOM_0515.csv
We also need to use -join to convert the array back into a string.
Bit basic, eg doesn't check if the file has already been renamed, but should get you to the next step
gci -filter *.csv -rec | % { Rename-Item $_ -newname ($_.Name.substring(0,15) + $_.Name.substring(17)) }
where
gci = get-childitem
% = for-each
$_ = this (inside the for-each)

Add counter as suffix to all files in subfolders

I have this problem with renaming 50,000 files, separated to numerous folders. the problem is that the original sequential naming is all massed up because some file were deleted, so the count jumps from let's say *0005 to *0007.
I want to rename all the files in all subfolders, so that the suffix will be a 4 digit number, based on the current order of the files. Most importantly I want the counter the restart in each folder.
I have one working script that renames the files according putting a general name and then the folder name and last the current file name.
Get-ChildItem C:\test -Filter *.tif -Recurse | Rename-Item -NewName { 'IL-TRUM'+'_'+$_.Directory.Name+'_'+$_.name}
All I need to do is to add another counter at the end!
If you want the counter to restart in each folder you need to separate folder recursion from enumerating/renaming the TIFF files in each folder. Try something like this:
Get-ChildItem -LiteralPath 'C:\test' -Recurse | Where-Object {
$_.PSIsContainer
} | ForEach-Object {
$cnt = 0
Get-ChildItem -LiteralPath $_.FullName -Filter '*.tif' | ForEach-Object {
Rename-Item -LiteralPath $_.FullName -NewName ("IL-TRUM_{0}_{1}_{2:d4}{3}" -f $_.Directory.Name, $_.Name, $cnt, $_.Extension)
$cnt++
}
}