Windows Batch:通过FTP自动上传本地CSV文件列表

时间:2012-05-30 13:30:16

标签: file-upload csv ftp batch-file automation

我构建了一个脚本,该脚本读取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

我不明白为什么下面的一切:结束了! 非常感谢您提前

3 个答案:

答案 0 :(得分:2)

为什么:end下面的代码不应该执行?你永远不会在脚本中添加任何内容来结束:解析例程。

第一个ewall答案应该几乎可以工作,除了在FOR语句之后你需要一个GOTO:END。我不知道如何正确实施他的建议会导致无休止的循环。

另一种选择是在EXIT语句之后简单地移动子程序。

您还有其他隐藏的问题。文件路径/名称可以包含空格,因此如果有空格,则空格的默认FOR分隔符,tab不会保留整行。默认EOL还会导致忽略以;开头的任何行。这是一个潜在的(但不太可能)问题,因为有效的文件名可以以;开头。

解决方案是将EOL设置为无法开始有效文件规范的字符,并将DELIMS设置为空:"EOL=: DELIMS="

将所有文件写入行括在括号中并将输出重定向一次会更有效。它写起来也更容易,看起来更好。

编辑 - 原始脚本尝试在ftp脚本和ftp命令行中连接到服务器。必须删除其中一个或另一个。我从ftp脚本中删除了它。

@echo off
setlocal EnableExtensions

rem Connection credentials
set Server=servername
set UserName=username
set Password=password

set Commands="commands.txt"
(
  echo %UserName%
  echo %Password%
  echo binary

  rem Read the CSV file line by line
  for /f "eol=: delims=" %%a in (matches3.csv) do call :parse %%a

  rem Add commands to close ftp conn
  echo close
  echo bye
)>%Commands%

rem Perform the FTP upload
echo logging in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause

rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit

rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2"

答案 1 :(得分:0)

:end不会阻止CALL function运行,它只是一个标签。相反,我怀疑你打算使用GOTO :eof,它将返回for循环:

...
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%

goto :eof

rem Add commands to close ftp conn
...

或者,您可能会发现使用括号更简单,而不是使用调用,如下所示:

...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do (
  rem Transform CSV line into FTP put command
  echo put "%%a" "%%b" >> %Commands%
)

rem Add commands to close ftp conn
...

答案 2 :(得分:0)

也许这有效

@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 /b



rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
exit /b

:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye   >> %Commands%
exit /b