Is there any place I can learn more about using the dir/file tests in Windows for path names which include spaces? I have written a couple of short scripts to test some ways of doing this. First, the simple case:

$me = "dirtest1"; # Perl script to test variations on file/dir test argument quoting print "All of the following conditions are true on my system.\n"; print "The issue is to see which tests work.\n"; print "The path includes a directory name containing a space.\n"; print "\n"; print "\nThese tests use a single, defined string as path:\n"; # This example would not run due to compiler errors # $expl = "Bare unquoted text with single backslashes"; # if ( -e C:\users\public\Music\Sample Music ) # { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\ +n"; } # This example would not run due to compiler errors # $expl = "Bare unquoted text with double backslashes"; # if ( -e C:\\users\\public\\Music\\Sample Music ) # { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\ +n"; } $expl = "Single-quoted text with single backslashes"; if ( -e 'C:\users\public\Music\Sample Music' ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Single-quoted text with double backslashes"; if ( -e 'C:\\users\\public\\Music\\Sample Music' ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Double-quoted text with double backslashes"; if ( -e "C:\\users\\public\\Music\\Sample Music" ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; }
This works pretty much as I expected.
All of the following conditions are true on my system. The issue is to see which tests work. The path includes a directory name containing a space. These tests use a single, defined string as path: '-e Single-quoted text with single backslashes' is true '-e Single-quoted text with double backslashes' is true '-e Double-quoted text with double backslashes' is true
I was a little surprised that the second test worked. I deleted several other versions which failed.

Now for the real test. This one stumps me.

$me = "dirtest2"; # Perl script to test variations on file/dir test argument quoting print "All of the following conditions are true on my system.\n"; print "The issue is to see which tests work.\n"; print "The final path includes a directory name containing a space.\n" +; print "\n"; $part1 = "C:\\users\\public\\Music"; $part2 = "Sample Music"; $dfull = "$part1\\$part2"; print "\nThese tests use a path consisting of concatenated substrings: +\n"; print " part1 = '$part1'\n"; print " part2 = '$part2'\n"; print " dfull = '$dfull'\n"; print "\n"; $expl = "Part1, unquoted"; if ( -e $part1 ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Full path, unquoted"; if ( -e $full ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Part1 in single quotes"; if ( -e '$part1' ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Full path in single quotes"; if ( -e '$full' ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Part1 in double quotes"; if ( -e "$part1" ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; } $expl = "Full path in double quotes"; if ( -e "$full" ) { print "'-e $expl' is true\n"; } else { print "'-e $expl' is false\n" +; }
The results are disappointing.
All of the following conditions are true on my system. The issue is to see which tests work. The final path includes a directory name containing a space. These tests use a path consisting of concatenated substrings: part1 = 'C:\users\public\Music' part2 = 'Sample Music' dfull = 'C:\users\public\Music\Sample Music' '-e Part1, unquoted' is true '-e Full path, unquoted' is false '-e Part1 in single quotes' is false '-e Full path in single quotes' is false '-e Part1 in double quotes' is true '-e Full path in double quotes' is false
I have yet to find any way to get the correct results with a path string I have built from separate variables.


In reply to Win file/dir names w/spaces by LloydRice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.