使用 WinSCP 在 SFTP 服务器上创建文件夹,当它不存在时创建批处理文件
Create folder on SFTP server with WinSCP and batch file when it does not exist already
我尝试创建文件夹(如果不存在)并使用 Windows 批处理文件将文件复制到该文件夹,但 cmd 保持 return 错误 "Unknown command 'IF'."
文件:Cycle2201_P.zip
如果文件夹不存在则应创建:Jan
文件应该传输到远程服务器创建的文件夹:Cycle2201_P.zip
远程服务器中的结果:/New/Jan/Cycle2201_P.zip
脚本:
@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://astrobcp@sftpdr.canon-mc.com.my/ -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"
另一种尝试:我修改了Copy
作为Call WinSCP to check if files on a host exist or not using a batch file中的例子。 Return 错误如下
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://astrobcp@sftpdr.canon-mc.com.my/ -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
没有直接的方法可以用纯 WinSCP scripting 来实现它。
要获得干净的解决方案,您必须至少 运行 两次。首先检查目录是否存在。第二次可选择创建文件夹并上传文件。为了获得更具可读性的解决方案,您甚至需要 运行 WinSCP 三次,例如:
@echo off
set WINSCP= ^
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log="WinSCP.log" /ini=nul /command ^
"open sftp://astrobcp@sftpdr.canon-mc.com.my/ -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
一种更简单的方法是尝试创建文件夹并忽略错误(当文件夹已经存在时)。使用 option batch continue
忽略错误。
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="WinSCP.log" /ini=nul
/command ^
"open sftp://astrobcp@sftpdr.canon-mc.com.my/ -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"
另请参阅:
- WinSCP 文章 Checking file existence。
- 和 Stack Overflow 问题
为了简单明了的代码,更喜欢使用WinSCP .NET assembly, for example from a PowerShell script. Use Session.FileExists
method:
if (!$session.FileExists($remotePath))
{
$session.CreateDirectory($remotePath)
}
$session.PutFiles(...).Check()
查看 converting WinSCP script to code based on .NET assembly 的指南。
我尝试创建文件夹(如果不存在)并使用 Windows 批处理文件将文件复制到该文件夹,但 cmd 保持 return 错误 "Unknown command 'IF'."
文件:Cycle2201_P.zip
如果文件夹不存在则应创建:Jan
文件应该传输到远程服务器创建的文件夹:Cycle2201_P.zip
远程服务器中的结果:/New/Jan/Cycle2201_P.zip
脚本:
@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://astrobcp@sftpdr.canon-mc.com.my/ -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"
另一种尝试:我修改了Copy
作为Call WinSCP to check if files on a host exist or not using a batch file中的例子。 Return 错误如下
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://astrobcp@sftpdr.canon-mc.com.my/ -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
没有直接的方法可以用纯 WinSCP scripting 来实现它。
要获得干净的解决方案,您必须至少 运行 两次。首先检查目录是否存在。第二次可选择创建文件夹并上传文件。为了获得更具可读性的解决方案,您甚至需要 运行 WinSCP 三次,例如:
@echo off
set WINSCP= ^
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log="WinSCP.log" /ini=nul /command ^
"open sftp://astrobcp@sftpdr.canon-mc.com.my/ -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
一种更简单的方法是尝试创建文件夹并忽略错误(当文件夹已经存在时)。使用 option batch continue
忽略错误。
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="WinSCP.log" /ini=nul
/command ^
"open sftp://astrobcp@sftpdr.canon-mc.com.my/ -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"
另请参阅:
- WinSCP 文章 Checking file existence。
- 和 Stack Overflow 问题
为了简单明了的代码,更喜欢使用WinSCP .NET assembly, for example from a PowerShell script. Use Session.FileExists
method:
if (!$session.FileExists($remotePath))
{
$session.CreateDirectory($remotePath)
}
$session.PutFiles(...).Check()
查看 converting WinSCP script to code based on .NET assembly 的指南。