【问题标题】:Upload timestamped file with SFTP using WinSCP使用 WinSCP 使用 SFTP 上传带时间戳的文件
【发布时间】:2015-05-07 19:40:01
【问题描述】:

我正在制作一个批处理文件以将文件上传到 SFTP 服务器上的一个特定文件夹。当我使用任务计划程序运行文件时,它没有显示任何错误,但客户端的 SFTP 文件夹上没有上传任何内容。这是我的批处理脚本:

@echo off

REM Defines an exiting variable to be added onto each file giving it a time-stamp and exiting the current instance of WinSCP.
set d=%date:~-4,4%%date:~4,2%%date:~-7,2%
set d=%d: =_%

REM Creates the variable lines to shorten the SFTP file upload.
REM Example usage --> %transferStart%ourFile.txt %transferEnd%TheirFile.txt%e%
SET upload=winscp.exe /console /command "option confirm off" "open sftp://user:pass@example.com" "c:\apps\ftpfiles\name_%d%.txt" "put \\mainfolder\inbound\" "close"

【问题讨论】:

  • 你是否缺少结尾的右引号?
  • 不,在原始版本中我有结束代码,我也在这里添加。感谢您让我知道这一点。
  • 代码里有,还是没有结果。
  • 我还添加了“-hostkey="ssh-rsa 22 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx: xx" 在我的 sftp open 语句的末尾,但它没有改变任何东西
  • c:\apps:\ftpfiles\name_%d%.txtapps 之后有一个:,这是无效的。

标签: batch-file sftp winscp


【解决方案1】:

您的批处理文件中有很多问题。我至少可以识别:

  • 您设置了环境变量upload(出于我不明白的原因),但您没有使用它。而是直接调用 WinSCP:

    winscp.exe /console /command ...
    
  • 虽然不是完全错误,但使用批处理文件中的winscp.exe(GUI 应用程序)会使调试复杂化(它会启动一个单独的窗口或 WinSCP 进程)。请改用winscp.com (console application)。对于winscp.com/console 参数是多余的:

    winscp.com /command ...
    
  • 您使用文件名"c:\apps\ftpfiles\name_%d%.txt",就好像它是一个命令一样。如果要上传这个特定文件,请使用put command中的路径:

    "put c:\apps\ftpfiles\name_%d%.txt"
    
  • 您在open commandverify your server's hostkey 中缺少-hostkey 开关。

  • 您缺少exit command(而exit 之前的close 是多余的)。

  • date 环境变量获取时间戳是不可靠的(date 的值是特定于语言环境的)。使用 WinSCP %TIMESTAMP% construct:

    "put c:\apps\ftpfiles\name_%TIMESTAMP#yyyymmdd%.txt"
    

    %TIMESTAMP% 需要使用 WinSCP 5.7 或更高版本。)

  • 使用最新版本的 WinSCP,option confirm off is unnecessary

所有应用,您的批处理文件将包含一个命令:

@echo off

winscp.com /command ^
    "open sftp://user:pass@example.com -hostkey=""your-servers-hostkey""" ^
    "put c:\apps\ftpfiles\name_%%TIMESTAMP#yyyymmdd%%.txt" ^
    "exit"

【讨论】:

  • 感谢马丁的回复。我使用了您提供的时间戳,但是当我添加它时,任务调度程序给了我不正确的功能。 %TIMESTAMP#yyyymmdd%
  • 抱歉,这很模糊。 “任务调度程序给我的功能不正确”是什么意思?
  • 这是任务最后运行结果的消息:“无效函数”。我刚刚注意到我们正在使用 WinSCP 5.1.1,这个 %TIMESTAMP#yyyymmdd% 也在这个版本上工作吗?
  • 不,不是。这就是为什么我明确提到,您需要使用 WinSCP 5.7 或更高版本。虽然我不认为“无效功能”消息与此有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 2015-01-18
  • 2012-03-23
  • 2019-02-11
  • 2019-03-23
相关资源
最近更新 更多