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
Solved

XCOPY Command - increment destination file name in FOR loop

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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