CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Need Help With Win32::GetLongPathName

 



Freon
Novice

Apr 24, 2011, 11:47 PM

Post #1 of 10 (3696 views)
Need Help With Win32::GetLongPathName Can't Post

I am trying to use Win32::GetLongPathName to produce long filenames from short filenames generated by

for my $line (`dir /s/x $ARGV[0]`)

The path names produced do not print correctly if they contain any character value greater than 0xFF (ÿ). What is the character encoding of strings returned by Win32::GetLongPathName and how do I convert them so that they print correctly?

Thanks!


(This post was edited by Freon on Apr 24, 2011, 11:50 PM)


miller
User

Apr 25, 2011, 12:01 AM

Post #2 of 10 (3691 views)
Re: [Freon] Need Help With Win32::GetLongPathName [In reply to] Can't Post

Don't use a system call, instead use File::Find:


Code
use File::Find; 
use Win32;

use strict;
use warnings;

my $dir = shift || '.';

find(sub {
print $File::Find::name, ' ', Win32::GetShortPathName($_), "\n";
}, $dir);



Freon
Novice

Apr 25, 2011, 12:45 PM

Post #3 of 10 (3649 views)
Re: [miller] Need Help With Win32::GetLongPathName [In reply to] Can't Post

Do I understand correctly that I must eliminate the "dir/s/x" command? That means I need to write the code to recurse all subdirectories of the main directory and reproduce "dir" display of file size and modification time. Does Win32::GetLongPathName not work with non-English filenames?


FishMonger
Veteran / Moderator

Apr 25, 2011, 1:07 PM

Post #4 of 10 (3644 views)
Re: [Freon] Need Help With Win32::GetLongPathName [In reply to] Can't Post

The dir command is not needed and should be removed from your script.

The File::Find module that miller suggested, and gave an example of, will recurse all subdirectories. To get the file size, you'd use Perl's -s file test operator and to get the modification time, you'd use Perl's stat function and to format that timestamp exactly the way you wish, you'd use the strftime function from the POSIX module.

http://search.cpan.org/~rjbs/perl-5.12.3/lib/File/Find.pm

http://perldoc.perl.org/functions/-X.html

http://perldoc.perl.org/functions/stat.html

http://linux.die.net/man/3/strftime


(This post was edited by FishMonger on Apr 25, 2011, 1:10 PM)


Freon
Novice

Apr 25, 2011, 2:41 PM

Post #5 of 10 (3637 views)
Re: [FishMonger] Need Help With Win32::GetLongPathName [In reply to] Can't Post

1. The sample script does not seem to obtain the full filename for filenames that contain non-English characters. Only the short name is retrieved. Please test it with a filename like "Tzur Menti⁄Se'i Yona⁄Sapri Tama.mp3" (the slashes are U+2044 for obvious reasons). Further, the file written is ANSI, so those characters cannot be represented anyway (maybe this can be fixed by having Perl write the file instead of writing to stdout and redirecting to a file on the command line).

2. I can accept a change to the date/time output if I must, but actually "exactly the way I wish" is "exactly the way 'dir' does it" for compatibility with existing output.

Thanks,

Freon


FishMonger
Veteran / Moderator

Apr 25, 2011, 4:53 PM

Post #6 of 10 (3627 views)
Re: [Freon] Need Help With Win32::GetLongPathName [In reply to] Can't Post


Code
#!/usr/bin/perl 

use warnings;
use strict;
use File::Find;
use Win32;

my $dir = shift || './fishmonger';

find(sub {
print $File::Find::name, "\n\t", Win32::GetShortPathName($_), "\n";
}, $dir);


D:\perl>test.pl

Code
./fishmonger 
.
./fishmonger/Example.pm
Example.pm
./fishmonger/perl-1.pl
perl-1.pl
./fishmonger/test.txt
test.txt
./fishmonger/Tzur Menti⁄Se'i Yona⁄Sapri Tama.mp3
TZURME~1.MP3
./fishmonger/vishwakar.txt
VISHWA~1.TXT


Does this differ from your results? If so, please post your test script and its output such as I did.


Freon
Novice

Apr 25, 2011, 7:09 PM

Post #7 of 10 (3617 views)
Re: [FishMonger] Need Help With Win32::GetLongPathName [In reply to] Can't Post

Sorry, my last post was not clear. The filename actually contains (U+2044) "division slash" characters. This forum cannot deal with non-English characters either, so it translates them to a sequence that describes the representation. I have attached a UCS-2 text file created with Notepad++ containing some non-English filenames.
Attachments: multi-language.txt (0.18 KB)


Freon
Novice

Apr 25, 2011, 8:18 PM

Post #8 of 10 (3610 views)
Re: [Freon] Need Help With Win32::GetLongPathName [In reply to] Can't Post

Here is a screenshot of my foreign filename test directory, my test Perl script, and the output text file generated by "perl testdir.pl \mp3\holding\foreign >testdirforeign.txt"

(BTW, the "&#8260" you see in the filename in my earlier post is the decimal equivalent of 0x2044, the Unicode code point for a "division slash" character. Open charmap, select "Ariel" and look about 2/3 of the way through the list; it's on the line that begins with the "5/8" character.)
Attachments: multi-language folder.jpg (152 KB)
  testdir.pl (0.18 KB)
  testdirforeign.txt (0.97 KB)


FishMonger
Veteran / Moderator

Apr 26, 2011, 6:31 PM

Post #9 of 10 (3485 views)
Re: [Freon] Need Help With Win32::GetLongPathName [In reply to] Can't Post

I have not run any tests, but try this version.


Code
#!/usr/bin/perl 

use warnings;
use strict;
use File::Find;
use Win32;

my $dir = shift || '.';

find(sub {
print Win32::GetLongPathName($_), "\n\t", Win32::GetShortPathName($_), "\n";
}, $dir);



miller
User

Apr 28, 2011, 8:05 PM

Post #10 of 10 (3344 views)
Re: [FishMonger] Need Help With Win32::GetLongPathName [In reply to] Can't Post

I created a quick script to make some files with the same names as you provided:


Code
use strict; 
use warnings;

use File::Slurp;

while (<DATA>) {
chomp;
write_file($_, "test");
}

__DATA__
.Mylène Farmer & Jean-Louis Murat- Regrets (Razormaid Mix).mp3
Mylène Farmer- Désenchantée (Razormaid Mix).mp3
Mylène Farmer- Je T'Aime Mélancolie (Razormaid Mix).mp3
Mylène Farmer- Psychiatric (121 BPM Razormaid Mix).mp3
Ofra Haza- Ash'alech.mp3
Ofra Haza- Barefoot.mp3
Ofra Haza- I'm Nin' Alu (Razormaid Mix).mp3
Ofra Haza- Kirya.mp3
Ofra Haza- Lefelach Harimon.mp3
Ofra Haza- Ode Le'eli.mp3
OF3552~1.MP3
Ofra Haza- Yachilvi Veyachali.mp3
Ofra Haza- Yesh Li Gan.mp3
-FCDE~1.MP3


I then ran the original script where I outputted to a file instead of to the console:


Code
#!/usr/bin/perl 

use warnings;
use strict;
use File::Find;
use Win32;

my $dir = shift || '.';

open my $fh, '>', 'files.txt' or die $!;
select $fh;

find(sub {
print $_, "\t", Win32::GetShortPathName($_), "\n";
}, $dir);


And everything appears to work fine. Note, I don't have any utf8 characters in my files, just extended ascii. But that much works at least.

- Miller

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives