afifi
afifi

Reputation: 85

Create folder on SFTP server with WinSCP and batch file when it does not exist already

I try to create folder if not exist and copy file to the folder using Windows batch file but cmd keep return error "Unknown command 'IF'."

File : Cycle2201_P.zip

Folder should be created if not exist : Jan

File should be transfer to remote server created folder : Cycle2201_P.zip

Result in Remote server : /New/Jan/Cycle2201_P.zip

Script :

@echo off
echo %date%

:AGAIN

Set filename=D:\Users\AALADELA\Desktop\pbilsr01\*.zip*
For %%A in ("%filename%") do (
    Set Folder=%%~dpA
    Set Name=%%~nxA
)

echo Folder name : %Folder%
echo Filename : %NAME%

set month=%Name:~7,2%

if %month%==01 set currentmonthfolder=Jan
if %month%==02 set currentmonthfolder=Feb
if %month%==03 set currentmonthfolder=Mar
if %month%==04 set currentmonthfolder=Apr
if %month%==05 set currentmonthfolder=May
if %month%==06 set currentmonthfolder=Jun
if %month%==07 set currentmonthfolder=Jul
if %month%==08 set currentmonthfolder=Aug
if %month%==09 set currentmonthfolder=Sep
if %month%==10 set currentmonthfolder=Oct
if %month%==11 set currentmonthfolder=Nov
if %month%==12 set currentmonthfolder=Dec


:COPY
@echo off
echo.
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open sftp://[email protected]/ -hostkey=""ssh4a"" -privatekey=""E:\SFTP CONNECTION\astrobcp.ppk"" -timeout=1800" ^
    "IF NOT EXIST "/New/%currentmonthfolder%/"( mkdir "/New/%currentmonthfolder%/" ) " ^
    "put -filemask=*_P.zip "D:\Users\AALADELA\Desktop\pbilsr01\%NAME%" "/New/%currentmonthfolder%" " ^
    "exit"

Another attempt: I modified the Copy as the example in Call WinSCP to check if files on a host exist or not using a batch file. Return error as below

Can't get attributes of file '/New/Jan'.
No such file or directory.
Error code: 2
Error message from server: File not found: /New/Jan
Error or file /New/Jan not exists
The syntax of the command is incorrect.
The system cannot find the file specified
:COPY
@echo off
set REMOTE_PATH=/New/%currentmonthfolder%
"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
    "open sftp://[email protected]/ -hostkey=""ssh4a"" -privatekey=""E:\SFTP CONNECTION\astrobcp.ppk"" -timeout=1800" ^
    "stat %REMOTE_PATH%" ^
    "exit"

if %ERRORLEVEL% neq 0 goto error

echo File %REMOTE_PATH% exists
copy -filemask=*_P.zip "D:\Users\AALADELA\Desktop\pbilsr01\%NAME%" "%REMOTE_PATH%"
exit /b 0

:error
echo Error or file %REMOTE_PATH% not exists
mkdir "%REMOTE_PATH%"
copy -filemask=*_P.zip "D:\Users\AALADELA\Desktop\pbilsr01\%NAME%" "%REMOTE_PATH%"
exit /b 1

Upvotes: 1

Views: 5086

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

There's no straightforward way to implement this with pure WinSCP scripting.

For a clean solution, you would have to run at least twice. First to check for an existence of the directory. And second time to optionally create the folder and upload the files. For a more readable solution, you would have to run WinSCP even three times, like:

@echo off

set WINSCP= ^
    "C:\Program Files (x86)\WinSCP\WinSCP.com" /log="WinSCP.log" /ini=nul /command ^
    "open sftp://[email protected]/ -hostkey=""ssh4a"" -privatekey=""E:\SFTP CONNECTION\astrobcp.ppk"" -timeout=1800" ^

set REMOTE_PATH=/New/%currentmonthfolder%
echo Checking for an existence of a folder...
%WINSCP% "stat %REMOTE_PATH%" "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
    echo Folder %REMOTE_PATH% exists already
) else (
    echo Folder %REMOTE_PATH% does not exist yet, creating...
    %WINSCP% "mkdir %REMOTE_PATH%" "exit"
)

echo Uploading...
%WINSCP% "put -filemask=*_P.zip ""D:\Users\AALADELA\Desktop\pbilsr01\%NAME%"" %REMOTE_PATH%/" "exit"

echo Done

A way easier is to simply try to create the folder and ignore errors (when folder exists already) while doing so. Use option batch continue to ignore the errors.

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="WinSCP.log" /ini=nul 
  /command ^
    "open sftp://[email protected]/ -hostkey=""ssh4a"" -privatekey=""E:\SFTP CONNECTION\astrobcp.ppk"" -timeout=1800" ^
    "option batch continue" ^
    "mkdir /New/%currentmonthfolder%" ^
    "option batch abort" ^
    "put -filemask=*_P.zip "D:\Users\AALADELA\Desktop\pbilsr01\%NAME%" "/New/%currentmonthfolder%" " ^
    "exit"

See also:


For easy and clear code, prefer using WinSCP .NET assembly, for example from a PowerShell script. Use Session.FileExists method:

if (!$session.FileExists($remotePath))
{
    $session.CreateDirectory($remotePath)
}

$session.PutFiles(...).Check()

See a guide to converting WinSCP script to code based on .NET assembly.

Upvotes: 2

Related Questions