Solved

XCOPY Command - increment destination file name in FOR loop

Posted on 2013-06-14
5
1,957 Views
Last Modified: 2013-06-14
I have a simple batch script that is looping through a directory of PDF files and copying the files to a destination folder that are identified in a list file.  Everything is working fine, however I want to rename the file that is copied to the destination.  If I am searching for 100 files, I want to destination files to be named in increment of search (i.e., 0001.pdf, 0002.pdf, 0003.pdf, etc.).

@echo off

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

for /f %%X in (%FIILELIST%) do call :COPY_FILES "%%X"


:COPY_FILES
for /r %FILESPATH% %%I in (%~1) do xcopy /qvs "%%I" "%DESTPATH%%%~pnxI"

Open in new window


Any help would be appreciated.
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 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 39248330
Something like this?

:COPY_FILES
set /a cnt+=1
for /r %FILESPATH% %%I in (%~1) do xcopy /qvs "%%I" "%DESTPATH%%%~pnxI_%cnt%"

Open in new window

0
 

Author Comment

by:zavikon
ID: 39248478
the cnt variable was successful, however the file name did not come out as desired.  It did append an _1 at the end (e.g., mypdf.pdf_1).  The desired result is 1.pdf
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39248714
That might take a bit more coding logic.  Forcing the extension okay for this?
:COPY_FILES
set /a cnt+=1
for /r %FILESPATH% %%I in (%~1) do xcopy /qvs "%%I" "%DESTPATH%%%~pnI_%cnt%.pdf"

Open in new window

0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39248718
You may need to use delayed expansion variable to get or set the counter in a for loop. Then use that counter value to set sequential file names:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION 

set /a mycounter=0
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

for /f %%X in (%FIILELIST%) do call :COPY_FILES "%%X"

:COPY_FILES
for /r %FILESPATH% %%I in (%~1) do (
set /a mycounter=!mycounter!+1
xcopy /qvs "%%I" "%DESTPATH%%%~pI!mycounter!%%~xI" 

)

Open in new window

0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39249324
Seems like this should work. Handles left fill with zeros. And simplifies the logic a bit.

@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

~bp
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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