How to rename files using a list file? - powershell

I have 100s of files that I would like to rename in a specific manner according to a correlated list. Rather than repeating the same cmdlets, as below, I would like to point the cmdlet to a couple of list files (below) containing the old and new names, say old.txt and new.txt. How do I do this using PowerShell?
Example:
Rename-Item -Path "E:\MyFolder\old1.pdf" -NewName new1.pdf
Rename-Item -Path "E:\MyFolder\old2.tif" -NewName new2.pdf
Rename-Item -Path "E:\MyFolder\old3.pdf" -NewName new3.pdf
List Files:
old.txt =
old1.pdf
old2.tif
old3.pdf
new.txt =
new1.pdf
new2.tif
new3.pdf

To avoid misalignment between the entries in old.txt and new.txt files, you could also put the files into a CSV file:
renames.csv =
old1.pdf,new1.pdf
old2.tif,new2.tif
old3.pdf,new3.pdf
Now you can use Import-Csv to import your "translation sets":
$Renames = Import-Csv '.\renames.csv' -Header "OldName","NewName"
foreach($File in $Renames) {
Rename-Item $File.OldName $File.NewName
}

Simply read your two files into two lists and process them like this:
$old = Get-Content 'old.txt'
$new = Get-Content 'new.txt'
Set-Location '$:\MyFolder'
for ($i = 0; $i -lt $old.Count; $i++) {
Rename-Item $old[$i] $new[$i]
}

So just for yucks, here is the one-liner version. It does depend on you being in the C:\MyFolder dir when you run it but that could easily be changed to a variable:
Get-Content new.txt -PipelineVariable NewName |
Foreach {$oldNames = Get-Content old.txt;$i=0} {Get-ChildItem $oldNames[$i++]} |
Rename-Item -NewName {$NewName} -WhatIf

Related

replacing files names with split output

I am trying to use PowerShell to read filenames from a dir;
then within a for loop:
split names using a delimiter; store desired output in a new variable. Now I want to replace the original filenames in the directory with this new variable. So far I have gathered the following with the expected outputs shown:
$files = Get-ChildItem -Path C:\Test
write-output $files
Directory: C:\Test
1_N04532L_LEFT.JPG
2_N04532R_RIGHT.JPG
code continues
foreach ($file in $files)
{
$nameArray = $file -split "_"
$newName = $nameArray[1]
write-output $newName
}
N04532L
N04532R
Any Ideas on how to accomplish this. I am not a programmer and there is lots of data on this, but it's not working for me.
As both commenters already explained, there is the Rename-Item cmdlet for renaming files.
Since this cmdlet can take a scriptblock in its NewName parameter, you can use that to create a new filename.
# adding switch -File makes sure you do not also try to rename subfolders
$files = Get-ChildItem -Path 'C:\Test' -File
foreach ($file in $files) {
$file | Rename-Item -NewName { '{0}{1}' -f ($file.BaseName -split '_')[1], $file.Extension }
}
You can shorten this by piping the results from Get-ChildItem trhough one-by-one to the Rename-Item cmdlet.
Because we're piping the FileInfo objects here, we can make use of the $_ automatic variable
# enclose the Get-ChildItem cmd in brackets so this will enumerate the files to completion
# before passing them on to te Rename-Item cmdlet.
# if you don't, files you already have renamed could be picked up and processed again..
(Get-ChildItem -Path 'C:\Test' -File) |
Rename-Item -NewName { '{0}{1}' -f ($_.BaseName -split '_')[1], $_.Extension }
Note: when renaming files, you can always run into naming collisions, upon which you will receive an exception

Is it possible to rename each file in a folder with a different name using powershell or a batch file?

I've tried looking this up but have got nowhere so far and I'm on a time limit.
Let's say I have three files that have similar clones in multiple folders:
(folder1)
image1.png
image2.png
image3.png
(folder2)
image1.png
image2.png
image3.png
I want to rename these using cmd prompt, powershell, or using a .bat to:
(folder1)
B-Sign.png
B-Gauge.png
B-Cup.png
(folder2)
G-Sign.png
G-Gauge.png
G-Cup.png
I intend to run the commands for each folder as only the front of the name is different. I want something simple.
rename-item *.png B-Sign.png
rename-item *.png B-Gauge.png
When it needs a different prefix I would just find and replace the prefix with the new one using ctrl+H in notepad.
Problem is I can't figure out, in any of these, how to automatically cycle to the next file in the folder instead of changing all of the files' names at once. Any one can help?
Yes that's very easy in PowerShell:
Get-ChildItem "C:\temp\Folder1" |
Rename-Item -NewName { "B-" + $_.Name }
The new name is a result of an Expression that Rename-Item knows to evaluate. Because the information is sent down the pipeline the Rename-Item command is run once per file, resulting in names like your example.
Also you can use the filter parameter as you described with either of the below:
Get-ChildItem "C:\temp\Folder1\*.png" |
Rename-Item -NewName { "B-" + $_.Name }
I prefer to filter using the -Filter parameter:
Get-ChildItem "C:\temp\Folder1" -Filter *.png |
Rename-Item -NewName { "B-" + $_.Name }
You can store the folder in a variable too:
$Folder = "C:\temp\Folder1"
Get-ChildItem $Folder -Filter *.png |
Rename-Item -NewName { "B-" + $_.Name }
Update:
Per comments here's an example to correlate a list of prefixes with the renames you want to do:
$Prefix = 'B-'
$NewNames =
#(
'Sign'
'Guage'
'Cup'
)
# If New names are stored ina file simply do:
# $NewNames = Get-Content <FilePath>
$Folder = "C:\temp\Test_10-30-20"
$Files = Get-ChildItem $Folder -Filter *.png
For( $i = 0; $i -lt $Files.Count; ++$i )
{
$CurrentFile = $Files[$i]
$NewName = $Prefix + $NewNames[$i] + $CurrentFile.Extension
Rename-Item $CurrentFile.FullName -NewName $NewName
}

