We help IT Professionals succeed at work.

Need a Script to copy and rename files based on an input file

DebbieFost
DebbieFost asked
on
I have 20,000 documents scattered across multiple folders that I need to have copied to a new location and renamed with a file extension. I would like to have a script that can read an Input file and perform the function accordingly.

Example of original files:
F:\000100\203010\G3.FB
F:\090320\134010\X7.6P
F:\012050\761010\5V.J1

Example of comma delimited input file contents (called C:\Input.csv)
PDF,F:\000100\203010\G3.FB,Invoice from ABC
DOCX,F:\090320\134010\X7.6P, Letter to Susie
HTM,F:\012050\761010\5V.J1,Bank Rec Report

I would like to have a script that will read the input file and copy & rename the files to the G" drive in the following format:

G:\000100\203010\G3.FB.PDF
G:\090320\134010\X7.6P.DOCX
G:\012050\761010\5V.J1.HTM

We are essentially adding the appropriate file extension to each file (as referenced in the Input.csv file), and copying the document to the G: Drive
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Here's a small BAT script that should do the job.

@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A
)

Open in new window

~bp

Commented:
@Bill: Forgot last " on line 5

Cheers,
Rene
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
So it should be this then:
@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A"
)

Open in new window

~bp

Commented:
@Debbie: I don't expect points for my mini-tiny-wanabe-contribution, as Bill did all the work!

Cheers,
Rene
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Only thing I'd say is... if the directory structure does not exist on the dest drive already then copy won't create it will it Bill?

It may be worth adding between the for and copy commands:

md "%destdrive%%%~pB"

(I think that's right!)

Steve
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Good point Steve, this would address that possibility.

@echo off
set DestDrive=G:
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  if not exist "%DestDrive%%%~pB" md "%DestDrive%%%~pB"
  copy "%%~B" "%DestDrive%%%~pnxB.%%~A"
)

Open in new window

~bp

Author

Commented:
Thanks bill (and Rene and Steve) - will give it a whirl today!

Author

Commented:
Ok....thinsk just got a lot simpler.....we no longer need to copy the documents to a new location, we just want to rename them in place!

Can you adjust this script so it just adds the extension to the filename in it's current location? (i.e. F:\,<folder>\,<filename>.<ext>
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
I think this should take care of that.  Make sure you test on a copy of the files first just in case the rename doesn't do what we expect.

@echo off
set ListFile=C:\input.csv
for /F "usebackq tokens=1,2 delims=," %%A in ("%ListFile%") do (
  ren "%%~B" "%%~nxB.%%~A"
)

Open in new window

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.