Connecting Tech Pros Worldwide Forums | Help | Site Map

Trying to incorporate variable data in system call?

Newbie
 
Join Date: Dec 2007
Posts: 1
#1: Dec 4 '07
I'm a newbie with Perl. I'm trying to make a system call to move some files to another directory, but the directory is based on a variable that I've created.

Here is a snippet of my code:

Expand|Select|Wrap|Line Numbers
  1. my $newsubdir = $dir . $filedate;
  2. mkdir($newsubdir,0777) || die "cannot mkdir";
  3.  
  4. system("move C:\\test\\files\\*.log  C:\\test\\files\\$newsubdir\\");
  5.  
The $newsubdir doesn't seem to work inside the system call.. Can someone tell me what I'm doing wrong? Thanks in advance! :)

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#2: Dec 4 '07

re: Trying to incorporate variable data in system call?


Quote:

Originally Posted by b0dhi

I'm a newbie with Perl. I'm trying to make a system call to move some files to another directory, but the directory is based on a variable that I've created.

Here is a snippet of my code:

Expand|Select|Wrap|Line Numbers
  1. my $newsubdir = $dir . $filedate;
  2. mkdir($newsubdir,0777) || die "cannot mkdir";
  3.  
  4. system("move C:\\test\\files\\*.log  C:\\test\\files\\$newsubdir\\");
  5.  
The $newsubdir doesn't seem to work inside the system call.. Can someone tell me what I'm doing wrong? Thanks in advance! :)

Have you tried doing a print of the $newsubdir variable to ensure that it is getting set correctly? We don't have the rest of your code and I don't like making assumptions.

Regards,

Jeff
Newbie
 
Join Date: Dec 2007
Posts: 2
#3: Dec 20 '07

re: Trying to incorporate variable data in system call?


Hi, I'm actually having the same problem..
when I run:
Expand|Select|Wrap|Line Numbers
  1. print "making directory ";
  2. print $directory;
  3. print "\n";
  4.  
  5. mkdir($directory,0777) ||  print $!;
  6.  
I get the output 'No such file or directory'.
I've tried this:
Expand|Select|Wrap|Line Numbers
  1. print "making directory ";
  2. print $directory;
  3. print "\n";
  4. $directory="\"".$directory."\"";
  5. print $directory;
  6. print "\n";
  7. mkdir($directory,0777) ||  print $!;
  8.  
Error = No such file or directory
and this:
Expand|Select|Wrap|Line Numbers
  1. mkdir("$directory",0777) ||  print $!;
  2.  
which creates the directory ./$directory ...
As you can see I've been checking that the variable prints out correctly. (Output for the first snippet of code is: "making directory ./testdir/testdir2/")
Any help would be really appreciated..
Newbie
 
Join Date: Dec 2007
Posts: 2
#4: Dec 20 '07

re: Trying to incorporate variable data in system call?


I think it's because I may or may not have subdirectories that I want to make too - I caved and used
Expand|Select|Wrap|Line Numbers
  1. system "mkdir -p $directory" ||  print $!;
  2.  
which works very nicely.. I'd still be interested in any alternative solutions as this seems a bit clumsy to me.
Cheers
p.s. i'm also struggling to remove the ./$directory that i created above (oops). Command line returns "directory: Undefined variable" I've tried hashing out the $
rm \$directory
and
rm "$directory"
--> same result.. d'oh!
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#5: Dec 20 '07

re: Trying to incorporate variable data in system call?


Quote:

Originally Posted by new1234

I think it's because I may or may not have subdirectories that I want to make too - I caved and used

Expand|Select|Wrap|Line Numbers
  1. system "mkdir -p $directory" ||  print $!;
  2.  
which works very nicely.. I'd still be interested in any alternative solutions as this seems a bit clumsy to me.
Cheers
p.s. i'm also struggling to remove the ./$directory that i created above (oops). Command line returns "directory: Undefined variable" I've tried hashing out the $
rm \$directory
and
rm "$directory"
--> same result.. d'oh!

Question, are you just wanting to print the error if it doesn't work? It is more customary to use the die() function for that and not just a print statement. That way, processing can be stopped at point of failure.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Dec 20 '07

re: Trying to incorporate variable data in system call?


this looks wrong:

$directory="\"".$directory."\"";

why do you want to add double-quotes around $directory? If $directory were: foo/bar it would equal "foo/bar" after being processed by the above line.
Reply