如何在windows批处理中执行while循环?

时间:2022-12-13 16:22:38

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.

我编写了一个windows批处理脚本,根据我在一个名为list.txt的记事本文件中的列表来检查和移动文件到另一个目录。但是我不知道如何设置while循环。只有当条件满足时才跳出子程序。

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

在C编程中,我们可以通过直接设置一个while循环来这样写。但是在windows的批次中似乎是完全不同的。

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:

目录B:

  1. None of files (Then move a file by order of line set in list.txt) OR
  2. 没有文件(然后按照list.txt中的行集顺序移动文件)。
  3. A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
  4. 一个。txt(如果在目录中已经存在一个文本文件,则处理。蝙蝠会在A之前停下来。txt消失)

Process.bat

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

请引导我使用while循环方法完成脚本。谢谢

2 个解决方案

#1


0  

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

#2


0  

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

#1


0  

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

#2


0  

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