Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mv command 1

Status
Not open for further replies.

bluedragon2

IS-IT--Management
Jan 24, 2003
2,642
0
0
US
I have a strange problem happening that hopefully someone can explain why:

If I run this code:

$a1 = "vcx";
$a2 = "xxx.xxx.xxx.xxx";
$a3 = "01.09.04-14.02.50";
$b = "/tmp/tmpdir/";
$c = "/tmp";
system ("mv", "$c$a1\@$a2\@$a3", "$b");

It will run correctly and move the file vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50 from /tmp to /tmp/tmpdir/

Now, if I do it this way:

$line = "vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50";
@comb = split(/\@/,$line);
$a1 = $comb[0];
$a2 = $comb[1];
$a3 = $comb[2];
$b = "/tmp/tmpdir/";
$c = "/tmp/";
system ("mv", "$a1\@$a2\@$a3", "$nd");

I get an error:

mv: cannot access /tmp/vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50

Does anyone know the reason for this?

Thanks,




[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
One thing I noticed was that the top one includes the directory name in the mv, but the bottom one doesn't. See change in red:
Code:
$line = "vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50";
@comb = split(/\@/,$line);
$a1 = $comb[0];
$a2 = $comb[1];
$a3 = $comb[2];
$b = "/tmp/tmpdir/";
$c = "/tmp/";
system ("mv", "
$c
Code:
$a1\@$a2\@$a3", "$nd");
 
Just an omission on my part, the actual code does have it in there.



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
the $nd is also $b on the actual code. Cut and paste blue's :)

The line is:

system ("mv", "$c$a1\@$a2\@$a3", "$b");



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
I do believe I figured it out....

The line in the one that does not work:

$line = "vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50";

which is actually a line being read from a file as that.

If I do a:

$line = "vcx\@xxx.xxx.xxx.xxx\@01.09.04-14.02.50";

instead of reading it in from the file, it works...

Now another question, is it possible for me to read into an array that string in such a manner that it will work with the code?






[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
If $line is being read from a file, are you chomping it after you read it? (You should.)

After running your first example which works, the file has been moved, so it no longer exists in its former location. Are you moving it back before trying your second example which does not work? (If not, the file is inaccessible because you just moved it.)

Yes, you could have the filename in an array rather than a simple string, but why would you want that? That's just making life more complicated than it needs to be.

I would suggest using the File::Copy module for copying and renaming files, instead of system calls. (More portable.)
 
Thanks for the comment, here is the process:

I have a text file with a list of file names like:

vcx@xxx.xxx.xxx.xxx@01.09.04-14.02.50

My intentions are to read the file name, determine the month in the file name and move it to the appropiate directory.

I believe the @'s in the file name are causeing me the problems.



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
Here's something that may help.
After suggesting File::Copy, I'm unable to think of a substitute for "system(mkdir ...)" if the directory doesn't already exist. I'm sure there must be one, but don't have time to look for it right now.
Code:
#!perl

use strict;
use File::Copy;

my $from_dir = "c:/work/";
my $to_dir = "${from_dir}tmp/";
my $testfile = "${from_dir}testfile.txt";
my $filename;
my $month;
my $monthpos = 0; #assuming date in US mm.dd.yy format. Use 1 for Euro dd.mm.yy format
my $newdir;

my $result;

open(TESTFILE, $testfile) ||
    die qq(Can't open "$testfile" for input\n);

while (defined($filename = <TESTFILE>))
{
    chomp $filename;
    if (! -f &quot;${from_dir}$filename&quot;)
    {
        warn qq(&quot;${from_dir}$filename&quot; doesn't exist or isn't a regular file\n);
        next;
    }

    $month = (split /\./, (split /@/, $filename)[2])[$monthpos];
    $newdir = &quot;${to_dir}${month}/&quot;;

    if (! -d &quot;$newdir&quot;)
    {
        $result = system(qq(mkdir $newdir));
        
        # mkdir returns 0 for success, 1 for failure
        die qq(Couldn't create directory &quot;$newdir&quot;\n)
            if $result;
    }

    $result = move(&quot;${from_dir}$filename&quot;, &quot;${newdir}$filename&quot;);
    
    # File::Copy::move returns 1 for success, 0 for failure
    warn qq(Couldn't move &quot;${from_dir}$filename&quot; to &quot;${newdir}$filename&quot;)
        unless $result;
}
 
I got mine to work by opening the reference file instead of doing a `cat and putting the result in an array.

I also tried your script and it worked as well, Thank you very much.

I guess the original way I tried it wasn't to pleaseing to perl with the @'s in the file name. That is the only reason I can come up with...

Thanks again



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
Thanks for the star, Blue.

I realized after posting that Perl has its own built-in mkdir function, and that's what I should've used instead of a system call.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top