Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Question

Options
  • 19-06-2002 5:40pm
    #1
    Registered Users Posts: 700 ✭✭✭


    Just started this yesterday,
    Im having trouble with copyng a file and pmoving it into a new folder.
    e.g. I want it to copy C:\temp\images\pic.gif and put it in C:\new\images

    can someone tell me how to do it?

    Ive tried
    copy("C:\temp\images\pic.gif ", "C:\new\images");

    but no joy...... me stoopid


Comments

  • Registered Users Posts: 1,103 ✭✭✭CodeMonkey


    That copy thing is a function in one of the standard modules/libraries right? Never used that before but I have a feeling it's failing because perl inteprets a bask slash infront of certain characters as special characters. FOr example, \n is next line, \t is tab, \\ means \. If you want c:\pic.gif then you need to type c:\\pic.gif
    copy("C:\\temp\\images\\pic.gif ", "C:\\new\\images");

    If that doesn't work then substitute the back slash for a forward slash
    copy("C:/temp/images/pic.gif ", "C:/new/images");

    If that still doesn't work then use the system command to call a native copy program to copy it ;)


  • Registered Users Posts: 700 ✭✭✭fuse


    Yep, should have mentioned that problem, I had it sorted by using forward slashes instead.
    Copy is in the library alright.

    So this system command, what exactly should I type to use it!?? whats a native copy program?

    thanks for the help...


  • Closed Accounts Posts: 6,601 ✭✭✭Kali


    system iirc, just initiates a shell for the underlying os and then executes the passed argument.

    so system("copy $filea $fileb");

    would use the copy program in MS-DOS as opposed to perls own file handling routines.


  • Registered Users Posts: 1,103 ✭✭✭CodeMonkey


    When I said "a native copy program" I just meant a copy program that's native to the OS environment you are using. XCOPY or COPY for MS-DOS and CP for unix etc.

    The system command is quite powerful as it basically lets you run anything you want. What kali said is correct and if you are running some external program that returns a value, you can also do the following and save it to a variable within your perl script.
    $errormessage = `xcopy` ;

    That's just like running xcopy with no arguments so you'll get the error message "Invalid number of parameters\n0 File(s) copied" save to the string $errormessage. Note that you need to use that funny ` symbol, it's below the ESC key, dunno what's it's called. I'm sure you'll find a use for this eventually cause I found it quite useful when I was playing around with perl.


  • Registered Users Posts: 4,433 ✭✭✭Gerry


    Imho, you are better off using perls built in commands where possible, rather than system commands. Perls commands are much less fussy about input ( so much less wierd errors ), and your code will work on any operating system perl works on.
    To do this, you need to include the copy function from the File module.

    So at the top of your file:

    Use File::Copy

    Then you can just use copy as you were trying to in the first instance.

    copy ("c:\\dira\\filea", "c:\\dirb\\filea") or die "Could not copy due to $!\n";


  • Advertisement
  • Closed Accounts Posts: 6,601 ✭✭✭Kali


    Originally posted by CodeMonkey
    you can also do the following and save it to a variable within your perl script.
    $errormessage = `xcopy` ;

    does that method will work in windows? I had thought explicit system calls where necessary.. then again `` might actually just run system() anyway? damn obfuscating perl :)


  • Registered Users Posts: 4,433 ✭✭✭Gerry


    nope, doesn't work on windows.


  • Registered Users Posts: 1,103 ✭✭✭CodeMonkey


    Originally posted by Gerry
    nope, doesn't work on windows.
    Errr, yes it does, though I just tested it and it's not working quite the way I described before. Using xcopy as an example cause it comes with windows (win2k anyway). If you run a perl script with the lines

    $error = `xcopy`;
    print "--> $error";

    It'll print out the following...

    D:\>perl test.pl
    Invalid number of parameters
    --> 0 File(s) copied

    The line "Invalid number of parameters" here is the errors being generated and printed to the console and not returned but the "0 File(s) copied" is the error message that's being returned so that's why only that got saved. In my original post, I thought both lines were being returned. Most people just use this trick it to catch boolean return states.
    And to show that it works in windows using a pure perl example. Create a script called print.pl and have the following lines in it...

    print "perl";
    print "syntax";
    print "is";
    print "odd";

    Then create a script called test.pl and have the lines...
    $message = `perl print.pl`;
    print "---> $message";

    When you run it, it should say...
    d:\>perl test.pl
    ---> perlsyntaxisodd


  • Registered Users Posts: 4,433 ✭✭✭Gerry


    Ok, you won't believe me now, but it didn't work on my home machine, however it does on my work machine. Both are win2k, a wee bit wierd.


  • Registered Users Posts: 700 ✭✭✭fuse


    Originally posted by Gerry

    Use File::Copy

    Then you can just use copy as you were trying to in the first instance.

    copy ("c:\\dira\\filea", "c:\\dirb\\filea") or die "Could not copy due to $!\n";

    Lovely! that did the job, thanks for the help people!


  • Advertisement
Advertisement