How to rename all files in directory based on contents of the 2nd line [POWERSHELL]

How to make this script read the second line on text in each file(s) in the directory recursively, and rename the file itself according to the contents of line #2?
This script was found here:
Renaming text files based on the first word in the file in Powershell
The script renames only the very first file in the folder using the first line of text, below are the top two lines of text in the file:
sample name O123456.NT; wanted rename to: O14294 (CXP-14294).NT
line 1: %
line 2: O14294 (CXP-14294)
...
... rest of file hundreds of lines long etc
$files = Get-ChildItem *.NT
$file_map = #()
foreach ($file in $files) {
$file_map += #{
OldName = $file.Fullname
NewName = "{0}.NT" -f $(Get-Content $file.Fullname| select -First 2)
}
}
$file_map | % { Rename-Item -Path $_.OldName -NewName $_.NewName }
Another option, getting the second line by indexing:
$files = (Get-ChildItem -path G:\Test -Filter *.ng).FullName
foreach ($File in $Files) {
$MyName = (get-content -Path "$File")[1] + ".ng"
Rename-Item -Path "$File" -NewName "$MyName"
}
HTH
This can be done a little more succinctly by just working with the name instead of full name. The trick to getting just the second line from the file is to skip one, then take just one.
So:
Get-ChildItem *nt |
ForEach-Object{
$oldname = $_.name;
$newname = "{0}.nt" -f $(Get-Content $oldname | Select-Object -Skip 1 -First 1);
Rename-Item -Path $oldname -NewName $newname
}

Renaming multiple files with different names

I am a programmer by no means and am brand new to using powershell, but have been tasked with setting up some batch export processes for daily files we FTP. I need to come up with a script that will take changing file names and change them within the same directory to new names;
Example: files will come in as below (YYYYMMDD will be the changing variable)
YYYYMMDD_Share_Support.txt
YYYYMMDD_Person_Support.txt
We need them to be stripped from the above to:
Share.txt
Person.txt
so on and so forth.
I have found ways to make this work, but only on an as needed basis for one file at a time with specific names, not names that will change daily.
So far I am using:
Get-ChildItem -Filter *.txt
Dir | %{Rename-Item $_ -NewName ("NEWFILENAME.txt" -f $nr++)}
You could use the regex -replace operator inside a pipeline-bound scriptblock:
$files = Get-ChildItem -filter *.txt
$files |Rename-Item -NewName { $_.Name -replace '^\d{8}_(.*)_Support\.txt$', '$1.txt' }
As suggested by TheIncorrigible1, if you know the relative position of the word you need, you can also use -split:
$files |Rename-Item -NewName {'{0}.txt' -f ($_.Name -split '_')[-2]} # grab 2nd last word
How about:
dir *.txt |
rename-item -newname { $null,$base,$null = $_.basename -split '_'; "$base.txt" } -whatif
Probably a longer version of the answer. An alternative mentioned by #TheIncorrigible1
$logDir = "D:\Satish\TestFolders"
cd $logDir
$files = ls
foreach ($file in $files){
$fileSplit=($file.ToString()).split("_")
ren $file "$($fileSplit[1]).txt"
}
And for Share.txt to YYYYMMDD_Share_Support.txt
$logDir = "D:\Satish\TestFolders"
cd $logDir
$files = ls
$date = Get-Date -Format "yyyyMMdd"
foreach ($file in $files){
$fileSplit=($file.ToString()).split(".")
ren $file "$($date)_$($fileSplit[0])_Support.txt"
}

Rename files with Powershell, file name already exist

I try to rename in batch files in multiple folders by removing - .extension
Example files:
test file 1 - 1234.docx
test file 1 - 4321.docx
test file 1 - 6789.docx
For this I run the following script:
Get-ChildItem -recurse -exclude .dxl | foreach {
Rename-Item -Path $_.FullName -NewName (($_.BaseName -replace '(.)-.*?$','$1') + $_.Extension) }
This will give me the following error:
"Rename-Item : Cannot create a file when that file already exists."
How can I get rid of this by adding (1), (2), (3) if the file already exist after renaming?
Here is something I came up with for this problem:
$FileTracker = #{}
$files = (Get-ChildItem -recurse -exclude "*.dxl" -file | sort fullname).fullname
$files | foreach {
$Base = (($_ | select-string -pattern "(.*)-.*?$").matches.groups[1].value).Trim()
if ($FileTracker[$Base]) {
Rename-Item -path $_ -newName ((split-Path $Base -Leaf),"($($FileTracker[$Base]))",($_ -replace ".*(\..*?)$",'$1') -join "")
$FileTracker[$Base]++
}
else {
$FileTracker.add($Base,1)
Rename-Item -path $_ -newName ((split-path $Base -Leaf),($_ -replace ".*(\..*?)$",'$1') -join "")
}
}
Keep in mind that this solution looks to make files unique in their respective directories. If it sees test file 1 - 23.txt in two different directories, it will rename them both to test file 1.txt. If you already have files that have been renamed like testfile 1(1).txt and testfile 1(2).txt, there will be issues with this working. So I need to know if that is a condition.