2

I can't try to reconnect with a different password after authentication failed. ERRORLEVEL seems to not be working in my code. I have no clue what's going wrong. The code is in batch file on Windows 10.

@echo off

echo use USERNAME> ftpcmd.dat
echo PASSWORD>> ftpcmd.dat


ftp -n -s:ftpcmd.dat files.000webhost.com

if %ERRORLEVEL% neq 0 (
   goto TEST
)

:TEST
echo user USERNAME> ftpcmd.dat
echo DIFFERENTPASSWORD>> ftpcmd.dat
echo prompt>>ftpcmd.dat
echo cd Test>>ftpcmd.dat
echo mput FILE PATH>> ftpcmd.dat

ftp -n -s:ftpcmd.dat FTPTHING
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ahm23
  • 338
  • 3
  • 16

1 Answers1

1

The Windows ftp.exe always returns 0, no matter what.

All you can possibly do, is to parse its output and look for errors. But that's pretty unreliable.

If you want to take this approach anyway, see


Though, you better use some better FTP client. Sooner or later you will run into troubles with the ftp.exe anyway, as it does not support an encryption, the passive mode or recursive transfers, among many other limitations.

If you are familiar with PowerShell, use the FtpWebRequest or WebClient from a Powershell script. There are numerous examples here, for example Upload files with FTP using PowerShell.


If not, use some 3rd party command-line FTP client.

For example with WinSCP FTP client, your batch file may look like:

@echo off
winscp.com /ini=nul /log=script.log /command ^
  "open ftp://USERNAME:PASSWORD@ftp.example.com/" ^
  "put C:\LOCAL\FILE /REMOTE/PATH/" ^
  "exit"

if ERRORLEVEL 1 (
  winscp.com /ini=nul /log=script2.log /command ^
    "open ftp://USERNAME:DIFFERENTPASSWORD@ftp.example.com/" ^
    "put C:\LOCAL\FILE /REMOTE/PATH/" ^
    "exit"
)

References:

WinSCP can also be used from PowerShell, if you want even more robust implementation.

(I'm the author of WinSCP)

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992