Solved

XCOPY Command - dynamic destination file extension in for loop

Posted on 2013-06-22
10
1,050 Views
Last Modified: 2016-08-16
Below is my block of code that iterates through my text file (FILELIST) and then searches for the file in a source directory (FILESPATH).  If found, it then copies the renamed file to the destination directory (DESTPATH).  

There are 2 more mods that I need to make to this script: (1) initially all of my source files were PDFs, so I hard coded the extension in the destination of the XCOPY command.  Source files can now also be JPEG files, so the rename needs to use the proper extension, and (2) the script prompts me each time it tries to copy a file to enter F or D, can i silently execute the F so the script will run start to finish without user input?

@echo off
setlocal EnableDelayedExpansion 

set FIILELIST=C:\export.txt
set FILESPATH=C:\TSU\www\ado\documents\Projects\140367C9-828B-4837-A1E4-1F6C98BDC06C\autos
set DESTPATH=C:\TSU\www\ado\documents_export

set Index=10000
for /f %%X in (%FIILELIST%) do (
  set /a Index+=1
  xcopy /qvs "%FILESPATH%\%%~X" "%DESTPATH%\!Index:~-4!.pdf" 
)

Open in new window


Thanks for your help.
0
Comment
Question by:zavikon
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39268104
Try this:

@echo off
setlocal EnableDelayedExpansion 

set FIILELIST=C:\export.txt
set FILESPATH=C:\TSU\www\ado\documents\Projects\140367C9-828B-4837-A1E4-1F6C98BDC06C\autos
set DESTPATH=C:\TSU\www\ado\documents_export

set Index=10000
for /f %%X in (%FIILELIST%) do (
  set /a Index+=1
  echo F|xcopy /qvs "%FILESPATH%\%%~X" "%DESTPATH%\!Index:~-4!%%~xX" 
)

Open in new window

~bp
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39268130
Please try replacing the xcopy line with the following. It uses an option of /y to suppress prompts, and also sets destination file extension to be same that of source:
xcopy /qvsy "%FILESPATH%\%%~X" "%DESTPATH%\!Index!%%~xX"

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39268153
The /Y will not suppress the "(F = file, D = directory)" propmpt.

~bp
0
 

Author Closing Comment

by:zavikon
ID: 39268422
Works like a charm!
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39268558
Thanks, Billprew. I feel I learn more when I try to answer questions than when I ask.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 39268725
Welcome all around, glad to help.

~bp
0
 

Expert Comment

by:John Doe
ID: 41757869
What if my file list has the whole path for the file, but the path has spaces.
The system seem to only save the path up to the first space.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41758000
If you mean the path of the list file has spaces in it, then try this adjustment:

@echo off
setlocal EnableDelayedExpansion 

set FIILELIST=C:\export.txt
set FILESPATH=C:\TSU\www\ado\documents\Projects\140367C9-828B-4837-A1E4-1F6C98BDC06C\autos
set DESTPATH=C:\TSU\www\ado\documents_export

set Index=10000
for /f "usebackq" %%X in ("%FIILELIST%") do (
  set /a Index+=1
  echo F|xcopy /qvs "%FILESPATH%\%%~X" "%DESTPATH%\!Index:~-4!%%~xX" 
)

Open in new window

~bp
0
 

Expert Comment

by:John Doe
ID: 41758009
It still does not treat spaces well. I have attached a sample of the file list and the error log.
Thanks for all the help.
Capture.PNG
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41758028
Okay, got it now, try this adjustment for that.

@echo off
setlocal EnableDelayedExpansion 

set FIILELIST=C:\export.txt
set FILESPATH=C:\TSU\www\ado\documents\Projects\140367C9-828B-4837-A1E4-1F6C98BDC06C\autos
set DESTPATH=C:\TSU\www\ado\documents_export

set Index=10000
for /f "usebackq tokens=*" %%X in ("%FIILELIST%") do (
  set /a Index+=1
  echo F|xcopy /qvs "%FILESPATH%\%%~X" "%DESTPATH%\!Index:~-4!%%~xX" 
)

Open in new window

~bp
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now