Trying to incorporate variable data in system call?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • b0dhi
    New Member
    • Dec 2007
    • 1

    Trying to incorporate variable data in system call?

    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:

    [code=perl]
    my $newsubdir = $dir . $filedate;
    mkdir($newsubdi r,0777) || die "cannot mkdir";

    system("move C:\\test\\files \\*.log C:\\test\\files \\$newsubdir\\" );
    [/code]

    The $newsubdir doesn't seem to work inside the system call.. Can someone tell me what I'm doing wrong? Thanks in advance! :)
    Last edited by numberwhun; Dec 4 '07, 04:50 PM. Reason: add code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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:

    [code=perl]
    my $newsubdir = $dir . $filedate;
    mkdir($newsubdi r,0777) || die "cannot mkdir";

    system("move C:\\test\\files \\*.log C:\\test\\files \\$newsubdir\\" );
    [/code]

    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

    Comment

    • new1234
      New Member
      • Dec 2007
      • 2

      #3
      Hi, I'm actually having the same problem..
      when I run:
      Code:
      print "making directory ";
      print $directory;
      print "\n";
      
      mkdir($directory,0777) ||  print $!;
      I get the output 'No such file or directory'.
      I've tried this:
      Code:
      print "making directory ";
      print $directory;
      print "\n";
      $directory="\"".$directory."\"";
      print $directory;
      print "\n";
      mkdir($directory,0777) ||  print $!;
      Error = No such file or directory
      and this:
      Code:
      mkdir("$directory",0777) ||  print $!;
      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..

      Comment

      • new1234
        New Member
        • Dec 2007
        • 2

        #4
        I think it's because I may or may not have subdirectories that I want to make too - I caved and used
        [code=perl]
        system "mkdir -p $directory" || print $!;
        [/code]
        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!

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          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
          [code=perl]
          system "mkdir -p $directory" || print $!;
          [/code]
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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.

            Comment

            Working...