Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XCOPY Command - dynamic destination file extension in for loop

Posted on 2013-06-22
10
Medium Priority
?
1,106 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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

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

~bp
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 57

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 57

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 57

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

705 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