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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with File move

Status
Not open for further replies.

wdellin

MIS
Feb 8, 2002
36
US
I have code that I'm trying to move matching files from one directory to another. When this runs it does not move the files to the directory I have on the move line. It moves to the directory I'm running from. Can someone more familiar with perl help?

Here is the code:
-----
#!C:\perl

my $filename="H:currdir\\zlist.lst";
my $line="";

if ( -e "$filename")
{
open(FILEOPENED, $filename) || die "Can't open file: $filename \n";
while ($line = <FILEOPENED>)
{
$filename=$line;
print "$line \n";
`MOVE "H:currdir1\\$line H:currdir2"`;
}
close(FILEOPENED);

}
else
{

print "no line was found.";
}
exit;
 
thanks, I tried
`MOVE "H:currdir1\\$line H:\\currdir2"`;
`MOVE "H:\\currdir1\\$line H:\\currdir2"`;


The results still go into the directory I'm running from. It's acts as if the second parameter does not exist.
 
Try this:

Code:
#!C:\perl

my $filename="H:currdir\\zlist.lst";
my $line="";

if ( -e "$filename")
{
    open(FILEOPENED, $filename)  || die "Can't open file: $filename \n";
    while ($line = <FILEOPENED>)
    {
        chomp($line);
        $filename=$line;
        print "$line \n";
        `MOVE "H:\\currdir1\\$line H:\\currdir2"`;
    }
    close(FILEOPENED);
    
}
else
{

    print "no line was found.";
}
exit;
 
figmatalan

I'm sorry but I do not see any difference in your move from the two I tried. I must be missing something, but I tried your code and had to remove the chomp for it to run. The results were the same, it did not move into the currdir2 directory.
 
... had to remove the chomp for it to run.
Sorry, but that's just crazy. [3eyes]
$line will have a newline on the end when it's read. If you don't chomp it, your command will have a newline in the middle. Try this.
Code:
    open(FILEOPENED, $filename)  || die "Can't open file: $filename \n";
    while ($line = <FILEOPENED>)
    {
        $filename=$line; [b]#this line serves no purpose, does it?[/b]
        print "$line \n";
        [b]my $cmd = "MOVE H:currdir1\\$line H:currdir2";
        print "$cmd\n";
        my $result = system($cmd);
        if ($result) {
            warn qq(Couldn't execute "$cmd"!\n);
            next;
        }[/b]
    }
    close(FILEOPENED);
This stores your MOVE command in a string and prints it out before trying to excute it. I think you'll see that the unchomped $line causes problems with the command.
 
mikevh and figmatalan

You are sooooo right. Not sure how I messed up the first script that figmatalan provided to me, but by using your result code I could see that the command was being split. I put the chomp back in and the output is going where it should go. Thank you both very much.

 
Glad you got it working. Actually, I suggest using the move and copy routines from the File::Copy module for moving/copying files. You might want to check it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top