?
Solved

Batch Rename with Powershell

Posted on 2016-08-06
14
Medium Priority
?
73 Views
Last Modified: 2016-08-07
Hi, I have got a list of about 140,000 .pdf files that I need to rename. I have a csv file that is in the following format

oldfilename,newfilename
example1oldname,example1newname

The files are located on a server, all in 1 folder. What would the powershell command, or a batch file script be to rename the old file name with the new file name?

We can use the example file location of : "\\server-bgs-bg02\My_Data\master\IMAGES\"

thanks for your help!
0
Comment
Question by:Brent Guttmann
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 41745775
There's not much to this one.  You just need to adjust the path to your .CSV as appropriate.
Set-Location "\\server-bgs-bg02\My_Data\master\IMAGES\"
$csvFile = "c:\temp\yourfile.csv"
Import-CSV $csvFile | ForEach { Rename-Item -Path $_.oldfilename -Newname $_.newfilename }

Open in new window

You can add the -whatif parameter to the Rename-Item command if you'd like to test beforehand.
0
 

Author Comment

by:Brent Guttmann
ID: 41745794
and I would just paste this into power shell? Or would it be a .bat file?   I tried pasting it into powershell but nothing happened
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41745809
Here is a BAT script approach, save as a .BAT file, adjust SETs near the top, and test, test, test...

@eho off
setlocal

set BaseDir=\\server-bgs-bg02\My_Data\master\IMAGES\
set ListFile=c:\temp\list.txt

for /f "usebackq tokens=1-2 delims=," %%A in ("%ListFile%") do (
  ren "%BaseDir%\%%~A" "%%~B"
)

Open in new window

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Brent Guttmann
ID: 41745822
I tried this and it is saying that it cannot find the path specified (on the server). It is reading the csv correctly, which is located in the temp file.. just not finding the network paths..

Note - I do not have admin privileges - Not sure if that is effecting it or not... but, I can access the network in powershell... (obviously just not in regular command prompt..)

sorry, this is probably something basic - I just dont have any experience with it yet
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41745824
Sorry, extra backslash, should be like this:

@eho off
setlocal

set BaseDir=\\server-bgs-bg02\My_Data\master\IMAGES
set ListFile=c:\temp\list.txt

for /f "usebackq tokens=1-2 delims=," %%A in ("%ListFile%") do (
  ren "%BaseDir%\%%~A" "%%~B"
)

Open in new window

~bp
0
 

Author Comment

by:Brent Guttmann
ID: 41745839
Hey, see attached and below... what am I missing here?

@eho off
setlocal

set BaseDir="c:\temp\testing"
set ListFile="c:\temp\testing.csv"

for /f "usebackq tokens=1-2 delims=," %%A in ("%ListFile%") do (
  ren "%BaseDir%\%%~A" "%%~B"
)
example.pdf
testing.txt
0
 

Author Comment

by:Brent Guttmann
ID: 41745841
and i originally tried it with .txt and without quotes.. just fyi
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41745863
Don't use any quotes in the two SETs, as I showed.

~bp
0
 

Expert Comment

by:Brent Guttmann
ID: 41745865
Yes, I tried without them as well without any luck
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41745869
I tested here, this worked for me, what do you get when you run it?

@echo off
setlocal

set BaseDir=b:\ee\EE28961920\the files
set ListFile=b:\ee\EE28961920\list.txt

for /f "usebackq skip=1 tokens=1-2 delims=," %%A in ("%ListFile%") do (
  ren "%BaseDir%\%%~A" "%%~B"
)

Open in new window

~bp
0
 

Expert Comment

by:Brent Guttmann
ID: 41745872
it just opens the command prompt window for a second, then it closes ... without the files being renamed
0
 
LVL 40

Expert Comment

by:footech
ID: 41745877
Yes, mine works if you just paste it into PowerShell.  You could also save it as a .ps1 file and run that file from PS (but you should read up on the basics of running PowerShell scripts if you're not familiar with that).

Using your example input .CSV (which you posted as "testing.txt", though it is in a .csv format), the problem is with the new file names.  "/" is not an allowed character.  Replace all those with something else, like a dash "-", and it works fine.  Almost any problem will result in an error being displayed (as it did when I tried to use your sample .CSV).
1
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41745884
Try this for debugging and see what it displays.

@echo on
setlocal

set BaseDir=b:\ee\EE28961920\the files
set ListFile=b:\ee\EE28961920\list.txt

for /f "usebackq skip=1 tokens=1-2 delims=," %%A in ("%ListFile%") do (
  ren "%BaseDir%\%%~A" "%%~B"
)
pause

Open in new window

~bp
0
 

Author Comment

by:Brent Guttmann
ID: 41746296
That worked after I switched the / with the -.     That may have also prevented the .bat file from working correctly as well, BP.  

Thank you both for your help,  i have saved both codes for future ref.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question