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
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:
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
{ 13 comments… read them below or add one }
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 😉
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
Hey Steve!
Is there a way to do the same, but with “SFTP” ?
I know is to much, but I youst wanted to ask.
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.
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
)
“much smaller, and simpler”, and not working either .
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…..
With this script can we upload file to FTP in a particular directory at FTP also?
I am not able to do so.
@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%
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
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.
Hello
Nice script but I need to do the same for downloading files with the use of a list. Is that also possible?
Karen
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.