我正在尝试通过批处理文件从WinSCP下载文件。如果我在批处理文件中输入文件名,我可以下载文件。
但我需要动态输入文件名(即必须在运行时输入文件名)。
这是我的代码
cd\Users\Desktop\WinSCP
winscp.com /ini=null /script=C:\Users\Desktop\test.txt
open sftp://username:password@hostname/
$ ssh -o "StrictHostKeyChecking=yes" username@hostname
cd /log
get test.log C:\Users\Desktop\Downloading_logs\
此处Test.log
是我在批处理文件中提供的文件名。
请建议一种动态输入文件名的方法。
提前致谢。
答案 0 :(得分:1)
使用批次文件的参数
代码( script_with_arg.bat ):
@echo off
setlocal enableextensions
if "%~1" equ "" (
echo "Please provide a file name"
goto :eof
)
cd\Users\Desktop\WinSCP
winscp.com /ini=null /script=C:\Users\Desktop\test.txt
open sftp://username:password@hostname/
$ ssh -o "StrictHostKeyChecking=yes" username@hostname
cd /log
get "%~1" C:\Users\Desktop\Downloading_logs\
echo "Done"
备注强>:
"%~1"
。查看[SS64]: Command Line arguments (Parameters)了解详情if
子句是验证参数是否已提供,如果不是简单地显示错误消息并退出其他方式:
set /p
),如@ ramu246的评论(我错过了这个:) :) <强> @ EDIT0 强>:
我做了一些更改,下面是真正有用的版本(当然敏感数据已被更改)。
script.bat :
@echo off
if "%~1" equ "" (
echo "Please provide a file name"
goto :eof
)
winscp.com /ini=winscp_cfg.ini /script=winscp_script.txt /parameter "%~1"
echo "Done"
winscp_script.txt :
echo Received argument: "%1%"
open sftp://cfati:password@127.0.0.1:22001/
cd /tmp
get "%1%" .\"%1%"
exit
<强>输出强>:
e:\Work\Dev\StackOverflow\q047989047>where winscp c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.com c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.exe e:\Work\Dev\StackOverflow\q047989047>dir /b script.bat winscp_script.txt e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log" Received argument: "test with spaces.log" Searching for host... Connecting to host... Authenticating... Continue connecting to an unknown server and add its host key to a cache? The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is. The server's Ed25519 key details are: Algorithm: ssh-ed25519 256 SHA-256: M/iFTnSbi0k4VEcd8I75GiO7t6gza6RL99Pkh+bz4AQ= MD5: 8f:2f:f0:4a:ed:41:aa:e6:61:fa:5d:1f:f4:5b:c0:37 If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel. In scripting, you should use a -hostkey switch to configure the expected host key. (Y)es, (N)o, C(a)ncel (8 s), (C)opy Key, (P)aste key: Yes Using username "cfati". Authenticating with pre-entered password. Authenticated. Starting the session... Session started. Active session: [1] cfati@127.0.0.1 /tmp test with spaces.log | 6 B | 0.0 KB/s | binary | 100% "Done" e:\Work\Dev\StackOverflow\q047989047>dir /b script.bat test with spaces.log winscp_cfg.ini winscp_script.txt e:\Work\Dev\StackOverflow\q047989047>del /q "test with spaces.log" e:\Work\Dev\StackOverflow\q047989047>dir /b script.bat winscp_cfg.ini winscp_script.txt e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log" Received argument: "test with spaces.log" Searching for host... Connecting to host... Authenticating... Using username "cfati". Authenticating with pre-entered password. Authenticated. Starting the session... Session started. Active session: [1] cfati@127.0.0.1 /tmp test with spaces.log | 6 B | 0.0 KB/s | binary | 100% "Done" e:\Work\Dev\StackOverflow\q047989047>dir /b script.bat test with spaces.log winscp_cfg.ini winscp_script.txt
备注强>:
-hostkey
命令([WinSCP]: open)传递open
参数,但我没有成功(我也没有尝试过多)
(Y)es, (N)o, C(a)ncel....
),以便生成文件,下次它只是使用它