?
Solved

XCOPY Command - increment destination file name in FOR loop

Posted on 2013-06-14
5
Medium Priority
?
1,996 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 56

Accepted Solution

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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