2

I'm writing a batch script transferring each subfolder of a pre-defined folder to a remote server through ftp.

everything works fine, but in the for loop, it only execute once, transfer only one folder and then break out the loop.

I have a bunch of code in the for loop

FOR /f "delims= " %%B IN ('DIR "%backup_dir%" /A:D /B') DO ( here is my code ) 

when I use some short code in the for loop, like "echo", it runs correctly. But not when I put bunch of code with ftp command. Those ftp code can run correctly but only once, then it breaks out the loop.

below is piece of code go in the for loop

 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. ) 

any comment is welcome!

8
  • 2
    This ftp thing, it is not a batch file, is it?
    – Andriy M
    CommentedAug 5, 2011 at 5:05
  • 1
    as @Andriy pointed out, you might have some BAT file invocation in your code. Break your code in pieces (use CALL command) and debug one step at a time until you find the culprit.
    – PA.
    CommentedAug 5, 2011 at 6:50
  • thank you @PA, this is what I do right now. And the problem is located in the ftp code part. Without that part the loop runs fine, but that part itself can run correctly, I can not pin-point the problem.
    – yangqi
    CommentedAug 5, 2011 at 13:06
  • @Andriy M, yes it's a batch file. I used windows ftp command in it. I don't know if that's something to do with the problem.
    – yangqi
    CommentedAug 5, 2011 at 13:10
  • 2
    If you have a batch file and call a second one from it, the entire batch session terminates when the second batch terminates if you called it without the CALL command. Using CALL to invoke the second batch causes the control to return to the first batch file once the second one finished running. (And please don't ask me why, things have been that way since the time immemorial.) I guess you've now simply added CALL in front of every ftp command, haven't you? That should solve the issue.
    – Andriy M
    CommentedAug 5, 2011 at 13:18

1 Answer 1

4

The FOR command is cancelled if you execute any GOTO within it. To solve this problem, CALL a subroutine in the FOR command and do any process you want in the subroutine (including goto's).

0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.