Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

FTP Files from a Batch File

by Steve Wiseman on April 30, 2010 · 13 comments

in BAT Files,Microsoft,Tips,Tools,Utility


.

I have a group of log files that I need to upload every day to an archival server. For a long time I have been doing this by hand.

I wanted a way to do this without using any extra software – just the default tools available in windows.

I found a clean solution that uses a batch file, and the scripting capabilities of FTP.

First, lets start with the criteria.

I have a group of files, all named XXX.log that I want to FTP to my server

Log Files

So what is our first step?

Lets start with a batch file that will list all of the files ending with .log in the current directory. We can do this with the for .. in command:

for %%f in (*.log) do @echo %%f

Simple enough. Execute it in the folder with our files, and we see our output:

Batch Output

Now, we can use that information to build our FTP script. Let me show you a simple FTP script that can upload one file:

open hostname
administrator
Password2000
bin
put SomeFile.log
quit

If you save the above as a text file (Lets call it script_file.txt), and then feed it to the FTP program like this:

ftp -s:script_file.txt

The FTP program will run through the commands and upload the file.

So now we have both parts that we need. Lets write a batch file that will build a temp script file, process it with FTP, and then remove the temp file:


@rem ===============================
@rem = FTP Script Made by =
@rem = http://www.intelliadmin.com =
@rem ===============================

@REM = Set the name of our temp script =

@set SCRIPT_NAME=FTP_SCRIPT.FTP

@REM = Generate our FTP Script
@echo open [host]> %SCRIPT_NAME%
@echo [username]>> %SCRIPT_NAME%
@echo [password]>> %SCRIPT_NAME%
@echo bin>> %SCRIPT_NAME%
@echo hash>> %SCRIPT_NAME%

@REM = Loop through each file that matches our wildcard

@for %%f in (*.log) do @echo put %%f>> %SCRIPT_NAME%

@echo quit>> %SCRIPT_NAME%

@REM = Now call FTP with our generated script
@ftp -s:%SCRIPT_NAME%

@REM = Delete our temp script file and we are done
@del %SCRIPT_NAME% /q

It will generate our script file, fill it with the files that match our criteria (*.log), and then execute FTP with it. When it is finished, the temp file is removed.

Note that if you want to use it, change the [username], [password], and [host] to your own values. You can even change the file criteria (*.log) to any type you need to upload – or even all of them if you use *.*

Now, you can easily add it to task scheduler, or even add it as a batch command when your backups are complete.

Next week I will show you how to email yourself the results each time it is run.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 13 comments… read them below or add one }

1 X May 1, 2010 at 10:44 am

Thx for the batch script.

I’m always starting mines with @ECHO OFF. So I save typing an @ before every command. Moreover, by changing OFF for ON, I can get a full log for debugging purposes.

Nice to know there are still a few of us old timers around 😉

2 Steve Wiseman May 1, 2010 at 9:07 pm

Good point. When I get a chance I will update the script to use your method – much simpler if you want to turn echo back on again

3 ElecBoy May 3, 2010 at 10:55 pm

Hey Steve!

Is there a way to do the same, but with “SFTP” ?

I know is to much, but I youst wanted to ask.

4 Steve Wiseman May 3, 2010 at 11:00 pm

It depends – what SFTP client are you using? As far as I know, SFTP is not built into windows…and there are many 3rd party SFTP clients. Each one has its own automation, or the lack of.

5 Jim Dandy May 5, 2010 at 1:07 pm

I make my batch files much smaller, and simpler. This will list the files in the SubDirectory “Files”. an transfer them to the site one by one. Replace site.com with your site. Replace Username with the username. Replace Password with the password.

dir /b .\Files >List.txt
for /f "Tokens=1 Delims= " %%a in (List.txt) do (
echo open site.com>FTP.ini
echo Username>>FTP.ini
echo Password>>FTP.ini
echo put ".\Files\%%a">>FTP.ini
echo quit>>FTP.ini
ftp -s:FTP.ini
)

6 X May 6, 2010 at 10:12 am

“much smaller, and simpler”, and not working either .

7 Steve Wiseman May 6, 2010 at 10:58 am

That might have been my fault. I edited his comment so the code looked more like a batch file…and it seems the >> were switched to >

I have updated the code now…so I think it should work. Still smaller does not always mean simpler. This way seems to be a little harder to read. To each his own…..

8 Amit Sharma March 22, 2012 at 2:19 am

With this script can we upload file to FTP in a particular directory at FTP also?
I am not able to do so.

9 Jose September 13, 2012 at 11:21 am

@Amit Yes you can. Between @echo bin>> %SCRIPT_NAME% and @echo hash>> %SCRIPT_NAME% enter the line below. Just change path/that/you/want to the correct path.

@echo cd path/that/you/want>> %SCRIPT_NAME%

10 Nasid Marediya December 7, 2012 at 1:47 pm

Hi

we would like to download one Zip file from Ftp server To many remote clients system at one point of time just running a batch file

so kindly suggest me batch file coding

11 Mohamed Marjuk January 20, 2013 at 2:00 am

Really Good script, But i just want one more option to upload only the changed files and newly created zip folder, bcoz its start uploading all the files the one already uploaded.

12 Derwael karen March 8, 2013 at 12:21 pm

Hello

Nice script but I need to do the same for downloading files with the use of a list. Is that also possible?

Karen

13 Terry Jo Parkin October 12, 2013 at 9:29 am

I need to append to files on an server, Will this work if I need to append mutlitple files. I’m using mput @.Dat now but it overwrites the files , I need to append to the files.

Leave a Comment

Category Links - Windows Forum - Exchange Forum