prab2112 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl $path = ""; $t = "dir C:\\ProgramData\\Test1\\Test Folder\\" ; $out = `$t`; print $out;
After running the above code it gives me "The system cannot find the file specified." error. what's wrong with the code?

Replies are listed 'Best First'.
Re: What is wrong with this code?
by jellisii2 (Hermit) on Sep 08, 2014 at 11:29 UTC
    While only obliquely involved, Use strict and warnings.

    Also, when running system commands on Win32, you have to quote paths in spaces. If I couldn't use any of the wonderful perl faculties to get a list of items in a folder, I'd have written $t = 'dir "C:\ProgramData\Test1\Test Folder"';, or renamed my target to not have spaces in it. :)

      Good catch! :)

      ... but shouldn't the failing path (ie w/o " Folder") be shown in the error msg ? At least in $! ?

      I have currently no win system to test..

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        That would be the sane thing to do. On Windows 8:
        C:\>perl -v This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x +86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2012, Larry Wall Binary build 1603 [296746] provided by ActiveState http://www.ActiveSt +ate.com Built Mar 13 2013 11:29:21 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. C:\>perl -e "print `dir \C:\Program Files`; print \"\nerror: $!\n\" " File Not Found Volume in drive C is Windows8_OS Volume Serial Number is 78A2-8234 Directory of C:\ Directory of C:\ error: C:\>perl -e "print `dir \"C:\Program Files\"`" Volume in drive C is Windows8_OS Volume Serial Number is 78A2-8234 Directory of C:\Program Files 07/08/2014 09:31 AM <DIR> . 07/08/2014 09:31 AM <DIR> .. [redacted directory list] 0 File(s) 0 bytes 45 Dir(s) 137,746,575,360 bytes free C:\>

        Windows 7 does much the same.

        But you do bring to mind a good point: Is perl in the path?

Re: What is wrong with this code?
by marto (Cardinal) on Sep 08, 2014 at 09:05 UTC

    I get that too, because the path "c:\ProgramData\Test1\Test Folder" does not exist on my system. Perhaps you should check that something exists before being surprised when you can't get a directory list? See -X.

Re: What is wrong with this code?
by Anonymous Monk on Sep 08, 2014 at 09:00 UTC
Re: What is wrong with this code?
by LanX (Saint) on Sep 08, 2014 at 09:05 UTC
    > What is wrong with this code?

    I think probably "The system cannot find the file specified"?

    Did you check your path ?

    Please note that Perl has it's own commands to read directories.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      yes, the path is correct. As i am new so i wasn't aware that perl has it's own commands to read directories. i was able to do using that. Thanks
Re: What is wrong with this code?
by Anonymous Monk on Sep 08, 2014 at 10:19 UTC

    My guess as to why it doesn't work would be the space in the pathname. As others have mentioned, there are "safer" and more portable ways to handle filenames - one more not yet mentioned is Path::Class.

Re: What is wrong with this code?
by locked_user sundialsvc4 (Abbot) on Sep 09, 2014 at 00:50 UTC

    The first step would have been to, say:

    print "T is '$t'\n";

    In other words, “never assume” that you actually know what command-string is being executed.   Tell Perl to print the content of the string, so that you can be certain that it is what you expect it to be.   Then, using the command-line shell, chase down any remaining issues.

    In this case, I daresay that the real problem is that there is a space in the path.   So, if you copy-and-paste the literal string into the command line shell of Windows, you will find that it ... doesn’t work there, either.   The shell sees the filename Test and has no idea what to do with “the next word of the command, which is Folder.”   Oops.   You need to present a command which encloses the pathname in double quotes.   Say ...

    $t = "dir \"C:\\ProgramData\\Test1\\Test Folder\""

    Notice how I have used \" to specify the inclusion of a literal quote-mark.   So, the command that is presented will be:

    dir "C:\\ProgramData\\Test1\\Test Folder"
    ... which the shell will now parse correctly.

      The first step would have been to, say:

      print "T is '$t'\n";

      In other words, "never assume" that you actually know what command-string is being executed. ...

      $t = "dir \"C:\\ProgramData\\Test1\\Test Folder\""

      ... So, the command that is presented will be:

      dir "C:\\ProgramData\\Test1\\Test Folder"
      $ perl -wMstrict my $t = "dir \"C:\\ProgramData\\Test1\\Test Folder\""; print "T is '$t'\n"; __END__ T is 'dir "C:\ProgramData\Test1\Test Folder"'