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

I have run into a weird issue with moving files. When attempting to move a file from one directory to another.

With in my file system I have the folders:

/mnt/fds-phx/docTmp/coded/ (the from folder) /mnt/fds-phx/plsTrans/98E98657-D4A3-6B6B-EF2990B199D75814/tmp (the to folder)

Now the issue that I am having is when I try to move a file from /mnt/fds-phx/docTmp/coded/ to the directory /mnt/fds-phx/plsTrans/98E98657-D4A3-6B6B-EF2990B199D75814/tmp the file is moved, but the file is moved to /mnt/fds-phx/plsTrans/98E98657-D4A3-6B6B-EF2990B199D75814 instead of /mnt/fds-phx/plsTrans/98E98657-D4A3-6B6B-EF2990B199D75814/tmp

When using the command line to move the file it works perfectly fine. When I use just a string instead of a dynamic string, the move works perfectly fine.

Here is my updated code.

# read trans barcode $primer = Util::Util::trim(Util::Util::fileDecode($transFile)) +; # set transaction folder path my $transPath = $wDir . "plsTrans/" . $primer . "/tmp/."; system("mv $transFile $transPath"); if(-d $transPath){ system("mv $transFile $transPath"); }

Replies are listed 'Best First'.
Re: Moving Files
by hexcoder (Curate) on Aug 07, 2008 at 18:13 UTC
    I assume the program has directory $wDir as its current working directory, and $transFile starts with the directory part "docTmp/coded/".

    If the target directory .../tmp is not present, the file to be moved will be placed in the parent directory instead and will be renamed to tmp. So I would make sure beforehand that the target directory exists.

    For the creation of directories of any depth, use mkpath() from core module File::Path. For moving you could use Perl's File::Copy module. It has a portable move() function. I would do it like this (untested):

    # read trans barcode $primer = Util::Util::trim(Util::Util::fileDecode($transFile)); # set transaction folder path my $transPath = "plsTrans/" . $primer . "/tmp/"; use File::Path qw(mkpath); # create the target directory if there is none if (!-d $transPath) { mkpath $transPath or die "cannot create target directory $transPat +h:$!\n"; } use File::Copy qw(move); move($transFile, $transPath . $transFile) or die "move to $transPath failed: $!";
      I can verify that the ../tmp folder will always be available. Right now things are very static. I have manually created all the directories and given proper permissions(777) just for testing and ease of development.

      When using move(..), I wind up getting the error move to /mnt/fds-phx/plsTrans/9DFA109A-B562-1BEA-064686FE31CA2F93/tmp/ failed: Is a directory at Lib/PLSParse.pm line 130.

      The output I get from the paths I have created is below.

      $transFile = /mnt/fds-phx/docTmp/coded/1.tif $transPath = /mnt/fds-phx/plsTrans/9DFA109A-B562-1BEA-064686FE31CA2F93 +/tmp/
Re: Moving Files
by jethro (Monsignor) on Aug 07, 2008 at 18:00 UTC

    The problem is probably a "/n" or space in $primer. Just insert a print "(",$primer,")\n"; after setting $primer and see if the braces enclose your transfile number without any space left.

    UPDATE: Also the '.' in "/tmp/." is superfluous. Does still work but it is rather unusual (like doing $f= 5+0;), so I just wanted to point that out.

      The brackets do enclose the transfile number with out any space left. I even added chomp just to be 100% sure.

      Here is my updated code

      # read trans barcode $primer = Util::Util::fileDecode($transFile); chomp($primer); print "(",$primer,")\n"; # set transaction folder path my $transPath = $wDir. "plsTrans/$primer/tmp/"; if(-d $transPath){ print "Transpath : " . $transPath . "\n"; print "FileName : ". $transFile . "\n"; print "Cmd : >cp $transFile $transPath<\n"; system("mv",$transFile,$transPath); }
        You might do print length($primer) to see if any invisible chars are in that string.

        UPDATE: Or use something like this to check for unusual ingredients:  if (not $primer=~/^[A-Z0-9-]+$/) print "AAAARGH\n";

Re: Moving Files
by toolic (Bishop) on Aug 07, 2008 at 18:41 UTC
    If you have symbolic links anywhere, that could explain the strange behavior.
    > mkdir a b > touch a/file > cd b > ln -s . tmp > cd .. > mv a/file b/tmp > ls b file tmp
      If that was the case he would also see the file in tmp
        That is the case, plsTrans is a symlink I made for testing. At this point, I am not sure what the issue is.