Batch Windows 7 - Upload directory and files to FT

2019-06-08 03:03发布

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-08 03:41

You are using put to transfer a folder, but put doesn't support transferring folders, only files as stated by:

**Error opening local file C:\directory\source_dir\TF_directory1**

Instead of using put, try using:

mkdir TF_directory1
cd TF_directory1
mput C:\directory\source_dir\TF_directory1\*

Which would:

  1. Make a directory on the FTP server named TF_directory1
  2. CD into the directory
  3. Copy all files from the TF_directory1 and place them in the new FTP folder



Reference - Superuser

查看更多
\"骚年 ilove
3楼-- · 2019-06-08 03:49

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

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

  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.

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

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

winscp.com /script=ftp.txt

See the guide for converting Windows FTP script to WinSCP script.

(I'm the author of WinSCP)

查看更多
登录 后发表回答