inserting word DELETE in a log file

Hewlett-packard / 3100
February 5, 2009 at 06:14:24
Specs: Windows 2003, Intel 4 Gb
Hi,

I have the following requirements to perform FTP file moves between two FTP servers, basically doing (PULL/GET and PUSH/PUT) of files.

The FTP Transaction consists of 4 Actions:

1. Get listing of remote files from remote FTP server and save listing to a file eg. ftplistfiles.log)

2. Copy list of remote files to the local ftp (eg. /localftp/incoming) folder until next scheduled batch run

3. Backup copied files to a separate folder
(eg. /localftp/incoming/backup-datetimestamp )

4. Delete list of files REMOTELY after action 1,2,3 is successful.

I HAVE DONE ACTIONS 1,2,& 3 SO FAR:
====================================
Action 1. Completed via batch file with sample following commands. Please note I'm a novice and just started learning batch file so forgive my syntax for obvious errors. This is just sample code as there was more code than shown below:

LIST.BAT
------------
@echo off
cmd.exe /c ftp -i -s:listing.scr > ftplistfiles.log

exit
------------

The contents of the ftplistfiles.log after performing an 'ls' command remotely via ftp listing.scr

C:\>type ftplistfiles.log
remotefile1.txt
remotereport2.pdf


Now, my question is how do you insert/prepend using Windows/DOS batch scripting the following ftp lines such as 'open ftp.remote.com' etc... and the word DELETE before the filename in the list of files in the ftplistfiles.log ?

Desired Content of transformed ftplistfiles.log should be something like after issuing 'type' command below:

C:\>type ftplistfiles.log
open ftp.remote.com
username
password
DELETE remotefile1.txt
DELETE remotereport2.pdf


The transformed file ftplistfiles.log will be used as ftp input script to the ftp command to DELETE remote files. As in:

ftp -i -s:ftplistfiles.log


I even used something like:

FOR /F "tokens=2* delims=." %%A IN ('type ftplistfiles.log') DO echo (set _delete.%%A)

set _delete.=

But I was getting notdefined environment vars. _delete.<filename> even though the variable was set.

Any help or directions on how to do achieve ACTION4-delete remote files, is very much appreciated as I'm stuck on the last action.


See More: inserting word DELETE in a log file

Report •


#1
February 5, 2009 at 12:37:43
To turn this:

open ftp.remote.com
username
password
remotefile1.txt
remotereport2.pdf

into this:

open ftp.remote.com
username
password
DELETE remotefile1.txt
DELETE remotereport2.pdf
====================================
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! gtr 3 (
echo DELETE %%a >> newfile
) else (
echo %%a >> newfile
)
)
=========================================
To make it really spiffy for use as a ftp scr, add this as the very last line [outside the for loop]

echo bye >> newfile


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 6, 2009 at 15:44:53
G'day M2,

Thanks heaps for the code logic! You really saved my day. I did some modification as it was displaying the filename of the ftp log file instead of the its filename contents. I had to prime the input file to insert the first three ftp command lines before going into the FOR loop. But overall it did the transformation I wanted with just some minor bug to clean up. Again Thank you for your assistance. Btw, if you happen to visit Melbourne, Australia I'll be happy to tour you around and it's my shout.


Report •

Related Solutions


Ask Question