Alex_T2 has asked for the wisdom of the Perl Monks concerning the following question:

hi all, ive this problem, i need to search 3 files, that can be located on 1 of 5 drives, in 3 diferent dirs on that drives, the files are always named OK.txt , error.txt and unknown.txt so i want to programm a script that searchs the drives for the files ( i think with dir into an array), the scripts needs to check if the file that is found is from today other wise it shoud search the other drives if nothing it found then the file from yesterday after finding the file it should rename the file with the curent date (done with localtime and rename i think) then it shoud copy the 3 files to a specific folder. so my problem i dont even have a clue how i can find out the date of a file. the rename and copy thing is no problem i think so far ;D but for me the search of the file and then when a file is found the check when it was created. thx for help PS: its for windows ,D
  • Comment on Finding files , rename then and copy them

Replies are listed 'Best First'.
Re: Finding files , rename then and copy them
by planetscape (Chancellor) on Nov 28, 2005 at 06:27 UTC
Re: Finding files , rename then and copy them
by GrandFather (Saint) on Nov 28, 2005 at 09:08 UTC

    The following should get you started:

    use strict; use warnings; use File::Copy; my @drives = qw(c: d: e: g: j:); my @dirs = qw(/dir1a/partb/partd /dir2/part1/part2); my @files = qw(OK.txt error.txt unknown.txt); my $bestPath; my $bestTime; for my $drive (@drives) { for my $dir (@dirs) { #Assume first file is sufficient test to find all files #That is, all the files are in the same dir and of the #same age. my $path = "$drive$dir$files[0]"; print "Trying $path\n"; next if ! -e $path; next if defined $bestPath and -M $path < $bestTime; $bestPath = "$drive$dir"; $bestTime = -M $path; } } die "No suitable files found" if ! defined $bestPath; #At this point $bestPath contains the path to the files. #Use rename oldname, newname to rename the files. #The time the program started can be got from $^T. #Use copy("from", "to"); from File::Copy to copy the files.

    DWIM is Perl's answer to Gödel
Re: Finding files , rename then and copy them
by tphyahoo (Vicar) on Nov 28, 2005 at 11:01 UTC
    I echo the above, and add also that for file munging, another very useful module is File::Basename.

    If you're using a regex for separating the file name from the full file path, eg, stripping out the directory, like I was for a while before I realized about F::B, then you need this module. Hope this helps!

Re: Finding files , rename then and copy them
by Alex_T2 (Initiate) on Nov 30, 2005 at 06:44 UTC
    thx all of u for ure help. my code looks quit the same like urs grnadfather instead that i use onla 1 arrya for the path and rename files on drive first and then copy the renamed wants ive also made somthing with a check of the last file access but with creationtime ive nothing jet ;D and still searching / understanding