gpt4 book ai didi

windows - Windows批量上传目录和文件到FTP

转载 作者:可可西里 更新时间:2023-11-01 10:33:09 27 4
gpt4 key购买 nike

我正在为 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.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

有谁知道更好的方法吗?

最佳答案

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

如果你想转移文件夹,你有三个选择:

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

例如 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 的作者)

关于windows - Windows批量上传目录和文件到FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com