Solved

XCOPY Command - increment destination file name in FOR loop

Posted on 2013-06-14
5
1,820 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
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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now