批量Windows 7 - 将目录和文件上载到FTP

时间:2017-02-25 09:32:24

标签: windows batch-file ftp

我正在为Windows 7计算机编写批处理脚本。该脚本的目标是将文件从目录C:\directory\source_dir\移动到ftp服务器ftpserver.domain.com

并非所有文件都应上传到ftp服务器,所以我使用正则表达式。

文件结构:

  

C:\目录\ source_dir \ TF_directory1
  C:\目录\ source_dir \ TF_directory1 \ FILE1.TXT
  C:\目录\ source_dir \ TF_directory1 \ FILE2.TXT
  C:\目录\ source_dir \ TF_directory1 \ sub_dir \ file_A.txt
  C:\目录\ source_dir \ Ignore_directory \ not_important.txt
  C:\目录\ source_dir \ TF_123.CAM555.abc
  C:\目录\ source_dir \ TF_123.CAM123.zyx
  C:\目录\ source_dir \ TF_987.CAM555.abc
  C:\目录\ source_dir \ wrong_file.txt

从上面的结构TF_directory1中,应该上传内部的所有内容。文件应该TF_123.CAM555.abcTF_123.CAM123.zyxTF_987.CAM555.abc

这是我的问题:

ftp put命令在目录

上返回错误
connected to ftpserver.domain.com
220 Welcome to the ftp server
ftp> user USERNAME
331 Please specify the password

---> PASS PASSWORD
230 Login successful.
ftp>
ftp> cd new_files
250 Directory successfully changed.
---> CWD new_files
ftp> put C:\directory\source_dir\"TF_123.CAM555.abc"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\TF_123.CAM123.zyx"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\TF_987.CAM555.abc"
---> PORT 10,X,X,X,4,240
200 PORT command successful. Consider using PASV.
---> STOR TF_123.CAM555.abc
150 Ok to send data.
226 File receive OK.
ftp> put C:\directory\source_dir\"TF_directory1"
**Error opening local file C:\directory\source_dir\TF_directory1.**
ftp> quit
---> QUIT
221 Goodbye.

脚本:

set base_dir=C:\directory\
set log_dir=%base_dir%source_dir\
set log_file=%base_dir%log_file.txt

::Function to check if the ftpinfo exists. If not, create it.
:createFTPinfoFile
echo ########################## entering function :createFTPinfoFile
if not exist %base_dir%ftpinfo.dat (
    echo %timestamp% -- Creating ftpinfo.dat file at location %base_dir% >> %log_file%
    echo user USERNAME> %base_dir%ftpinfo.dat
    echo PASSWORD>> %base_dir%ftpinfo.dat
    echo %timestamp% -- Created ftpinfo.dat >> %log_file%
) ELSE (
    echo %timestamp% -- %base_dir%ftpinfo.dat was not properly removed - Removing the file >> %log_file%
    del %base_dir%ftpinfo.dat
    echo %timestamp% -- Creating ftpinfo.dat file at location %base_dir% >> %log_file%
    echo user USERNAME> %base_dir%ftpinfo.dat
    echo PASSWORD >> %base_dir%ftpinfo.dat
    echo %timestamp% -- Created ftpinfo.dat >> %log_file%
)
echo ############################ finished :createFTPinfoFile
EXIT /B 0

:addFilesToFTPinfo 
echo ############################ entering function :addFilesToFTPinfo
set num=0
echo %timestamp% -- Starting to add files from %log_dir% to ftpinfo.dat >> %log_file%
for /f "delims=" %%i in ('forfiles /p %log_dir% /m "TF_*.CAM*.*" /d -0 -c "cmd /c echo put %log_dir%@file >> %base_dir%ftpinfo.dat & echo 1" ^| find /c /v ""') do set /a num=%%i-1
echo %timestamp% -- Starting to add folders from %log_dir% to fptinfo.dat >> %log_file%
for /f "delims=" %%i in ('forfiles /p %log_dir% /m "TF_*" /d -0 /c "cmd /c if @isdir==TRUE echo put %log_dir%@file >> %base_dir%ftpinfo.dat & echo 1" ^| find /c /v ""') do set /a num=%num%+%%i-1
echo %timestamp% -- added everything to ftpinfo.dat >> %log_file%
echo ############################ finished :addFilesToFTPinfo
EXIT /B 0

REM::This function creates the connection to the ftp server using the information from ftpinfo.dat
:ftpUploadFiles
echo ########################## entering function :ftpUploadFiles
if exist %base_dir%ftpinfo.dat (
    echo cd new_files >> %base_dir%ftpinfo.dat
    CALL :addFilesToFTPinfo
    echo quit >> %base_dir%ftpinfo.dat
    echo %timestamp% -- Connecting to FTP server to upload files >> %log_file%
    ftp -n -s:%base_dir%ftpinfo.dat ftpserver.domain.com
)
echo ########################## finished :ftpUploadFiles
EXIT /B 0

有谁知道更好的方法吗?

2 个答案:

答案 0 :(得分:0)

您正在使用put传输文件夹,但put不支持传输文件夹,只支持以下文件所述的文件:

  

**打开本地文件时出错 C:\ directory \ source_dir \ TF_directory1 **

请尝试使用:

,而不是使用put
mkdir TF_directory1
cd TF_directory1
mput C:\directory\source_dir\TF_directory1\*

哪个会:

  1. 在名为 TF_directory1
  2. 的FTP服务器上创建目录
  3. CD进入目录
  4. TF_directory1 复制所有文件并将其放入新的FTP文件夹


  5. 参考 - Superuser

答案 1 :(得分:0)

Windows命令行ftp.exe客户端不支持递归操作。

如果要传输文件夹,可以使用以下三个选项:

  1. 完成批处理文件中的所有繁重工作,为所有文件和文件夹生成ftp上传命令。虽然可行,但这很难实现。
  2. 为特定文件夹使用临时解决方案,例如@SamDenty节目的答案。
  3. 最简单的方法是使用第三方命令行FTP客户端。大多数第三方FTP客户端都支持递归操作。
  4. 例如,使用WinSCP FTP client,您可以使用如下脚本:

    open ftp://username:password@ftp.example.com/
    put TF_directory1
    put TF_123.CAM555.abc
    put TF_123.CAM123.zyx
    put TF_987.CAM555.abc
    exit
    

    从批处理文件中运行脚本(ftp.txt),如:

    winscp.com /script=ftp.txt
    

    请参阅guide for converting Windows FTP script to WinSCP script

    (我是WinSCP的作者)