Mohan Lalwani
Mohan Lalwani

Reputation: 57

FTP check if directory exists on server or not

I am running backup.bat script and used below command:

echo cd /public_html/GURUITSOFTWARESBACKUP>> ftpcmd.dat

I am getting reply 550

Can't change directory to /public_html/GURUITSOFTWARESBACKUP: No such file or directory

I need to change path to other directory in case if above path doesn't exists.

Upvotes: 1

Views: 405

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202272

This is hardly doable with a batch file and the Windows built-in ftp.

You better use some 3rd party FTP client. But even then, it is not easy to do any conditional processing in a batch file, unless you find an FTP client with a real scripting built-in (I'm not aware of any). Using a different scripting language than the batch file would be better option.

Anyway, if you use WinSCP scripting, you can do something like this:

@echo off
 
set SESSION=ftp://username:[email protected]/
set REMOTE_PATH=/path/one
set REMOTE_PATH2=/path/two
winscp.com /command ^
    "open %SESSION%" ^
    "stat %REMOTE_PATH%" ^
    "exit"
 
echo %ERRORLEVEL%
if %ERRORLEVEL% equ 0 (
    echo Directory %REMOTE_PATH% exists, will use it
) else (
    echo Error or directory %REMOTE_PATH% not exists, will use %REMOTE_PATH2%
    set REMOTE_PATH=%REMOTE_PATH2%
)

echo Doing something with %REMOTE_PATH%
winscp.com /command ^
    "open %SESSION%" ^
    "cd %REMOTE_PATH%" ^
    "ls" ^
    "exit"

This is based on WinSCP article Checking file existence.


The article also shows how to check for the [file/directory] existence using WinSCP .NET assembly from a PowerShell script. That's what I would recommend.

(I'm the author of WinSCP)

Upvotes: 1

Related Questions