Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automating FTP with Batch Files or Scripts

Status
Not open for further replies.

jayvo

MIS
May 24, 2001
27
US
I am very new to scripting in Windows2000 but need help trying to automate an FTP process that needs to be done everyday sending one file. The file's name changes everday with the month date and year so i would need to incorporate variables. Any suggestions. I know how ftp can be automated but I'm stuck on getting the correct file ftp-ed automatically every day. Also, I was looking into CRON for scheduling the process rather than task scheduling. Please help


Thanks


Jayvo
 
I don't think you can automate FTP via a batch file running off a DOS prompt.

At least i have never made it work, it stops at the FTP prompt and then continues after you BYE from the FTP session.
 
Sounds similar to a few things that I have to do and have yet to find a solution. If you find anything more about this, please let me know and I will do the same if I find out any more.
 
WS FTP Pro has a scripting functionality in it

this appears to be the only route
 
Hey Jayjo,

I've been doing quite a bit of this recently in our conversion from unix to win2k. It's actually not too hard once you get it going. Are you trying to ftp out just one specific file (i.e. log file) or is this a dynamically changing file name? If you need to specify the filename, that's workable too. Here's a batch script (i modified it a bit to try to suit your situation more) that you can try out. We use this to backup to a remote server and it works well. you will need to pass in parameters to get it going. lemme know if this is what you are looking for:

:: Script to ftp single file to remote server
:: It needs:
:: $1 = host
:: $2 = directory to ftp to
:: $3 = full path of file to be sent

@ECHO OFF
SETLOCAL
SET username=<your username>
SET PASSWD=<your password>

:: check # of arguments
IF [%1] ==[] (ECHO Wrong number of arguments
goto done)
IF [%2] ==[] (ECHO Wrong number of arguments
goto done)
IF [%3] ==[] (ECHO Wrong number of arguments
goto done)

:: check directory to use is of the correct form then send
:: use these 3 lines if you want to ensure
SET correctstr=<first three letters of directory...i.e. /ho for /home>
SET passedstr=%2
:: you can change the 0,3 here to be sensitive to more characters.
:: ~0,3 means look at first 3 from the beginning
SET passedstr=%passedstr:~0,3%

:: creates temp file with ftp script rdy. deletes when it's done. add in other command lines as needed.
IF %passedstr% EQU %correctstr% (
> d:\scripts\ftp_script echo %username%
>> d:\scripts\ftp_script echo %password%
>> d:\scripts\ftp_script echo ascii
>> d:\scripts\ftp_script echo prompt
>> d:\scripts\ftp_script echo cd %2
>> d:\scripts\ftp_script echo mput %3
>> d:\scripts\ftp_script echo bye

ftp -s:d:\scripts\ftp_script %1
del d:\scripts\ftp_script
)ELSE (
ECHO Incorrect Directory: %2
goto done
)
:done
ENDLOCAL
 
Hey thanks for all the help, Im back on this now and really need to get this going. I will try to play with this script. Yes, the file i need to ftp everday is a diff file. Basically it't a log file and it is in the format wms2003mmdd_acess.log. The only thing that changes is the mmdd of course. I ftp every afternoon the log from the previous day so the script will have to be a -1 date format i think. Thanks again for the help, i'll keep hacking away at it


Jayvo
 
The FTP command shell client in Windows 2000 can do this. However, if the file changes every day it will be a problem. If it is in its own directory the whole directory can be uploaded by using wildcards.

If you create a script (i.e. script.txt) with these contents (without the <begin> and <end> tags:
<begin>
open ftp.microsoft.com
anonymous
test@test.com
binary
lcd c:\download
cd reskit/y2kfix/x86
prompt off
mget *.*
bye
<end>

Make sure you create a c:\download folder, and run this at the command prompt:
ftp -s:script.txt

This will snag the files for the y2k fix from microsoft's site. (4 files, ~36k).

Of course, you would have to change it to place files on an ftp slightly.

I don't think this will work unless you synchronize the whole logs directory. But it's a start.

Daniel.
 
Okay,
If you want to set the filename to include just the mmdd to vary, here's what you gotta do:

SET MON=%DATE:~4,2%
SET DAY=%DATE:~7,2%
SET FILE=wms2003%MON%%DAY%_acess.log

This will basically use the environment variable 'DATE' and, in the case of MON, will skip the first 4 characters, then take the next two. You can do all kinds of things with extraction of variables. see for a full description of things you can do to variables.

hope that helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top