批处理 Windows 7 - 上传目录和文件到 FTP

泽维尔

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

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

档案结构:

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.abc,TF_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

有谁知道更好的方法来做到这一点?

马丁·普里克里尔

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

如果要传输文件夹,您有三个选择:

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

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

open ftp://username:[email protected]/
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

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

(我是WinSCP的作者)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows批处理-ftp上传和连接

Windows 7批处理文件以创建目录

Windows批处理文件-仅将最新文件上传到FTP

Spring批处理FTP文件

在Windows 7上以批处理文件启动服务

Windows 7启动批处理文件已隐藏

Windows 7中的批处理文件故障

在Windows批处理脚本中按文件传输到FTP文件

在Windows批处理文件中通过命令行ftp使用%USERNAME%环境变量

我可以在Windows的FTP批处理文件中使用SOCKS5吗?

FTP 用户名与 | (管道)在 Windows 批处理文件中

如何在Windows 7上通过批处理文件删除目录树?

使用批处理脚本的 FTP 文件传输

通过批处理文件进行FTP

通过批处理文件上传文件到FTP服务器

Windows文件中批处理文件中具有国际字符/突变/元音(ä。ö。ü)的文件夹中的FTP文件

Windows批处理文件处理-循环

在Windows 7中批处理文件以将重要文件存档为zip文件

Windows 7批处理文件循环访问子目录,选择第一个文件

从Windows 7中的任何文件夹运行批处理文件

如何使用批处理脚本删除Windows 7中许多文件的文件名的某些部分?

Windows 7中的批处理文件以查找不匹配模式的文件

Windows7批处理文件:Net Use命令

Windows 7在宽窗口中以低优先级运行批处理文件

让Windows 7使用组策略编辑器执行批处理文件;但保持与Notepad ++的关联

Windows 7:如何修改哪个程序打开批处理文件?

使用批处理文件创建并启动Windows 7或10服务?

在Windows 7上运行在XP上创建的批处理文件

Windows 7 Admin用户-批处理文件始终以管理员身份运行