Solved

XCOPY Command - dynamic destination file extension in for loop

Posted on 2013-06-22
10
1,087 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
[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
  • 2
  • 2
  • +1
10 Comments
 
LVL 55

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 55

Expert Comment

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

~bp
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 55

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 55

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 55

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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