1.

Solve : How to break out of 'for /f ...' loop after one iteration?

Answer»

I am pretty new to batch writing and so the issue I have is probably more a problem of wrong approach rather than a fix for the path I am on.

I need to copy the 'latest' files from many sub directories up to their parent dirs.
The structures look like:

SourceFiles/Cust1/Archive/files.txt
SourceFiles/Cust2/Archive/files.txt
etc.

The Archive folders are filled with lots of files, I occationally need to MOVE the latest archived file back 'up' one dir to the CUST# folder for re-processing.
I have one for loop that marches through the Cust folders (using a temp dirlist.txt) building a temp filelist.txt (sorted by mod date latest first)and an INNER loop that reads in the filelist and copies the files to the destination. My problem is that I don't know how to gracefully exit the inner loop after 1 iteration. I tried using a GOTO to get back to the outer for loop, but that seemed to break the outer loop and cause it to exit after only 1 iteration as well.

In it's current form this batch will bring up ALL the files from the archive and that is bad for me!
***************************
set prodSourceFolderPath="C:\temp\SourceFiles1"
set qaSourceFolderPath="C:\temp\SourceFiles2"

DIR %prodSourceFolderPath% /A:D /B /O:N >  C:\temp\dirlist.txt
setlocal enabledelayedexpansion
 FOR /F %%d IN (C:\temp\dirlist.txt) DO (
   DIR %prodSourceFolderPath%\%%d\Archive /w /B /o:-d >  C:\temp\filelist.txt
   FOR /F "delims=" %%a IN (C:\temp\filelist.txt) DO (
      echo %%a
      set latestFile=%%a
      C:\WINDOWS\System32\xcopy.exe /q /r /y /i %prodSourceFolderPath%\%%d\Archive\!latestFile! %qaSourceFolderPath%\%%d
   )
)I was able to solve this on my own but I decided that I would have to move the inner loop logic out to a 2nd .BAT file. That allowed me to use GOTO w/out disturbing the outer loop:


1st batch file:
*************
DIR %prodSourceFolderPath% /A:D /B /O:N >  C:\temp\dirlist.txt
setlocal enabledelayedexpansion
 FOR /F %%d IN (C:\temp\dirlist.txt) DO (
   DIR %prodSourceFolderPath%\%%d\Archive /w /B /o:-d >  C:\temp\filelist.txt
   CALL T:\2nd.bat C:\temp\filelist.txt %prodSourceFolderPath%\%%d\Archive %qaSourceFolderPath%\%%d\
)

2nd  batch file:
*************

echo off
if %1'==' echo which file? && goto :eof

set DONE==N
for /f "tokens=*" %%L in (%1) do call :1 %%L %2 %3
goto :eof

:1
if %DONE%==Y goto :eof
echo %1
echo %3
C:\WINDOWS\System32\xcopy.exe /q /r /y /i %2\%1 %3
set DONE=Y
goto :eof

:eof
:: DONEWhat o/s were you using?   I tried this on Windows Server 2003 and it does not work.  I've also tried BREAK and EXIT /B, but it ALWAYS coninues to read the rest of the file.

Thanks for any other tips.You're responding to a post that is two years old. You'll get better attention if you start a new thread.



Discussion

No Comment Found