Home > other >  How to execute further batch file commands after running "ftp" commands contains in the ba
How to execute further batch file commands after running "ftp" commands contains in the ba

Time:10-20

I have a .bat document where I do some operations with FTP. I want to end the FTP connection and get to finish with the terminal on the desired directory.

I'm trying this, but when it gets to "quit" it stops.

cd %USERPROFILE%\Foilder\project\angular
call ng build

@FTP -i -s:"%~f0"&GOTO:EOF
open connection
user
password

cd httpdocs/project

mdelete *.woff

quit

cd %USERPROFILE%\Foilder\Subfolder

It gives me this message right after doing the "quit" and never run the next line.

221 Goodbye.

Ideal scenario: run this last cd command and have the terminal ready on the folder that I want to run it. Thing is that I have to do some uploads to two different FTPs and it would be great to have the console finishing in the folder where I run "dev.bat" to get it to work

Is it possible to do?

Thanks in advance

CodePudding user response:

Your script uses a known hack that allows you to specify ftp commands directly in the batch file.

The %~f0 is replaced with the path to the batch file itself. So the ftp -s:%~f0 runs ftp and tells it to use the batch file itself as ftp script file. You have probably noticed that it results in couple of errors, as ftp fails on the first few lines of the batch file, which are not valid ftp commands (the cd ..., call ... and ftp ...).

Equivalently, the batch file would try to run all commands after ftp ... once ftp finishes, also failing, as those are not valid batch file commands. To avoid that, the hack uses GOTO:EOF to skip the rest of the batch file (EOF=eof of the file).

While you actually want to execute some commands after the ftp. At least the cd command. So do not skip the rest of the batch file. Skip only the ftp commands:

ftp -i -s:"%~f0"&goto AFTER_FTP
(ftp commands)
quit

:AFTER_FTP
cd %USERPROFILE%\Foilder\Subfolder

Note the @ before ftp. That (and the GOTO:EOF) are clear signs, that the script you based your batch file on, was designed to start with ftp on the very first line and contain nothing else, but the ftp commands. You have deviated from that.


Alternatively, use some more capable FTP client that allows specifying the commands on its command-line without hacks.

For example with my WinSCP FTP client, you can do:

cd %USERPROFILE%\Foilder\project\angular
call ng build

winscp.com /ini=nul /command ^
    "open ftp://user:password@connection/" ^
    "cd httpdocs/project" ^
    "rm *.woff" ^
    "exit"

cd %USERPROFILE%\Foilder\Subfolder

There's a guide for converting Windows ftp script to WinSCP script.

  • Related