FTP over CMD for different servers - windows

So I am trying to write a small batch program to copy files over ftp to another device
The problem is the devices I am copying to are all different servers, but the information i'm copying stays the same.
How can I write this so when I open the batch program, i specify the IP address of the device, and the batch will connect to the server automatically and copy the directories or files i need copied.
Currently it will allow me to input the IP, connect to the server and open a specific file, but every time it tries to connect to copy files it says invalid directory or incorrect server.
:Log
set /p PDTFTP= Enter PDT IP Address:
start "ftp://admin:2p0d0t7#%PDTFTP%/pub/IPSM/fds/log/PDTApplicationLog.txt"
:DB
set /p PDTFTP= Enter PDT IP Address:
xcopy "C:\test.txt" "ftp://admin:2p0d0t7#%PDTFTP%/pub/IPSM/fds/"
Is there a way to do this when the ftp server will be different almost every time its used

You could do it like this:
to download the file:
#echo off
set /p ip=IP:
echo username> temp.txt
echo password>> temp.txt
echo get fileToGet>> temp.txt
echo quit>> temp.txt
ftp -s:temp.txt %ip%
del temp.txt
to upload the file:
#echo off
set /p ip=IP:
echo username> temp.txt
echo password>> temp.txt
echo put fileToUpload>> temp.txt
echo quit>> temp.txt
ftp -s:temp.txt %ip%
del temp.txt

Related

Upload files generated in last one hour to FTP

I'm trying to upload latest files generated in one hour to a remote server. But the script I wrote is intelligent enough to gather only the latest file in directory and upload it to FTP. Any chance of set the variable type as array and upload the files in the array?
My FTP batch file:
FOR /F %%I IN ('DIR "abcdef*.bac" /B /O:D') DO SET latest_file=%%I
echo user domain/username> ftp.txt
echo password>> ftp.txt
echo cd remotepath>> ftp.txt
echo put %latest_file%>>ftp.txt
echo quit>> ftp.txt
ftp -n -s ftp.txt Servername>ftp_logs.txt
del ftp.txt
It would be difficult to write this in a pure batch file code.
You better use some more powerful tool.
For example with WinSCP FTP client, it's trivial:
winscp.com /ini=nul /log=upload.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"put -filemask=>1H C:\local\path\abcdef*.bac /remote/path/" ^
"exit"
References:
Automate file transfers to FTP server with WinSCP;
Converting Windows FTP script to WinSCP script;
File masks with time constraints.
(I'm the author of WinSCP)

Upload directory and files to FTP in Windows batch

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?
The Windows command-line ftp.exe client does not support recursive operations.
If you want to transfer folders, you have three options:
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.
Use an ad-hoc solution for your specific folder(s), like the answer by #SamDenty shows.
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)
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:
Make a directory on the FTP server named TF_directory1
CD into the directory
Copy all files from the TF_directory1 and place them in the new FTP folder
Reference -
Superuser

batch script to download a file via ftp, how can I handle the overwrite

Good day all.
I've done a batch script that connects to an FTP and download a file on the same location of the .bat file. everything works fine, now, is there a way to "ask for file overwrite permissions"? I mean, if the user already have a file named in the same way, is it possible to prevent overwrite and ask him what to do? the code actually is :
#echo off
echo user MYUSERNAME> ftpcmd.dat
echo MYPASSWORD>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo cd /www.website.com/>>ftpcmd.dat
echo get afile.txt>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.website.com
del ftpcmd.dat
You can use if exist "afile.txt" in advance and use choice to let the user decide what to do:
#echo off
if exist "afile.txt" (
choice /C YN /M "Overwrite 'afile.txt'? "
) else (
> nul ver & rem (clear `Errorlevel`)
)
if not ErrorLevel 2 (
> "ftpcmd.dat" (
echo user MYUSERNAME
echo MYPASSWORD
echo bin
echo cd /www.website.com/
echo get afile.txt
echo quit
)
ftp -n -s:ftpcmd.dat ftp.website.com
del "ftpcmd.dat"
)
However, since there is no -i switch in the ftp command line and the interactive prompt mode should be on as per default, I assume the ftp command would prompt the user anyway...
Corrective Update: The "interactive mode" means that the mget and mput will ask user to confirm transfer of each matching file, not matter if it exists or not. See technet.microsoft.com/en-us/library/bb490670.aspx. There are no overwrite confirmations in ftp.exe. [Martin Prikryl]
Use the if exist command to test, if the local file exists already before the transfer.
If it exists, ask an user for confirmation.
#echo off
if not exist afile.txt goto download
:ask
echo Overwrite?
set INPUT=
set /P INPUT=Y/N: %=%
If /I "%INPUT%"=="Y" goto download
If /I "%INPUT%"=="N" exit
echo Incorrect input
goto ask
:download
...
ftp -n -s:ftpcmd.dat ftp.website.com
Though for a long term solution, you better use PowerShell, instead of hacking this in a batch file.

