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
.
Not all the files should be uploaded to the ftp server, so I'm using regular expression.
File Structure:
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
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
.
Here's my problem:
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.
Script:
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?
You are using
put
to transfer a folder, butput
doesn't support transferring folders, only files as stated by:Instead of using
put
, try using:Which would:
Reference - Superuser
The Windows command-line
ftp.exe
client does not support recursive operations.If you want to transfer folders, you have three options:
ftp
upload commands for all files and folders. While doeable, this is pretty difficult to implement.For example with WinSCP FTP client, you can use a script like:
And run the script (
ftp.txt
) from a batch file like:See the guide for converting Windows FTP script to WinSCP script.
(I'm the author of WinSCP)