Solved

XCOPY Command - dynamic destination file extension in for loop

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

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

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

~bp
0
 

Author Closing Comment

by:zavikon
Comment Utility
Works like a charm!
0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
Thanks, Billprew. I feel I learn more when I try to answer questions than when I ask.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Welcome all around, glad to help.

~bp
0
 

Expert Comment

by:John Doe
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

11 Experts available now in Live!

Get 1:1 Help Now