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

My dilemna is that I have a number of text files in a directory, say C:\Text_Files. They all follow the same naming convention, mlb_boxscore$124728.txt, mlb_boxscore$429722.txt, and so on. Right now, I have code written that sorts the file with the greatest numerical value. I am trying to move this file to another directory and rename it as soon as it gets there. So far, I can only get the sort to work and I am unable to move the file to the new directory, and am not sure how to rename files, so if there are any suggestions I would greatly appreaciate them. Below is what I have written out so far, it's weak to say the least.
my $base_dir = "C:/Text_Files"; my $new_dir = "D:/Test"; opendir (DIR, "C:/Text_Files") || die "Could not open data directory\n +"; my ($top_file) = reverse sort readdir(DIR); closedir (DIR); use File::Copy; move($base_dir.$top_file, $new_dir.$top_file);

Replies are listed 'Best First'.
Re: Moving a Text File
by azatoth (Curate) on May 16, 2001 at 19:42 UTC

      FYI, that won't work in this case since rename() usually doesn't work across file systems (and Win32 isn't an exception), and the original code was trying to go across file systems.

              - tye (but my friends call me "Tye")
(tye)Re: Moving a Text File
by tye (Sage) on May 16, 2001 at 21:24 UTC

    Your problem is that $base_dir.$top_file expands to 'C:/Text_Filesmlb_boxscore$124728.txt', which doesn't exist. A fix is probably as simple as: move($base_dir.'/'.$top_file, $new_dir.'/'.$top_file); though you could also consider using File::Spec/File::Spec if you wanted to make your code more portable (probably not warranted in this specific case).

            - tye (but my friends call me "Tye")