I'm trying to make a bat script to upload all files from a folder to an FTP server.
I followed the below guide and manage to get a single file uploaded but can't figure out how to upload everything in the folder.
How to Automate FTP Uploads from the Windows Command Line
From what I've read I think i need to somehow use the mput command?
At the moment my upload.bat file looks like this:
myftp.bat .\logs\test.txt
inside myftp.bat is:
#echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat MyFTPServer
del ftpcmd.dat
pause
You need to use mput command like:
cd logs
prompt
mput *
Without prompt command, you would get asked to confirm each transfer.
Instead of the prompt command, you can also use the -i switch:
ftp -i -n -s:ftpcmd.dat MyFTPServer
You could also make a batch file that runs multiple other hidden batch files so that you can transfer each file with an individual batch file. If you want the code for this, just ask but it looks like the best solution has already been said ^^.
Related
I have this batch file which will be able to send files to my FTP. It logs in and everything but connects via the wrong port. Is there a way I can make this connect via port 22?
This FTP is with 000webhost
#echo off
CD %userprofile%/desktop
echo user MyUsername> ftpcmd.dat
echo PASSWORDHERE>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat files.000webhost.com
del ftpcmd.dat
For an answer to your literal question, see:
Cannot connect to non standard port number with Windows command-line ftp.
Though, the port 22 is for SSH/SFTP. That has nothing to do with FTP, so you cannot use command-line ftp.
There's no built-in SFTP client in Windows. You have to use some 3rd party SFTP client.
For that, see Secure FTP using Windows batch script.
There is a alternative way for this. The ftp command has a subcommand open, and the open subcommand support specific port, Click here. So you can put the subcommand open in your ftpcmd.dat file and open connect to your ftp server with a specific port via this subcommand.
For example, I did a test on my site, open ftp connect to a ftp server 172.18.128.122 with port 21, it works.
C:\>ftp -d -v -s:test.txt 127.0.0.1 #note, it is not ftp server ip here.
ftp> open 172.18.128.122 21
Name(172.18.128.122:(none)):
---> USER test
---> PASS test
ftp> put aaa.txt
---> PORT 172,18,128,121,218,69
---> STOR aaa.txt
ftp> quit
---> QUIT
And my test.txt file is like below:
open 172.18.128.122 21
test
test
put aaa.txt
quit
So I think your bat file can be like below:
#echo off
CD %userprofile%/desktop
echo open files.000webhost.com 22> ftpcmd.dat
echo user MyUsername>> ftpcmd.dat
echo PASSWORDHERE>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat 127.0.0.1
del ftpcmd.dat
Hope it help, FYI.
I can't try to reconnect with a different password after authentication failed. ERRORLEVEL seems to not be working in my code. I have no clue what's going wrong. The code is in batch file on Windows 10.
#echo off
echo use USERNAME> ftpcmd.dat
echo PASSWORD>> ftpcmd.dat
ftp -n -s:ftpcmd.dat files.000webhost.com
if %ERRORLEVEL% neq 0 (
goto TEST
)
:TEST
echo user USERNAME> ftpcmd.dat
echo DIFFERENTPASSWORD>> ftpcmd.dat
echo prompt>>ftpcmd.dat
echo cd Test>>ftpcmd.dat
echo mput FILE PATH>> ftpcmd.dat
ftp -n -s:ftpcmd.dat FTPTHING
The Windows ftp.exe always returns 0, no matter what.
All you can possibly do, is to parse its output and look for errors. But that's pretty unreliable.
If you want to take this approach anyway, see
how to capture error conditions in windows ftp scripts? or
Batch command to check if FTP connection is successful.
Though, you better use some better FTP client. Sooner or later you will run into troubles with the ftp.exe anyway, as it does not support an encryption, the passive mode or recursive transfers, among many other limitations.
If you are familiar with PowerShell, use the FtpWebRequest or WebClient from a Powershell script. There are numerous examples here, for example Upload files with FTP using PowerShell.
If not, use some 3rd party command-line FTP client.
For example with WinSCP FTP client, your batch file may look like:
#echo off
winscp.com /ini=nul /log=script.log /command ^
"open ftp://USERNAME:PASSWORD#ftp.example.com/" ^
"put C:\LOCAL\FILE /REMOTE/PATH/" ^
"exit"
if ERRORLEVEL 1 (
winscp.com /ini=nul /log=script2.log /command ^
"open ftp://USERNAME:DIFFERENTPASSWORD#ftp.example.com/" ^
"put C:\LOCAL\FILE /REMOTE/PATH/" ^
"exit"
)
References:
Automate file transfers to FTP server;
How do I know that script completed successfully?;
Guide to converting Windows FTP script to WinSCP SFTP script.
WinSCP can also be used from PowerShell, if you want even more robust implementation.
(I'm the author of WinSCP)
I searched from several posts to find the solution for this problem but couldn't find it. I want to build a Batch file that goes to a folder on a FTP and Download all the records then Delete all of them.
So far I tried to Use the command "mdel .", please see the code below:
#echo off
echo user ftpUser> ftpcmd.dat
echo Password>> ftpcmd.dat
echo cd /tst/>>ftpcmd.dat
echo binary>> ftpcmd.dat
echo prompt n>> ftpcmd.dat
echo mget *.*>> ftpcmd.dat
echo mdel *.*>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat 77.99.999.999
del ftpcmd.dat
For this solution I noticed that if i use the command "delete" and specify the file it works. Like this:
echo delete 312312312.csv >> ftpcmd.dat
But for some reason using mdel(like the example) it doesn't.
Do you know a better way to make it works?
Thanks
The mdel command needs to list files in the remote directory. The problem is that the Windows built-in ftp.exe supports an FTP active mode only. In this mode the server needs to open an incoming connection to your machine, to transfer the listing, what gets by default blocked by Windows firewall or any other firewall/proxy/NAT on the way between you and the server. That makes the ftp.exe most useless nowadays.
See my article about network configuration for FTP active mode for details.
Use any other 3rd party FTP client. Most support the passive mode that does not have these kinds of problems.
As you have found out yourself, one such client is WinSCP.
Using WinSCP scripting, your batch file would be like:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://ftpUser:Password#77.99.999.999/" ^
"cd /tst" ^
"get *" ^
"rm *" ^
"exit"
Though, if supported by the server, consider using a secure FTP over TLS/SSL, by replacing the ftp:// with the ftpes://.
See also a guide for converting Windows FTP script to WinSCP script.
(I'm the author of WinSCP)
I found a workaround for this problem:
http://winscp.net/eng/docs/scriptcommand_rm#examples
Using this software I was able to get and delete several files at the same time, and the code now is like this:
option batch abort
option confirm off
open ftp://user:Password#77.XX.XXX.XXX
cd /tst/
get *.* d:\www\*.bak
rm *.*
EXIT
Thanks for the help anyway.
#Martin Prikryl Thanks for the software and the explanation.
Regards!
I am writing a batch file that will do the following:
Connect to remote FTP site
Push all files from local director to remote FTP site
Disconnet
This works fine, but I also want to empty the local directory after upload (otherwise they will be transferred everytime). Unfortunately I have a problem where the connection to side might not always be possible, and thus if I add a crude del . to the end of my batch file, it will delete the files even if they havent been uploaded.
Can anyone think of a way around this? i.e. If file uploaded then delete.
#echo off
cd \
cd c:\temp
echo user bacon> ftpcmd.dat
echo eggs>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put *.xls>> ftpcmd.dat
echo bye>> ftpcmd.dat
ftp -n -s:ftpcmd.dat <Server>
del ftpcmd.dat
It seems ftp command does not return an errorlevel on which to operate, so best option is to redirect output of ftp command to a file and use findstr to check for errors in output.
The Windows FTP command does not provide a method to guarantee the file is complete.
A command line FTP transfer could fail and you will have a truncated file and FTP doesn't provide a method to detect this.
I currently have this in a .bat file of mine:
#echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat SERVERNAME.COM
del ftpcmd.dat
However, I'm very new with .bat files and I don't quite understand what each line is doing. So if somebody could explain this to me, it would be great. Also, how do I make it upload a file to a specific directory on the site? So mainly I'm asking what do I have to replace?
You need to add a command to cd to the specific directory. Add
echo cd <path to directory> >> ftpcmd.dat
before
echo put %1>> ftpcmd.dat
I would recommend reading through this to get an idea of how ftp works in general.