FTP script to get yesterdays log file

February 12, 2010 at 06:38:26
Specs: Windows XP
I need to create a batch file that will do the following

1: FTP to server
2: Switch local directory
3 Switch remote directory
4: Put a log file in the remote directory

I need to select a specific log, so if the script runs today , then I will need it to transfer the log from yesterday.

The logs in the directory are iis log files. They have the following format

exYYMMDD.log

I've tried looking how I can do this, and Im not having much luck. Could anyone explain very slowly how I could achieve this?

I've got as far as creating the batch file, and I can get it to log in and change directories.


See More: FTP script to get yesterdays log file

Report •

#1
February 12, 2010 at 07:03:02
Please post names of files for:

today
yesterday
day before yesterday


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 12, 2010 at 07:30:21
Today
ex100212.log

yesterday
ex100211.log

day before yesterday
ex100210.log

Thanks


Report •

#3
February 12, 2010 at 08:10:42
@echo off & setLocal enableDELAYedexpansion

pushd e:\mylogs

for /f "tokens=* skip=1" %%f in ('dir/b/o-n ex*.log') do (
set yest=%%f
goto :done here
)
:done here

 > # echo o mysite.com
>> # echo user
>> # echo pass
>> # echo cd /incoming
>> # echo put !yest!
>> # echo bye

ftp -s:#


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
February 13, 2010 at 07:42:39
> for /f "tokens=* skip=1" %%f in ('dir/b/o-n ex*.log')
> do (

Are you assuming the file of yesterday is the second last updated file in that directory, named "ex*" ? If yes ... that is a very dangerous assumption, so easy to fail ...


Report •

#5
February 13, 2010 at 07:45:10
User, it can be done in a thorough way, but you first need a tool that says, today I am:

- this day
- this month
- this year

Not just :
date /t
echo %date%

Do you have such a tool ?


Report •

#6
February 13, 2010 at 07:58:15
Thank you for the script Mechanix2go :)

I have no tool to say what the date is. Yesterdays log file should always be the second last updated file in this particular directory. All this directory has in are log files. I can understand how it could go wrong though.

I don't know if you could incorporate vbscript into this particular batch script to solve the issue? Sadly I don't know a lot of vbscript. I have a grounding in perl and bash.


Report •

#7
February 13, 2010 at 12:16:00
Don't need VBscript to mess with dates, but the problem is that date formats are very loose on Windows ...

Report •

#8
February 15, 2010 at 10:23:16
I don't know if you could incorporate vbscript into this particular batch script to solve the issue?
Well I could, but I do hate mixing scripting languages together. It'd end up looking like this:
echo wscript.echo Right(DatePart("yyyy", Date - 1), 2) _ >a.vbs
echo ^& DatePart("m", Date - 1) ^& DatePart("d", Date - 1) ^& ".log" >> a.vbs
for /f %%a in ('cscript //nologo a.vbs`) set var=%%a


Report •

#9
February 15, 2010 at 13:53:13
i just wanted to fix all the mistakes i made in the original, so i'm reposting
this to relieve my conscience (and generate new and more interesting mistakes!):
:: generate current date then decrement by 1 day and normalize output.
::With no "setlocal", the script can be called from another script which can
::access the var.s via the env.table
@goto :AAA
e 100 8A 1E 82 00 80 CB 20 B4 2A CD 21 80 FB 77 74 10
e 110 88 C8 80 FB 79 74 09 88 F0 80 FB 6D 74 02 88 D0
e 120 B4 4C CD 21 03 B4 4C CD 21
RCX
30
W
Q

:AAA
@echo off
if not exist dmy.com debug dmy.com < %0.bat >nul 2>nul
if not defined dlm set dlm=-
if "%1"=="" (set sub=1) else (set sub=%1)
dmy d
set day=%errorlevel%
dmy w
set dofw=%errorlevel%
dmy m
set month=%errorlevel%
dmy y
set /a year=%errorlevel%+1792
for /L %%a in (1 1 %sub%) do call :subdate
if "%month:~1%"=="" set month=0%month%
if "%day:~1%"==""set day=0%day%
set ddate=%month%%dlm%%day%%dlm%%year%
echo.%ddate%
goto :eof

:subdate
set /a day-=1
if %day% equ 0 (
set /a day=31
set /a month="(%month%+10)%%12+1"
call %month% 2>nul)
echo.%month% %day% %year%
pause
GOTO :EOF
:4
:6
:9
:11
set /a day-=1
goto :eof
:12 set /a year-=1
goto :eof
:2
set /a day=28
set /a leap="year%%4"
if %leap% equ 0 set /a day+=1


Report •

#10
February 17, 2010 at 02:52:13
Thanks for the vbscript code nbrane. I'll have to test it out.

I used the original batch code with success. I was just
wondering though how I would change it if I needed to upload
log files from different directories.

So after I upload the log file from the first dir I want to change
the local dir to another one, select yesterdays log file and
upload it to a new directory.

I don't know if it's possible to do an lcd and then run the code
to select the file again. I'm trying to modify the script but I'm
not having much luck.


Report •

#11
February 17, 2010 at 03:10:42
I think using lcd will be messy. Primarily because you need to finish the ftp, then do your cd or pushd and get the next file name.


=====================================
Helping others achieve escape felicity

M2


Report •

#12
February 17, 2010 at 04:52:41
Oh right. So really it would be best to create seperate batch files that will ftp to the server ?

Just didn't like the idea of ftping in and out repeatedly. I'll probably have to do it 3/4 times .



Report •

#13
February 17, 2010 at 05:03:29
You probably don't need multiple BATs.

Think of this: [pseudo-code]
---------------------------------

for /f "tokens=* delims= " %%a in (myfile) do (
set local=%%a
call :scriptmaker
ftp -s:#
)
goto :eof

:scriptmaker
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#14
February 17, 2010 at 08:51:14
seems like you need to have a list of local dir.names and a list of remote dir.names (unless they're the same, which would be nice). Once you have the "date-name" determined, you could just keep doing "cd" (remote) and put the local dir\date-name up. using M2's script, (remotes same name as locals)
:: init: build ftpscript base, determine filename (yesterday)
@echo off & setLocal enableDELAYedexpansion
> # echo open mysite.com
>> # echo user
>> # echo pass
pushd e:\mylogs
for /f "tokens=* skip=1" %%f in ('dir/b/o-n ex*.log') do (
set yest=%%f
goto :done here
)
:done here
popd
:: main loop: ftpscript put each localdir\yest.log to remdir/yest.log,
:: based on a local list "remdirs"
for /f "tokens=*" %%a in (remidrs) do (
>> # echo cd /%%a
>> # echo put %%a\!yest!
>> # echo cd ..
)
:: execute the ftp w/script
>> # echo bye
ftp -s:#

if the local dirs are not same name as remotes, you could either build an array of localnames or make the "list file" so it contains both names on one line per pair:
localdir1,remotedir1
localdir2,remotedir2
then the main loop would be something like:
for /f "tokens=1,2 delims=," %%a in (dirs.lst) do (
>> # echo cd /%%b
>> # echo put %%a\!yest!
>> # echo cd ..


Report •

Ask Question