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

Need help. how to dir a directory that have space.

my $source_directory = 'E:\\common\compare\latest folder\PDF\DB\\'; `dir $source_directory /b/s /o:-d`;

i get error "The system cannot find the file specified."

Replies are listed 'Best First'.
Re: how to dir a directory that have space
by eyepopslikeamosquito (Archbishop) on Jan 16, 2015 at 06:20 UTC

    Notice that it is not difficult to write a self-contained Perl script that does not require the Windows DIR command. Here is an example Perl script to give you a feel for where I'm coming from:

    use strict; use warnings; # Function to format a time: YYYY-MM-DD HH:MM:SS sub mydatetime { my $time = shift; my ($s, $m, $h, $d, $mon, $y) = localtime $time; sprintf "%04d-%02d-%02d %02d:%02d:%02d", $y+1900, $mon+1, $d, $h, +$m, $s; } my $source_directory = 'E:\\common\compare\latest folder\PDF\DB\\'; # Read directory contents into array @paths. opendir my $dh, $source_directory or die "error: open '$source_directo +ry': $!"; my @paths = grep { $_ ne '.' && $_ ne '..' } readdir $dh; closedir $dh; print "Contents of dir '$source_directory':\n"; for my $p (@paths) { my $fullpath = $source_directory . $p; my ($size, $modtime) = (stat $fullpath)[7,9]; my $type = -d $fullpath ? "directory" : "$size bytes"; printf "%-20.20s: %s (%s)\n", $p, mydatetime($modtime), $type; }

    An example run of this script:

    Contents of dir 'E:\common\compare\latest folder\PDF\DB\': abc.txt : 2015-01-16 16:33:09 (5 bytes) somedir : 2015-01-16 16:58:18 (directory) xyz.txt : 2015-01-16 16:58:42 (42 bytes)

    Note that this script does not use any external commands at all, just Perl internal functions. To better understand how it works see: opendir, readdir, grep, stat, localtime, sprintf.

    The advantages of a pure Perl solution is portability (e.g. the script will work fine on Unix and other OSes also) and flexibility (you can tailor the script to do things that the DIR command cannot do). Note, for example, that it would not be difficult to tailor this script to sort the files by date last modified. An added bonus is that writing such a script will improve your Perl coding skills.

      Thanks you very much. It worked as wanted. I'll explore more on this.

Re: how to dir a directory that have space
by vkon (Curate) on Jan 16, 2015 at 07:59 UTC
    given that your $source_directory contains spaces, the system command is misinterpreted:
    `dir $source_directory /b/s /o:-d`;
    use system(@list) form instead:
    system('dir', $source_directory, '/b/s', '/o:-d');
      use system(@list) form instead:

      That does nto work on windows!

      The simplest solution to the OP's problem is:

      my $source_directory = 'E:\\common\compare\latest folder\PDF\DB\\'; `dir \"$source_directory\" /b/s /o:-d`;

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        works for me,
        perl -we "system('dir','/b/s','/o:-d',shift)" "Program Files" C:\Program Files\Internet Explorer C:\Program Files\Microsoft Office C:\Program Files\Syncplicity C:\Program Files\McAfee C:\Program Files\WinDjView C:\Program Files\Synaptics C:\Program Files\Realtek C:\Program Files\Microsoft Silverlight C:\Program Files\Windows Journal C:\Program Files\Java C:\Program Files\Microsoft Policy Platform C:\Program Files\Common Files C:\Program Files\Lenovo C:\Program Files\Courion Corporation C:\Program Files\Windows Defender ...
        indeed, there was dir.exe in cygwin in my setup that I needed to expunge before running the one-liner

        No it does not work.

Re: how to dir a directory that have space
by Anonymous Monk on Jan 16, 2015 at 04:11 UTC
    How would you do it from cmd.exe?

      From cmd need to put "" but when i put " in script it just hang and does not give any response.

      dir E:\common\compare\"latest folder"\PDF\DB\

      this is what i tried but it just stall when perform dir

      my $source_directory = 'E:\\common\compare\"latest folder"\PDF\DB\\'; `dir $source_directory /b/s /o:-d`;
        Hello AtlasFlame,

        here you are mixing two problems: path with spaces AND path separator.
        With ugly path with spaces you need double quotes around the whole path:
        dir "C:\Local Publish\images" #OK
        So if you need to pass such a string with doublequotes untouched, for example inside a sigle quote string you need to escape (with '\') all occourences of " you want to be passed unaltered, as in:
        perl -e " $dir = '\"C:\Local Publish\images\"'; system qq(dir $dir )" + #OK
        As personal suggestion avoid to create and use paths with spaces, is a very stupid 'feature'.

        Now the path separator part. As far as i know, you can use both '\' and '/' as path separator in windows. This is handy because the '\' happens to be the escape char in Perl. In the command prompt you can safely use:
        dir "C:/Local Publish/images" #OK
        So you can safely do the same in a Perl program:
        perl -e " $dir = '\"C:/Local Publish/images\"'; system qq(dir $dir )" + #ok
        If you need to interpolate some var inside such strings you need the concatenation operator (the dot '.'):
        perl -e " $img = 'images';$dir = '\"C:/Local Publish/'.$img.'\"'; syst +em qq(dir $dir )"
        You can use this post as explaination of quotes in Perl and the docs as base rference. Read also the usage hints on windows especially using Perl from command line.

        HtH
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.