Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: How to pass a folder name to Windows external commands?

by haukex (Archbishop)
on Mar 04, 2022 at 18:45 UTC ( [id://11141828]=note: print w/replies, xml ) Need Help??


in reply to Re: How to pass a folder name to Windows external commands?
in thread How to pass a folder name to Windows external commands?

I created several directories and files with non-ANSI filenames, and the following code works fine on them - it iterates over the short names such as "C:\\Temp\\testuni\\5E71~1\\BF49~1.TXT".

use warnings; use strict; use Path::Class 'dir'; use IPC::Run3 'run3'; use Data::Dump; my $dir = dir("C:\\Temp\\testuni"); $dir->recurse( callback => sub { my $path = shift; if (!$path->is_dir) { run3 ['hex', $path], undef, \my $out; dd "$path", $out; } });

Don't use dir to get a list of filenames, which is why I'm using Path::Class (at the very least, use readdir). Please show a runnable, representative SSCCE that demonstrates the problem you're having, including sample input (directory structure), expected output for that input, and the actual output you're getting including any error messages.

And by representative I also mean to please show the actual command you're running, assuming it isn't robocopy - since its /l option means to just list files, I'm not sure what you're doing with that command in your sample code. One of the things discussed in The problem of "the" default shell, which is linked from my node referenced above, is that a common mistake is to shell out to commands that should be implemented in native Perl.

Replies are listed 'Best First'.
Re^3: How to pass a folder name to Windows external commands?
by freonpsandoz (Beadle) on Mar 08, 2022 at 11:41 UTC

    I'll try to generate a better example.

    I'm using 'dir' in an attempt to glob a filespec and get Unicode filenames, which File::Glob::Windows doesn't seem to do. Will Path::Class do that? I'm using robocopy /l to get the total size of a folder, including subfolders. It's supposed to be a fast way to do that.

    I tried using run3, but I don't understand its rules and haven't got it working yet. Is 'hex' a command in your example? Is there a reason that you used short pathnames? Thanks.

      I'm using 'dir' in an attempt to glob a filespec and get Unicode filenames

      I see, that does make it a bit more complicated. I used Win32::LongPath in the following, plus Text::Glob since you mentioned globs, but of course using a regex directly would be easier.

      use warnings;
      use strict;
      use utf8;
      use Text::Glob 'glob_to_regex';
      use Win32::LongPath;
      use File::Spec::Functions qw/no_upwards catfile/;
      use IPC::Run3 'run3';
      use Data::Dump;
      
      my $glob = glob_to_regex('*Перл*');
      my $dir = "C:\\Temp\\testuni";
      
      my $dh = Win32::LongPath->new;
      $dh->opendirL($dir) or die "unable to open $dir: $^E";
      for my $file ( grep {/$glob/} no_upwards $dh->readdirL ) {
      	my $path = catfile($dir, $file);
      	my $sp = shortpathL($path);
      	run3 ['hex', $sp], undef, \my $out;
      	dd $path, $sp, $out;
      }
      $dh->closedirL;
      
      Is 'hex' a command in your example?

      I happened to have C:\Windows\hex.exe lying around, a simple hex dump tool from here.

      Is there a reason that you used short pathnames?

      In my first example, that's just how Path::Class returned them; in my example above I used them because it appeared to be easier to pass the pathname to the external command that way.

      I'm using robocopy /l to get the total size of a folder, including subfolders. It's supposed to be a fast way to do that.

      I tried running robocopy, and at least on my system the report it outputs doesn't mention file sizes anywhere. You might also want to look at e.g. the Perl version of du to see the algorithm used there.

      I tried using run3, but I don't understand its rules and haven't got it working yet.

      Again, it would be best if you can show an SSCCE.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found