开发者

Is it possible to pass a variable into a Windows FTP script file?

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.

As of right now, my .bat file ha开发者_JAVA百科s a line like "ftp -s:ftp.txt". Ftp.txt contains some fairly straightforward FTP script stuff: something like this--

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.

Is this possible? Anybody know how to do it? Or is my whole approach wrong?


You could generate the ftp.txt file on the fly with your bat file. Simply do something like :

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"

For example :

echo put %file1% >>ftp.txt


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.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