是否可以将变量传递到 Windows FTP 脚本文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5170627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to pass a variable into a Windows FTP script file?
提问by SuperNES
I have a batch script that dynamically creates some files and generates four files with somewhat random filenames based on the time, date, etc. It then needs to upload that file to a server via FTP.
我有一个批处理脚本,它动态创建一些文件并根据时间、日期等生成四个文件名随机的文件。然后它需要通过 FTP 将该文件上传到服务器。
As of right now, my .bat file has a line like "ftp -s:ftp.txt". Ftp.txt contains some fairly straightforward FTP script stuff: something like this--
截至目前,我的 .bat 文件有一行类似于“ftp -s:ftp.txt”。Ftp.txt 包含一些相当简单的 FTP 脚本内容:像这样——
open ftp.myserver.com
username
password
put filename1.dat
put filename2.dat
put filename3.dat
put filename4.dat
What I'd like to do is pass in the filenames that need to be uploaded and then replace the "put filename1.dat" with "put %file1%"--where %file1% is the filename variable being passed in.
我想要做的是传入需要上传的文件名,然后将“put filename1.dat”替换为“put %file1%”——其中 %file1% 是传入的文件名变量。
Is this possible? Anybody know how to do it? Or is my whole approach wrong?
这可能吗?有人知道怎么做吗?或者我的整个方法是错误的?
回答by Sébastien Nussbaumer
You could generate the ftp.txt file on the fly with your bat file. Simply do something like :
您可以使用 bat 文件即时生成 ftp.txt 文件。只需执行以下操作:
echo ftp.myserver.com>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo put filename1.dat>>ftp.txt
echo put filename2.dat>>ftp.txt
echo put filename3.dat>>ftp.txt
echo put filename4.dat>>ftp.txt
ftp -s:ftp.txt
Of course now that you are in the bat file you can use environment variables and other stuff in place of "filenameX.dat"
当然,现在您在 bat 文件中,您可以使用环境变量和其他内容代替“filenameX.dat”
For example :
例如 :
echo put %file1% >>ftp.txt
回答by Randy Walker
To update the answer, here's an updated batch file. Note the exclusion of the space after the username and password (as it passes a space to the ftp site). This will also utilize an argument passed to the batch file.
要更新答案,这里有一个更新的批处理文件。注意用户名和密码后面的空格被排除在外(因为它向 ftp 站点传递了一个空格)。这也将利用传递给批处理文件的参数。
echo open ftp.myserver.com>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo put %1>>ftp.txt
echo quit>>ftp.txt
ftp -s:ftp.txt
回答by Marc B
I don't think the command line FTP client in windows is smart enough to do environment/shell variable interpolation on the commands. But you could have the controlling .bat file generate the ftp script dynamically, and do the variable stuff while you're still at the shell level.
我认为 Windows 中的命令行 FTP 客户端不够智能,无法对命令进行环境/shell 变量插值。但是您可以让控制 .bat 文件动态生成 ftp 脚本,并在您仍处于 shell 级别时执行变量操作。