批处理脚本从用户获取文件列表输入以提供给WinSCP

时间:2014-08-24 06:23:32

标签: unix batch-file batch-processing winscp

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

因为我是批处理脚本的新手,有人可以帮我解决这个问题吗?

传输单个文件的示例命令

  

调用C:\ Progra~2 \ WinSCP \ WinSCP.exe / console / timeout =“120”/ command“option batch continue”“option confirm off”“open sftp://%userid%:%passw%@ %host%“”获取%/ file / filename.txt%“”退出“

传输多个文件的示例命令

  

调用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“

2 个答案:

答案 0 :(得分:2)

在这种情况下,您根本不需要多次输入。 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毫无意义

答案 1 :(得分:1)

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

@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!     

在标签后面添加winscp命令行:begin

测试样本 -

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

相关问题