Windows 7批处理-将目录和文件上传到FTP [英] Batch Windows 7 - Upload directory and files to FTP

查看:110
本文介绍了Windows 7批处理-将目录和文件上传到FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a batch script for a Windows 7 machine. The goal of the script is to move files from the directory C:\directory\source_dir\ to an ftp server ftpserver.domain.com.

不是所有文件都应该上传到ftp服务器,所以我使用的是正则表达式.

Not all the files should be uploaded to the ftp server, so I'm using regular expression.

C:\ directory \ source_dir \ TF_directory1
C:\ directory \ source_dir \ TF_directory1 \ file1.txt
C:\ directory \ source_dir \ TF_directory1 \ file2.txt
C:\ directory \ source_dir \ TF_directory1 \ sub_dir \ file_A.txt
C:\ directory \ source_dir \ Ignore_directory \ not_important.txt
C:\ directory \ source_dir \ TF_123.CAM555.abc
C:\ directory \ source_dir \ TF_123.CAM123.zyx
C:\ directory \ source_dir \ TF_987.CAM555.abc
C:\ directory \ source_dir \ wrong_file.txt

C:\directory\source_dir\TF_directory1
C:\directory\source_dir\TF_directory1\file1.txt
C:\directory\source_dir\TF_directory1\file2.txt
C:\directory\source_dir\TF_directory1\sub_dir\file_A.txt
C:\directory\source_dir\Ignore_directory\not_important.txt
C:\directory\source_dir\TF_123.CAM555.abc
C:\directory\source_dir\TF_123.CAM123.zyx
C:\directory\source_dir\TF_987.CAM555.abc
C:\directory\source_dir\wrong_file.txt

从上述结构TF_directory1中,应该上传其中的所有内容.文件TF_123.CAM555.abcTF_123.CAM123.zyxTF_987.CAM555.abc也应如此.

From the above structure TF_directory1 and everything inside should be uploaded. As should the files TF_123.CAM555.abc, TF_123.CAM123.zyx and TF_987.CAM555.abc.

这是我的问题:

ftp put命令在目录上返回错误

The ftp put command returns an error on the directory

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

有人知道更好的方法吗?

Does anyone know a better way to do this?

推荐答案

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

The Windows command-line ftp.exe client does not support recursive operations.

如果要传输文件夹,则有三个选项:

If you want to transfer folders, you have three options:

  1. 在批处理文件中进行所有艰苦的工作,为所有文件和文件夹生成ftp上载命令.尽管可以实现,但很难实现.
  2. 为您的特定文件夹使用临时解决方案,例如@SamDenty显示的答案.
  3. 最简单的方法是使用第三方命令行FTP客户端.大多数第三方FTP客户端都支持递归操作.
  1. Do all the hard work in the batch file, generating ftp upload commands for all files and folders. While doeable, this is pretty difficult to implement.
  2. Use an ad-hoc solution for your specific folder(s), like the answer by @SamDenty shows.
  3. Easiest is to use a 3rd party command-line FTP client. Most 3rd party FTP clients do support recursive operations.


例如,对于 WinSCP FTP客户端,您可以使用如下脚本:


For example with WinSCP FTP client, you can use a script like:

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):

And run the script (ftp.txt) from a batch file like:

winscp.com /script=ftp.txt

请参见将Windows FTP脚本转换为WinSCP脚本的指南.

(我是WinSCP的作者)

这篇关于Windows 7批处理-将目录和文件上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