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

Hi Monks,

I'm trying to move log files in a remote directory (share) to a folder created in that share, but when I run the perl script, it only creates a the time stamped folder and does not move the log files in the share directory into the new one. What am I doing wrong????

Thanks for you help!

use POSIX; use File::Copy; my $WLpath = '\\\\10.1.1.5\\Weblogiclogs\\' . strftime( '%y%m%d%H%M', +localtime); ######################################## # Creating directory based on time stamp ######################################## { mkdir $WLpath or die "Couln't create $WLpath because $!($^E)\n"; } ############## sub Move_Logs ############## { my $logs = "\\\\192.168.2.5\\Weblogiclogs\\*.log"; my $target = $WLpath; move("$logs", "$target") or die ("move from $target to $logs was unsuc +essful because - $!($^E)\n"); }

Edit by dws for <code> tags

Replies are listed 'Best First'.
Re: moving logs files on a remote share.
by kabel (Chaplain) on Sep 19, 2002 at 06:07 UTC
    as far as i could read the post, it seems to me that you just don't call the Move_Logs subroutine!

    and you do not need to encase the mkdir call.
Re: moving logs files on a remote share.
by tadman (Prior) on Sep 19, 2002 at 07:07 UTC
    In looking over your code, I thought I'd just make a couple of suggestions that may help you with future programs:
    • You don't need to quote simple scalars to use them. For example: "$foo" can be simply $foo as the quotes are used to add things or combine scalars, such as "$foo$bar" or "${foo}s". The quotes are harmless in most cases, so it's nothing to worry about, but it looks odd.
    • Throwing things, like comments, around your subroutine declarations is highly unconventional. In this case, I think it degrades readability, since many are used to seeing them less decorated. You'll note that despite similar formatting, you have a case where one is a subroutine declaration, and the other is a code BLOCK.
    • Instead of using a constant, you might want to use a subroutine parameter. Something like this:
      sub move_logs { my ($logs, $target_path) = @_; # : (etc.) } # ... my $log_path = '\\\\10.1.1.5\\Weblogiclogs\\' . strftime( '%y%m%d%H%M', localtime); my $logs = "\\\\192.168.2.5\\Weblogiclogs\\*.log"; move_logs($logs, $log_path);
    • One of the reasons for this restructuring is that it keeps the distance between the declaration of the variable and its use to a minimum. This makes it easier to find out what it is, without having to scroll all over the place. Imagine there was 1500 lines between the top and bottom of your program, which is not uncommon.
    • You may have to expand your $logs file spec into a list of files using glob and iterate over it. Something like:
      foreach my $file (glob($logs)) { move($log, $log_path) || die "screaming"; }
Re: moving logs files on a remote share.
by Marza (Vicar) on Sep 19, 2002 at 06:34 UTC

    Dumb question for the others. It's late and I don't have my books....

    However, can you wild card with the move()?

    Looking up the online doc it says:

    The move function also takes two parameters: the current name and the intended name of the file to be moved. If the destination already exists and is a directory, and the source is not a directory, then the source file will be renamed into the directory specified by the destination.

    If possible, move() will simply rename the file. Otherwise, it copies the file to the new location and deletes the original. If an error occurs during this copy-and-delete process, you may be left with a (possibly partial) copy of the file under the destination name.

    You may use the "mv" alias for this function in the same way that you may use the "cp" alias for copy.

    It only mentions file....