...

View Full Version : Opening directorys of files and getting input



ap3k
08-30-2002, 01:46 PM
Hi peeps,

I hope this is possible, or i have just wrote a script for nothing!

I have a directory on my server '/var/log/user' which contains files with random names (based on date/time and random numbers). Each one of these files has the following layout:



variable1 variable2 variable3 variable4 variable5


I need the code to pick up one of these files, open it, put the contents of the file into $variable1 $variable2 ..... and then once it has done that, delete that file, so the script can then move on to the next file until it has read them all in, and no more files exist.

I have no idea how to do this, so help would be much appreciated.

ap3k
08-30-2002, 11:55 PM
Update:

What i realy wanted the script to do was the following:

1. Open one of the files within the /var/log/user directory

2. The file will be in the format of 'username,domain,email,service,ip' (without quotes), so it needs to split these up into varables $username $domain $email $service $ip (so that beginers like me can make use of them)

3. Run a bit of code that uses those variables.

4. Close the open file, and then delete it.

5. Move on to next file within that directory until no more files exist.



Thanks



James

Mouldy_Goat
08-31-2002, 03:59 PM
Well if I understand the format rightly, you could use something like the following to parse the directory:


#!/usr/bin/perl

$directory = "/var/log/user";

# get the random files in the directory..
opendir(DIR, "$directory") || die "Couldn't open directory for reading: $!";
while ($file = readdir(DIR)) {
# this is assuming that the extension you've given the files is 'rnd'
push (@files, $file) if $file =~ /\.rnd$/i;
}
closedir DIR;

foreach (@files) {
open (FILE, "$_") || die "Couldn't open $_ for reading: $!";
chomp($line = <FILE>);
($username, $domain, $email, $service, $ip) = split(/,/, $line);
close FILE;
# here 'process' is a subroutine you've made to do step 3.
process($username, $domain, $email, $service, $ip);
unlink;
}

I haven't tested this code out yet, and I haven't used warnings or strict so it's a bit shoddy - but it shows you how to do it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum