Solved

XCOPY Command - increment destination file name in FOR loop

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

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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