Solved

Script for copying files listed in a txt file

Posted on 2014-01-09
5
300 Views
Last Modified: 2014-01-15
Hi All,

I'm in need of a script or batch file to copy files listed in a txt file. The files listed use many different paths but I want them copied to the same path, just on a different drive.

example of files2copy.txt:

X:\AW\FAR\P946.PDF
X:\AW\FAR\SupFar.PDF
X:\AW\FAR\TIP.DOC
X:\AW\PFP\ANALYSIS.MDB
X:\AW\PFP\COLLEGE.MDB
X:\EASY\DATA\A4G.DER
X:\EASY\DATA\ADRDATX.CER
X:\EASY\DATA\ADRDATX.DER

Again, I would need these files copied from source x:\<path> to the same path on destination  T:\<path> (both local drives) . These files already exist on destination T:\ <path> and should be overwritten from the ones copied from X drive.

Did I lose anybody??

Thanks in advance.
0
Comment
Question by:ITMan64
  • 3
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 39769907
Try this.  It should SHOW you the copy commands it would do on the screen.  Remove the word ECHO and it will run them instead.

hth

Steve

@echo off
set FileList=files2copy.txt
Set DestDir=T:

for /f "tokens=1* delims=:" %%A in ('type "%filelist%"') do (
  ECHO COPY /Y "%%~A:%%~B" "%DestDir%%%~B"
)

Open in new window

0
 

Author Comment

by:ITMan64
ID: 39770030
Steve,

I think you nailed it! I tried a sample of files and it looks like it would work as I needed.
Thanks so much! I will post back if I run into any problems.

Thanks Again,

-Rob
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39770293
No problem, can always add some logging and the like too if needed, i.e. if source or det file doesnt exist or the copy fails etc.

Steve
0
 

Author Comment

by:ITMan64
ID: 39781625
Steve,

Those additional functions would be a great help if possible. Thanks.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39781881
OK you can start doing more with things like this... if you want anything specific ask a question and link back to this one and someone will pickup.  I haven't tested below so apologies if any typos.

Steve

@echo off
set FileList=files2copy.txt
Set Log="c:\somedir\log.txt"
Set DestDir=T:

for /f "tokens=1* delims=:" %%A in ('type "%filelist%"') do (
  If exist "%DestDir%%%~B" echo "%DestDir%%%~B" already exists >> %LOG%
  If not exist "%%~A:%%~B" (
    echo "%%~A:%%~B" does not exist in source >> %log%
  ) ELSE (
    COPY /Y "%%~A:%%~B" "%DestDir%%%~B"
    if errorlevel 1 echo Error %errorlevel% copying "%%~A:%%~B" >> %log%
  )
)

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Learn about cloud computing and its benefits for small business owners.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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