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
if
[not] exist
is a command of the Windows Command Prompt but obviously not of WinSCP... – aschipflcopy -filemask=*_P.zip
. You need to run WinSCP again withput
command. – Martin Prikryl