General
cancel
Showing results for 
Search instead for 
Did you mean: 

I need a perl script to delete files on the ftp server older then 1 week

SOLVED
Go to solution
'chris'
Super Advisor

I need a perl script to delete files on the ftp server older then 1 week

hi

I need a perl script to delete all files
beginning with cm* and older then 1 week
via ftp, on the ftp server.

kind regards
chris

9 REPLIES
Olivier Drouin
Trusted Contributor

Re: I need a perl script to delete files on the ftp server older then 1 week

no need for perl

find / -name "cm*" -mtime +7 -exec rm {} \;

or something like that... ( man find ).
'chris'
Super Advisor

Re: I need a perl script to delete files on the ftp server older then 1 week

but it's external ftp server
and I can connect only via ftp.
Olivier Drouin
Trusted Contributor

Re: I need a perl script to delete files on the ftp server older then 1 week

Much easier would be to open ssh and remotely run the command I gave you over ssh.
'chris'
Super Advisor

Re: I need a perl script to delete files on the ftp server older then 1 week

on linux yes,
but the external server is AS400
and doesn't understand ssh

I CAN CONNECT ONLY VIA FTP
and no other way.

so I need a script

regards
chris
labadie_1
Honored Contributor

Re: I need a perl script to delete files on the ftp server older then 1 week

record in a file a session doing only

ls -al

then parse the file, and create a command file deleting only the files matching yout criteria.

ans issue the command file
Paul Cross_1
Respected Contributor

Re: I need a perl script to delete files on the ftp server older then 1 week

sounds like a job for expect, run from cron.
Elmar P. Kolkman
Honored Contributor

Re: I need a perl script to delete files on the ftp server older then 1 week

What you can do is to open the ftp connection in a perl script using open2, which means you have a two way connection to the shell command, in this case ftp.
Login, go the the directory and do ls -lt cm*. There find the file that is old enough to delete and then use all filename and all older to delete.

First check by running from the command line if your AS/400 ftp server supports the ls -lt and then I might give it a start by writing something for you... Or perhaps Procura sees this thread and does it...
Every problem has at least one solution. Only some solutions are harder to find.
'chris'
Super Advisor

Re: I need a perl script to delete files on the ftp server older then 1 week

hi elmar

what do you mean with exactly
" old enough to delete with ls -lt cm* " ?

how old files will be deleted ?
this is very important for me to know.

kind regards
chris
Elmar P. Kolkman
Honored Contributor
Solution

Re: I need a perl script to delete files on the ftp server older then 1 week

What you need to do is in the perl script find, in the output of 'ls -lt cm*', find the first file that's older then the 1 week limit you set and delete that file and everyone that comes next. In pseudo perl, it would become something like:

my found_delete=0;
my delete_from=currentdate - 1week;
while (line=readline(FI)) {
if (!found_delete) {
pd=parse_date(line);
if (pd < delete_from) {
found_delete=1;
}
}
if (found_delete) {
writeline(FO,"DEL ".getfile(line));
}
}
Every problem has at least one solution. Only some solutions are harder to find.