Solved

XCOPY Command - dynamic destination file extension in for loop

Posted on 2013-06-22
10
1,069 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 54

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 54

Expert Comment

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

~bp
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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
 
LVL 54

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 54

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 54

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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