LloydRice has asked for the wisdom of the Perl Monks concerning the following question:
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:
This works pretty much as I expected.$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" +; }
I was a little surprised that the second test worked. I deleted several other versions which failed.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
Now for the real test. This one stumps me.
The results are disappointing.$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" +; }
I have yet to find any way to get the correct results with a path string I have built from separate variables.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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win file/dir names w/spaces
by Corion (Patriarch) on Mar 22, 2014 at 21:14 UTC | |
|
Re: Win file/dir names w/spaces
by kcott (Archbishop) on Mar 23, 2014 at 06:19 UTC | |
by LloydRice (Beadle) on Mar 26, 2014 at 10:16 UTC | |
|
Re: Win file/dir names w/spaces
by LloydRice (Beadle) on Mar 22, 2014 at 21:11 UTC | |
|
Re: Win file/dir names w/spaces
by LloydRice (Beadle) on Mar 22, 2014 at 21:25 UTC | |
by AnomalousMonk (Archbishop) on Mar 22, 2014 at 23:47 UTC | |
by GrandFather (Saint) on Mar 22, 2014 at 21:41 UTC | |
by LloydRice (Beadle) on Mar 22, 2014 at 21:28 UTC |