[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1264
  • Last Modified:

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.  :-)
0
SmartPcGuy
Asked:
SmartPcGuy
  • 7
  • 4
  • 3
  • +1
2 Solutions
 
Steve KnightIT ConsultancyCommented:
Sure.  There are some examples of the different methods on my page here:

http://scripts.dragon-it.co.uk/links/batch-ftp-scripting

Sometimes you need to work differently with different servers but for nwo try this:

@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

Save that as "myftp.cmd" or whatever, just not ftp.cmd otherwise may cause confusion with ftp.exe.

You can test out what you need to do by using ftp.exe manually:

Start | run and type in cmd.exe
Then type this lot:
ftp
open x.x.x.x  (where x.x.x.x is the IP address or hostname of your server)
(it will ask for user/password, enter those)
lcd C:\source\data  (where the path you want to copy files from)
cd /destination/folder (where the folder is to copy TO)
prompt  (to turn off any asking of overwriting etc.)
mput *.* (to send all files from this dir to the dest.)
quit (to stop the connection)
exit (to close the cmd.exe prompt)

Steve
0
 
AlexPaceCommented:
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

0
 
SmartPcGuyAuthor Commented:
Steve and Alex.  Thank you both. I will try both scripts as soon as possible and get back to you.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Bill PrewCommented:
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
0
 
SmartPcGuyAuthor Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
Ok, it works differently with different servers and timings when automated sometimes.  For starters try:

echo user username password

instead.

Steve


0
 
SmartPcGuyAuthor Commented:
Thanks Steve!!!
0
 
Steve KnightIT ConsultancyCommented:
No problem, glad it helped.
Steve
0
 
SmartPcGuyAuthor Commented:
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!
0
 
Bill PrewCommented:
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
0
 
SmartPcGuyAuthor Commented:
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.
0
 
SmartPcGuyAuthor Commented:
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.  :-)
0
 
Bill PrewCommented:
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
0
 
AlexPaceCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
SmartPcGuyAuthor Commented:
No I didn't steve.  Try re-sending.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now