首页 > 解决方案 > 如何在远程服务器上创建目录之前检查目录是否存在?

问题描述

我正在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

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

标签: batch-fileftpcommand-line-interfacewinscp

解决方案


推荐阅读