我正在编写一个批处理脚本,通过ftp将预定义文件夹的每个子文件夹传输到远程服务器。
一切正常,但在for循环中,它只执行一次,只传输一个文件夹,然后打破循环。
我在for循环中有一堆代码
FOR /f "delims= " %%B IN ('DIR "%backup_dir%" /A:D /B') DO (
here is my code
)
当我在for循环中使用一些短代码时,如“echo”,它运行正常。但是当我用ftp命令放入一堆代码时。那些ftp代码可以正常运行但只运行一次,然后它就会中断循环。
下面是for循环中的一段代码
REM -- Send most recent daily backups offsite
IF !daily_flag! EQU YES (
ECHO -- Starting Daily backups for !current_folder!
ECHO.
CD 1DAYS_OLD\
REM -- Start entries for ftp commands file
ECHO user %login% %pass% > %ftp_cmds1%
ECHO cd backup/files/!current_folder!/1DAYS_OLD >> %ftp_cmds1%
ECHO quit >> %ftp_cmds1%
SET go_flag=NO
SET /a ftp_cntr=0
REM -- Loop to see if offsite backup server is alive
:CHECK_FTP1
IF !go_flag! EQU NO (
ECHO %ftp_cmds1% > %ftp_dbg1%
ftp -ind -s:%ftp_cmds1% %machine_ip% > %ftp_dbg1%
REM -- Check ftp debug file to decide what to do
FIND /i "Not connected" %ftp_dbg1%
IF !ERRORLEVEL! EQU 0 (
ECHO *** Unable to connect to offsite backup machine: %machine_ip% at %DATE%
ECHO !ftp_cntr!
IF !ftp_cntr! GTR 10 (
ECHO *** Unable to connect to offsite backup machine: %machine_ip% for 10 minutes from %machine%
ECHO ----- Start ftp debug file
TYPE %ftp_dbg1%
ECHO ----- End ftp debug file
DEL /f /q %lock_file%
DEL /f /q %ftp_cmds1%
DEL /f /q %ftp_dbg1%
EXIT 2
) ELSE (
TIMEOUT /t 60
SET /a ftp_cntr+= 1
)
) ELSE (
SET go_flag=YES
ECHO Offsite backup machine is alive, proceeding with ftp
)
GOTO :CHECK_FTP1
)
FIND /i "No such file or directory" %ftp_dbg1%
IF !ERRORLEVEL! EQU 0 (
ECHO * Directory backup/files/!current_folder! does not exist on offsite backup machine.
SET go_flag=MKDIR
)
REM -- IF %go_flag% == MKDIR create remote directory structure
IF !go_flag! EQU MKDIR (
REM -- Start entries for ftp commands file
ECHO user %login% %pass% > %ftp_cmds2%
ECHO cd ~/backup/files/ >> %ftp_cmds2%
ECHO mkdir !current_folder! >> %ftp_cmds2%
ECHO cd !current_folder! >> %ftp_cmds2%
ECHO mkdir 1DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 2DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 3DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 4DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 5DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 6DAYS_OLD >> %ftp_cmds2%
ECHO mkdir 7DAYS_OLD >> %ftp_cmds2%
ECHO ls -lart >> %ftp_cmds2%
ECHO quit >> %ftp_cmds2%
ftp -ind -s:%ftp_cmds2% %machine_ip% > %ftp_dbg2%
ECHO + Directory structure backup/files/!current_folder! created on offsite backup machine: %machine_ip%
)
REM -- FTP to offsite backup machine and begin FTP transfer
ECHO user %login% %pass% > %ftp_cmds3%
ECHO binary >> %ftp_cmds3%
ECHO cd ~/backup/files/!current_folder!/7DAYS_OLD >> %ftp_cmds3%
ECHO mdelete * %ftp_cmds3%
ECHO cd .. >> %ftp_cmds3%
ECHO rmdir 7DAYS_OLD >> %ftp_cmds3%
ECHO rename 6DAYS_OLD 7DAYS_OLD >> %ftp_cmds3%
ECHO rename 5DAYS_OLD 6DAYS_OLD >> %ftp_cmds3%
ECHO rename 4DAYS_OLD 5DAYS_OLD >> %ftp_cmds3%
ECHO rename 3DAYS_OLD 4DAYS_OLD >> %ftp_cmds3%
ECHO rename 2DAYS_OLD 3DAYS_OLD >> %ftp_cmds3%
ECHO rename 1DAYS_OLD 2DAYS_OLD >> %ftp_cmds3%
ECHO mkdir 1DAYS_OLD >> %ftp_cmds3%
ECHO cd 1DAYS_OLD >> %ftp_cmds3%
ECHO mput * >> %ftp_cmds3%
ECHO quit >> %ftp_cmds3%
ECHO ---- Starting Daily FTP at: %DATE%
ftp -ind -s:%ftp_cmds3% %machine_ip% > %ftp_dbg3%
REM -- Go up a directory in case you have to next process weekly backups
CD..
CD
ECHO ---- Finished Daily backups at: %DATE%
ECHO.
ECHO.
)
欢迎任何评论!
答案 0 :(得分:3)
如果在其中执行任何GOTO,则取消FOR命令。要解决此问题,请在FOR命令中调用子例程,并在子例程中执行所需的任何处理(包括goto)。