如何在远程服务器上创建目录之前检查目录是否存在?

时间:2019-01-30 17:45:09

标签: batch-file ftp command-line-interface winscp

我正在使用File Transfer Protocol使用工具 WinSCP 将文件从一台服务器传输到另一台服务器,每种类型的文件都有一个单独的目录要存储,因此在传输之前,我必须创建该目录。但是,当第二次传输相同类型的文件时,它失败了,因为已经存在具有相同名称的目录。

两台服务器均使用 Windows操作系统,因此我正在创建一个批处理文件以传输文件,批处理代码如下:-

@echo off

set user=%1
set type=%2
set input=D:\path\to\files\%user%\%type%\*.*


"WinSCP.com" ^
  /log="D:\path\to\log\WinSCP.log" /ini=nul ^
  /command ^
    "open ftp://users:passwords@127.0.0.1/" ^
    "mkdir %user%" ^
    "cd %user%" ^
    "mkdir %type%" ^
    "cd %type%" ^
    "put %input%" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%

if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%

这是WinSCP.log:-

. 2019-01-29 14:40:28.938 Using FTP protocol.
. 2019-01-29 14:40:28.938 Doing startup conversation with host. 
> 2019-01-29 14:40:28.938 PWD
< 2019-01-29 14:40:28.938 257 "/" is the current directory.
. 2019-01-29 14:40:28.938 Getting current directory name.
. 2019-01-29 14:40:28.938 Startup conversation with host finished.
< 2019-01-29 14:40:28.939 Script: Active session: [1] users@127.0.0.1
> 2019-01-29 14:40:28.939 Script: mkdir compo-sucks-a-little
. 2019-01-29 14:40:28.939 Creating directory "compo-sucks-a-little".
> 2019-01-29 14:40:28.939 CWD /
< 2019-01-29 14:40:28.939 250 "/" is the current directory.
> 2019-01-29 14:40:28.939 MKD compo-sucks-a-little
< 2019-01-29 14:40:28.940 550 File exists.
. 2019-01-29 14:40:28.940 Asking user:
. 2019-01-29 14:40:28.940 Error creating folder 'compo-sucks-a-little'. ("File exists.")
< 2019-01-29 14:40:28.940 Script: Error creating folder 'compo-sucks-a-little'.
< 2019-01-29 14:40:28.940 Script: File exists.
. 2019-01-29 14:40:28.941 Script: Failed
. 2019-01-29 14:40:28.941 Script: Exit code: 1
. 2019-01-29 14:40:28.941 Disconnected from server

我想在创建目录之前检查目录是否已经存在。

0 个答案:

没有答案