我可以在 WinSCP 批处理脚本中引用批处理文件目录吗?
Can I reference the batch file directory in a WinSCP batch script?
我正在尝试使用批处理命令文件将文件上传到 FTP 服务器。我的代码使用 WinSCP,看起来像这样:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Program Files (x86)\WinSCP\WinSCP.log" /ini=nul ^
/command ^
"open ftp://user:pass@ftp.website.org/" ^
"lcd %cd%" ^
"cd /incoming/data" ^
"put %~dp0file.txt" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
日志显示如下:
> 2020-10-14 15:16:39.044 Script: put C:\Users\me\sharepoint - library folder\FTP\file.txt
. 2020-10-14 15:16:39.047 Copying 4 files/directories to remote directory "/incoming/data" - total size: 0
. 2020-10-14 15:16:39.047 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: folder\FTP\file.txt
. 2020-10-14 15:16:39.047 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2020-10-14 15:16:39.047 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
* 2020-10-14 15:16:39.048 (EOSError) System Error. Code: 2.
* 2020-10-14 15:16:39.048 The system cannot find the file specified
我期待 put %~dp0file.txt
给出我要上传的文件的完整路径,看起来它从日志中得到了正确的路径,但我不知道它为什么要发送 4文件或找不到指定文件的原因。我应该注意到路径 (folder/ftp) 实际上要长得多,完整路径是 102 个字符。路径包含空格和一个破折号。
您的路径包含空格,因此必须是 wrapped to double quotes:
"put ""%~dp0file.txt""" ^
lcd
也是如此。但由于使用 lcd
没有任何意义,如果您在 put
命令中使用绝对路径,则可以完全删除 lcd
。
我正在尝试使用批处理命令文件将文件上传到 FTP 服务器。我的代码使用 WinSCP,看起来像这样:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Program Files (x86)\WinSCP\WinSCP.log" /ini=nul ^
/command ^
"open ftp://user:pass@ftp.website.org/" ^
"lcd %cd%" ^
"cd /incoming/data" ^
"put %~dp0file.txt" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
日志显示如下:
> 2020-10-14 15:16:39.044 Script: put C:\Users\me\sharepoint - library folder\FTP\file.txt
. 2020-10-14 15:16:39.047 Copying 4 files/directories to remote directory "/incoming/data" - total size: 0
. 2020-10-14 15:16:39.047 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: folder\FTP\file.txt
. 2020-10-14 15:16:39.047 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2020-10-14 15:16:39.047 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
* 2020-10-14 15:16:39.048 (EOSError) System Error. Code: 2.
* 2020-10-14 15:16:39.048 The system cannot find the file specified
我期待 put %~dp0file.txt
给出我要上传的文件的完整路径,看起来它从日志中得到了正确的路径,但我不知道它为什么要发送 4文件或找不到指定文件的原因。我应该注意到路径 (folder/ftp) 实际上要长得多,完整路径是 102 个字符。路径包含空格和一个破折号。
您的路径包含空格,因此必须是 wrapped to double quotes:
"put ""%~dp0file.txt""" ^
lcd
也是如此。但由于使用 lcd
没有任何意义,如果您在 put
命令中使用绝对路径,则可以完全删除 lcd
。