我构建了一个脚本,它读取 CSV 文件以获取文件列表,然后将它们上传到 FTP 服务器。CSV 结构是这样的:
local_file,remote_file
该脚本会创建一个包含所有必要 FTP 命令的文本文件,然后运行 FTP 命令。一切正常,除了 for 循环执行它的命令之外的代码,即,下面的所有echo put "%1" "%2" >> %Commands%
内容也在每个 for 循环上执行,而不是得到一个包含所有 put 命令的格式良好的文件,我得到了这个(来自命令文件输出):
open servername
username
password
binary
put "local_path_to\first_file_on_the_list.php" "remote_path_to\first_file_on_the_list.php"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
...
这是脚本代码:
@echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
我不明白为什么下面的一切 :end 都被执行了!非常感谢您提前