Link to home
Start Free TrialLog in
Avatar of SmartPcGuy
SmartPcGuyFlag for United States of America

asked on

Copy files to FTP server by batch file/script

Would someone be so kind as to create a very basic windows script, that will allow me to copy all files in a specific directory on my server, to an FTP server?  I will create a scheduled task in the middle of the night that will run the script and transfer the files.  I would like it to be automatic and silent.

I know, I know... there's already a dozen or more people who've already asked this question, and there have been plenty of answers.  None of them worked for me, mostly because I don't have time right now to learn FTP scripting, I just need help a.s.a.p.  Please and thank you, very much.  :-)
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The following Robo-FTP Script will log in to the FTP site every night at midnight and upload all files.  If a file that exists in the local source folder also exists on your FTP site, the file will only be uploaded if the local file modification date is newer than the one on the server.  This saves you uploading the same file over and over every night.
:top
CRON "@daily"  ;; wait until midnight
WORKINGDIR "c:\local\source\folder"
FTPLOGON "ftp.domain.com" /user="UserID" /pw="secret"
FTPCD "/remote/destination/folder"
SENDFILE "*" /localdate /ifnewer
FTPLOGOFF
GOTO top

Open in new window

You might notice this first example is written as an infinate loop.  The reason is so you can install it as a Windows Service instead of running it as a scheduled task.  

If you are sure that you'd like to run it as a scheduled task instead you could remove the CRON command and replace the GOTO at the bottom with EXIT:
WORKINGDIR "c:\local\source\folder"
FTPLOGON "ftp.domain.com" /user="UserID" /pw="secret"
FTPCD "/remote/destination/folder"
SENDFILE "*" /localdate /ifnewer
FTPLOGOFF
EXIT

Open in new window

Avatar of SmartPcGuy

ASKER

Steve and Alex.  Thank you both. I will try both scripts as soon as possible and get back to you.
Avatar of Bill Prew
Bill Prew

If you want to do it without scripting, you can use the ncftpput utility (link below) and do it all with a single command line like:

ncftpput -u userid -p password ftp.yourhost.com "." "c:\temp\files.*"

~bp

http://www.ncftp.com/ncftp/
http://www.ncftp.com/ncftp/doc/ncftpput.html
Steve.  Your auto-script below works fine....

@echo off
(echo open 110.110.110.110
echo user username
echo password
echo lcd c:\source\path
echo cd /destination/path
echo bin
echo prompt
echo mput *.*
echo quit
) | ftp -n -i

...as long as I type it into a command prompt manually  :-)

If I try to run it as a batch, I get a prompt for a password.  However, when I enter the password in at THAT prompt, it fails with "invalid command" and then something about "interactive mode ON".

Again in summary:
If I type all of your commands out line by line, files and folders copy successfully. But if I create a batch using the auto script you created, (with the all the echo commands) it asks for a password.

Thanks!

If I type every line in manually it works.  Help please.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Steve!!!
No problem, glad it helped.
Steve
Hello again Steve.  Can you help me again please?  

The script works great, with the exception of files that are interrupted.  When they get interrupted, the stop transferring.  I've spent several hours trying to modify the script with some conditions to continue the transfers if interrupted, but I'm not having any luck.

In summary, how do I:
1. Add FTP commands to my script which will allow my file transfers to resume if interrupted.
2. Allow my transfers to pick up where they left off. I don't want it to re-download 1.99 GB of a 2GB file if it stops right near the end.

Is this possible?

Thanks!
Unfortunately I'm 90% sure that the built in Windows FTP client doesn't support resuming of transfers.

If you want that capability you will likely have to use an add-on FTP client that has more functionality, like the NCFTP clients I mentioned above.

~bp
billprew. Thanks for the reply.

I attempted to get it working with NCFTP, but it was a little too complicated for me.  I wasn't able to make much progress.  

I'll try again now.  Thanks again.
billprew.  Upon closer inspection, I will not be able to use NCFTP, for I would need to install it on tens, if not hundreds of computers and servers over time.  Since there is no windows installer, and all the install instructions appear to be Linux related, I cannot invest the time to manually install it on that many workstations.

Thank you very much for your help and expertise.  I do appreciate it.  :-)
It's fine if you don't want to install additional code on all those machines, I can understand that.  But FYI, there is a Windows installer titled "NcFTP Client 3.2.5 for Microsoft Windows" on this page.  You are only interested in the NCFTPGET or NCFTPPUT client programs, those are the command line ones.  I suspect you could do the install on one computer, move the EXEs to a central server and then run them from that server location on the PCs needed, without doing an install, but I haven't tested that.

http://www.ncftp.com/download/

~bp
Here is a modified version of the Robo-FTP script above, this one with retry ability.  You can set the number of retries by chaging the value of LOOPCOUNT:
FTPLOGON "ftp.domain.com" /user="UserID" /pw="secret"
FTPCD "/remote/destination/folder"
WORKINGDIR "c:\local\source\folder"
:find_file 
GETFILE "*" * get next file to send
IFERROR!= $ERROR_SUCCESS GOTO done
SENDFILE %nextfile /localdate /ifnewer * upload if newer
IFERROR= $ERROR_SUCCESS GOTO find_file
IFERROR= $ERROR_SRVR_FILE_NEWER GOTO find_file  *file not newer
LOOPCOUNT 5  * max number of retries
:try_resume
IFNFILE %nextfile GOTO find_file * file doesn't exist; exit resume loop
SENDFILE %nextfile /localdate /resumeany
IFERROR= $ERROR_SUCCESS GOTO find_file * upload OK; exit resume loop
LOOPTO try_resume  
GOTO find_file * give up on this file after failing LOOPCOUNT times
:done
FTPLOGOFF
EXIT 

Open in new window

This version of the script only uploads files that are newer than the version on the server but the script could be even more simple if you didnt need that feature.

At $150 a pop Robo-FTP may be too expensive to install on 100 machines if you buy retail so, if you go this route, you should ask for pricing on a site license.  

Another strategy that works is to use one instance of Robo-FTP to send files from multiple machines on the same network.  For example on the script above if you set a new WORKINGDIR on another computer an then called GETREWIND you could process files on that other machine as well...  or you could reverse the process and create one network share used by all the computers on that network.  Each machine would have a little batch file or VBS or whatever that copies files to be sent into the network share and then you use a single instance of Robo-FTP to upload everything in the share.

Here is a Robo-FTP script that is designed to run as a "hot-send" service that constantly monitors a folder and uploads any files that appear and then deletes the file after a successful upload:
LOG "hot_send_script.log"
TRACELOG "hot_send_trace.log"
WORKINGDIR "c:\data\source\folder"

:find_file 
GETNEXTFILE "*" /timeout=0 
IFERROR= $ERROR_WAIT_TIMED_OUT GOTO find_file
 
:upload 
FTPLOGON "ftp.new.com" /user=anonymous /pw=itchy 
IFERROR!= $ERROR_SUCCESS GOTO upload_error 
SENDFILE %nextfile /resumeany
IFERROR!= $ERROR_SUCCESS GOTO upload_error 
FTPLOGOFF 
DELETE %nextfile 
GOTO find_file 

:upload_error 
FTPLOGOFF 
IFFILE %nextfile  ; if file exists try to upload 
GOTO find_file     ; file no longer exists so find another file 

Open in new window

This one will do an infinate number of retries as long as the source file still exists.
Did you get my email in response to your "hire me" request?  WPUT.exe (http://wput.sourceforge.net/) was the suggested method, though ncftput.exe would most likely be similar.

Steve
No I didn't steve.  Try re-sending.