Windows batch - ftp uploading and connection

I would like to upload a file to my ftp server if internet is connected
In each run, i prefer:
if (ftp server can be connected){
upload the file "C:\abc.txt" to the ftp server directory "/ABC_DB"
}
Thus, I am not sure how to check ftp connection, is it possible to run like this:
echo abc > C:\abc.txt
???Check the connection here
OPEN your.ftp.server.com
usernameabc
passwordbcd
CD /ABC_DB
PUT "C:\abc.txt"
QUIT
PAUSE
Sorry for asking such silly question, but I am new in batch, hope u can help me =[
Test this: change lines 2,3,4 with your details
#echo off
set "name=your_ftp_user-name"
set "password=your_ftp_password"
set "server=ftp_server_name"
ping %server% |find /i "TTL=" >nul || (echo server offline, aborting&pause&goto :EOF)
set "ftpScript=%temp%\%~nx0.ftp.tmp"
(
echo open %server%
echo %name%
echo %password%
echo bin
echo CD /ABC_DB
echo PUT "C:\abc.txt"
echo quit
) > "%ftpScript%"
ftp -i -s:"%ftpScript%"
del "%ftpScript%"

How to capture the ftp error code in batch scripts?

I have a somewhat related, but different questions here.
I have a batch script (*.bat file) such as this:
#ftp -i -s:"%~f0"&GOTO:EOF
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye
Basically this is a script that uploads a zip file to a ftp site. My question is that, the upload operation can fail from time to time ( the remote ftp is not available, "myfile.zip" is non-existent, upload operation interrupted and whatnot), and when such unfortunate things happen, I want my bat file return 1 ( exit 1).
It would be great if my upload wasn't successful, the ftp would throw an exception ( yes, like exception in C++), and I would have a catch-all exception that catches it and then exit 1, but I don' think this is available in batch script.
What is the best way to do what I need here?
You can redirect the output to a log file and when the ftp session is finished the file can be parsed.
#ftp -i -s:"%~f0" > log.txt & GOTO :parse
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye
:parse
for /F "delims=" %%L in (log.txt) Do (
... parse each line
)
Windows FTP does not return any codes.
I suggest running a batch file that echo your ftp commands to a input response file, then use this file as input to the ftp command, redirecting stderr to a file and verifying the file size. something like this
echo open ftp.myhost.com >ftpscript.txt
echo myuser >>ftpscript.txt
echo mypassword >>ftpscript.txt
echo lcd "C:\myfolder" >>ftpscript.txt
echo cd /testdir >>ftpscript.txt
echo binary >>ftpscript.txt
echo put "myfile.zip" >>ftpscript.txt
echo disconnect >>ftpscript.txt
echo bye >>ftpscript.txt
ftp -i -s:ftpscript.txt >ftpstdout.txt 2>ftpstderr.txt
rem check the ftp error file size, if 0 bytes in length then there was no erros
forfiles /p . /m ftpstderr.txt /c "cmd /c if #fsize EQU 0 del /q ftpstderr.txt"
if EXIST ftpstderr.txt (
exit 1
)
Your only option in batch files that I know of is to use the "IF ERRORLEVEL" syntax, which requires your ftp client to return a non-zero error code.
http://www.robvanderwoude.com/errorlevel.php is a good reference guide.
Unfortunately I do not if the standard Windows ftp client returns non-zero error codes, so you may have to code your own if this is a requirement. This link suggests that it does not return an error code, but provides a work around albeit clunky, by redirecting the output to a file and using the FIND command to return an error code.

Resources