Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Opening directorys of files and getting input

    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.

  • #2
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well if I understand the format rightly, you could use something like the following to parse the directory:

    Code:
    #!/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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •