You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WinSCP脚本自动化SFTP文件下载时目录切换异常的问题求助

使用WinSCP脚本自动化SFTP文件下载时目录切换异常的问题求助

大家好,我正在尝试用WinSCP实现从SFTP服务器自动下载文件的流程,但遇到了一个奇怪的问题——当连接成功建立后,脚本并没有切换到我指定的远程目录,反而试图切换到一个我从未在脚本里提及的路径C:\Users\darkovuj\Desktop,直接报了找不到文件的错误。有没有朋友能帮我排查下问题原因,给个解决方案呀?

下面是我遇到的错误输出、批处理脚本以及WinSCP命令脚本的内容,涉及账号密码的部分我已经打码了,但手动用这些凭据是可以正常连接的:

错误输出

batch           abort

confirm         off

Searching for host...

Connecting to host...

Authenticating...

Using username "bancaintesa".

Authenticating with pre-entered password.

Authenticated.

Starting the session...

Session started.

Error changing directory to 'C:\Users\darkovuj\Desktop'.

System Error.  Code: 2.

The system cannot find the file specified

Active session: [1] **********@195.178.44.158

0 File(s) copied

Press any key to continue . . .

批处理脚本(.bat文件)

@echo off

setlocal

REM === Starting SFTP Download Task ===

echo Starting SFTP Download Task

REM Set your SFTP server credentials

set SFTP_USER=********

set SFTP_PASS=*********

set SFTP_SERVER=195.178.44.158

set SFTP_PORT=22

REM Set the local folder paths

set DESTINATION_DIR=C:\olconnect\BI_SNE\input

echo getting date

REM Get the current date in YYYYMMDD format

for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
    set "YYYY=%%c"
    set "MM=%%a"
    set "DD=%%b"
)

set CURRENT_DATE=%YYYY%%MM%%DD%

echo date completed

REM Create the download directory with the current date

set DOWNLOAD_DIR=C:\olconnect\BI_SNE\backup\%CURRENT_DATE%

mkdir "%DOWNLOAD_DIR%"

REM Specify the path to the WinSCP commands file

set WINSCP_SCRIPT_PATH=C:\olconnect\BI_SNE\download_automation.txt

REM Run WinSCP to download files and delete them from the server

"C:\winscp514\winscp.com" /script="%WINSCP_SCRIPT_PATH%"

REM Copy downloaded files to the destination directory

xcopy /Y /I "%DOWNLOAD_DIR%\*.pdf" "%DESTINATION_DIR%"

echo completed copying...

REM Keep the window open to see the output

pause

endlocal

WinSCP命令脚本(download_automation.txt)

option batch abort
option confirm off
open sftp://%SFTP_USER%:%SFTP_PASS%@%SFTP_SERVER%:%SFTP_PORT% -hostkey="ssh-ed25519 255 *************************"
cd /SNETest
lcd %DOWNLOAD_DIR%
get *.pdf
rm *.pdf
exit

非常感谢大家的帮助!

备注:内容来源于stack exchange,提问作者Ivan Aksentijevic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 15:14:52