【问题标题】:Batch script to take file list input from user to feed to WinSCP批处理脚本从用户获取文件列表输入以馈送到 WinSCP
【发布时间】:2014-10-17 14:37:50
【问题描述】:

我的需求是创建一个 ftp 批处理脚本,通过 WinSCP 命令行将文件从 Unix 传输到 Windows。因此,我将文件名传递给脚本,并将文件从 Unix 传输到 Windows。但是,当我想传输多个文件时,这里的挑战是从用户那里获取所有文件名并运行 WinSCP 命令来获取所有文件。如何循环输入不同文件名并构造相同的 WinSCP 命令?

由于我是批处理脚本的新手,有人可以帮助我使用这种方法吗?

传输单个文件的示例命令

call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw%@ %host%" "get %/file/filename.txt%" "exit"

传输多个文件的示例命令

call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw%@ %host%" "get %/file/filename.txt%" "get %/file/filename2.txt%" "get %/file/filename3.txt%" "exit"

【问题讨论】:

    标签: unix batch-file batch-processing winscp


    【解决方案1】:

    在这种情况下,您根本不必接受多个输入。 WinSCP 命令get 可以将多个源文件作为参数。您只需使用目标路径作为最后一个参数。使用.\ 下载到当前工作目录(什么是默认值,仅使用单个参数时,就像在您的示例中一样)。

    因此您可以只提示用户一次输入要下载的文件的空格分隔列表

    set /p "list=Enter space separated list of files to download: "
    winscp.com /command ^
        "option batch continue" ^
        "option confirm off" ^
        "open sftp://%userid%:%passw%@%host%" ^ 
        "get %list% .\" ^
        "exit"
    

    旁注:

    • 从批处理文件运行 WinSCP 时,请使用 the winscp.com instead of the winscp.exe 以避免为 WinSCP 打开额外的控制台窗口。

    • 命令行参数/timeout=120 不适用于在脚本中使用open 命令打开的会话。使用open command-timeout=120开关:

        open sftp://%userid%:%passw%@%host% -timeout=120
      
    • 使用call命令运行WinSCP没有意义

    【讨论】:

      【解决方案2】:

      我假设您将在控制台中手动提示用户输入。这将反复提示用户输入,直到看到一个点.

      @echo off       
      setlocal enabledelayedexpansion     
      :prompt     
      set /p "input=Enter the filename:"      
      if "!input!"=="." (     
          goto :begin ) else (    
              set input_all=!input_old! "!input!" 
              set input_old=!input_all!
              goto :prompt
          )   
      :begin      
      echo consolidated_input=!input_all!     
      

      在标签 :begin 之后添加您的 winscp 命令行

      测试样本-

      D:\Scripts>draft.bat
      Enter the filename:c:\sample\test1.txt
      Enter the filename:c:\sample\test2.log
      Enter the filename:c:\sample\test3.ini
      Enter the filename:.
      consolidated_input= "c:\sample\test1.txt"  "c:\sample\test2.log"  "c:\sample\test3.ini"
      

      干杯,G

      【讨论】:

      • 谢谢gbabu。这对我有用。但我面临一个问题。在运行时,winscp 命令不采用合并的输入值。它运行为 -- call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw %@%host%" ""
      • 可能是因为winscp需要get %/file/filename.txt%形式的输入。上面的脚本没有在输入中添加单词get。如果您想按照示例中的说明添加get,请将get input_all=!input_old! "!input!" 行替换为get input_all=!input_old! "get !input!" 并尝试。
      • 我已经尝试过仅使用该选项。在开始标签中:set /psolidated_input=!input_all! @echo ON echo %consolidated_input% cd C:\Users\ebavkar\Documents\Projects\VMware\Scripts call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "选项确认关闭" "打开 sftp://%userin%:%pasw%@%host%" "%consolidated_input%"
      • 您的示例未显示您如何添加 get 部分。假设你有这个权利......你还需要删除你在%consolidated_input% 周围的双引号。并且您需要确保在循环中的每个 get filename 对周围添加双引号。这样生成的语法就如您在问题中所拥有的那样,即"get /xxx/filename1" "get /xxx/filename2"
      猜你喜欢
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      相关资源
      最近更新 更多