如果尚不存在WinSCP和批处理文件,请在SFTP服务器上创建文件夹

时间:2019-07-18 04:17:24

标签: windows batch-file sftp winscp

我尝试创建文件夹(如果不存在),然后使用Windows批处理文件将文件复制到该文件夹​​,但cmd保持返回错误“未知命令'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中的示例。返回错误如下

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

1 个答案:

答案 0 :(得分:0)

没有简单的方法可以使用纯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 .NET assembly,例如从PowerShell script中使用。使用Session.FileExists method

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

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

请参阅converting WinSCP script to code based on .NET assembly指南。