使用批处理脚本自动上传WinSCP的SFTP文件时出错

发布于 2025-01-09 12:39:13 字数 750 浏览 0 评论 0 原文

我对 WinSCP 和批处理文件脚本都很陌生。如果这个问题非常基本,请原谅。 我正在尝试使用批处理文件将文件从本地文件夹上传到远程文件夹。文件名每周都会更改。我面临两个问题。

  1. 我在批处理文件中使用以下代码将文件上传到 WinSCP
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    open sftp://descartes:z*******[email protected]/ -hostkey="ssh-rsa 1024 ******=" ^
    "lcd  C:\Users\kajal.jain\Downloads\New folder" ^
    "cd /" ^
    "put Week 7 2022 Portal Data" ^
    "exit"

我收到以下错误。

Unknown command 'lcd  C:\Users\kajal.jain\Downloads\New folder'.
Same for cd 
  1. 由于要上传的文件名每周都会发生变化。如何在 Put 命令中自动执行此操作?

I am very new to both WinSCP and batch file scripting. So excuse me if this question is very basic.
I am trying to upload file from my local folder to remote folder using batch file. The name of the file changes every week. I am facing 2 problems.

  1. I am using below code in batch file to upload a file to WinSCP
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    open sftp://descartes:z*******[email protected]/ -hostkey="ssh-rsa 1024 ******=" ^
    "lcd  C:\Users\kajal.jain\Downloads\New folder" ^
    "cd /" ^
    "put Week 7 2022 Portal Data" ^
    "exit"

I am getting below error.

Unknown command 'lcd  C:\Users\kajal.jain\Downloads\New folder'.
Same for cd 
  1. As the name of file to be uploaded changes every week. How can I automate it in the Put command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-01-16 12:39:14
  1. 添加引号! 由于 WinSCP 需要其所有命令都用双引号括起空格 (请参阅此处的语法参考),我首先在 open ... 命令周围放置一些内容,然后在 lcd 中的其他路径周围放置一些内容> 和 put 命令。您可以在命令中使用两个双引号来表示文字"

  2. 传递文件名。您可以使用%1 引用此批处理文件的第一个命令行参数,因此,如果将下面的脚本保存为“upload.bat”...

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open sftp://descartes:z*******[email protected]/ -hostkey=""ssh-rsa 1024 ******=""" ^
    "lcd  ""C:\Users\kajal.jain\Downloads\New folder""" ^
    "cd /" ^
    "put %1" ^
    "exit"

...那么您可以从命令行键入

upload "Week 7 2022 Portal Data"

以让它上传该文件(引号也很重要)。

另一种选择——我将留给你研究——如果你根本不想给它一个文件名,你可以使用SET命令来探索,这样它就会做一些事情就像总是寻找并上传以今天日期命名的文件一样。 有关批处理参数和 SET 的更多信息

  1. Add quotes! Since WinSCP needs all its commands with spaces surrounded by double-quotes (see syntax reference here), I'd first put some around that open ... command, and then around other paths in your lcd and put commands. You can use two double-quotes to mean a literal " in your command.

  2. Pass in a filename. You can use %1 to refer to the first command-line argument to this batch file. So if you save the script below as "upload.bat"...

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open sftp://descartes:z*******[email protected]/ -hostkey=""ssh-rsa 1024 ******=""" ^
    "lcd  ""C:\Users\kajal.jain\Downloads\New folder""" ^
    "cd /" ^
    "put %1" ^
    "exit"

...then from the command line you could type

upload "Week 7 2022 Portal Data"

to have it upload that file (quotes are important there too).

Another option--and I'll leave it to you to research--if you didn't want to give it a filename at all, you could explore using the SET command so it'd do something like always looking for and uploading a file named with today's date. More info here on batch arguments and SET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文