in reply to how to dir a directory that have space
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to dir a directory that have space
by AtlasFlame (Novice) on Jan 16, 2015 at 07:02 UTC |