我正在爲Windows 7機器編寫批處理腳本。該腳本的目標是將文件從目錄C:\directory\source_dir\
移動到FTP服務器ftpserver.domain.com
。批處理Windows 7 - 上傳目錄和文件到FTP
並非所有的文件都應該上傳到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.abc
,TF_123.CAM123.zyx
和TF_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
有誰知道一個更好的方式來做到這一點?