user2164919
user2164919

Reputation: 21

How to do while loop in windows batch

I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.

In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.

All I want is like this:

Directory A:

 1. A.txt 
 2. B.txt 
 3. list.txt (By line sequential with filename want to move) 
 4. process.bat

Directory B:

  1. None of files (Then move a file by order of line set in list.txt) OR
  2. A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)

Process.bat

@echo off

:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a

goto :eof

:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30 
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing   %name%.txt   now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer   %name%.txt   completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)

:eof

Please guide me to finish the script by using a while-loop method. Thanks

Upvotes: 0

Views: 8985

Answers (2)

Kali_Int
Kali_Int

Reputation: 1

This will copy as well as count the number of lines from your text file..

@ echo off 
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :

:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice

:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y  "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit

:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath

Upvotes: 0

user2164919
user2164919

Reputation: 21

I change some script sequence and it works now.

@echo off

:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a

goto :eof

:processline
set name=%*
if exist C:\Test2\*.txt (
        echo %date% %time% >> C:\Test2\Log.txt
        echo Previous job did not finished yet. >> C:\Test2\Log.txt
        Timeout /t 5
        echo.
        echo. >> C:\Test2\Log.txt
        goto :processline
         )
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing   %name%.txt   now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer   %name%.txt   completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)

:eof

Upvotes: 0

Related Questions