Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: I go crazy with windows filenames with spaces!

by ahmad (Hermit)
on Jan 19, 2010 at 15:03 UTC ( #818204=note: print w/ replies, xml ) Need Help??


in reply to I go crazy with windows filenames with spaces!

If you are running on windows XP or higher you don't need to escape the blank space.

Try this:

my $path = 'C:\Documents and Settings\Administrator\Desktop'; opendir(DIR,$path) or die $!; while (my $File = readdir DIR) { print "$File\n"; } closedir(DIR);

As I remember older versions of windows only support 8 characters long file/folder name so you'll have to use 6 characters and add ~1 at the end of the file name.

I am not very sure I haven't used old windows OS from like 6 years now


Comment on Re: I go crazy with windows filenames with spaces!
Download Code
Re^2: I go crazy with windows filenames with spaces!
by Anonymous Monk on Jan 19, 2010 at 15:07 UTC
    Win32::GetShortPathName($fullpath);

      I could not find

      Win32::GetShortPathName($fullpath);

      But I've knocked up this which seems to work

      use Win32::API; sub GetShortPathName { my $GetShortPathName = new Win32::API('kernel32', 'GetShortPathName +A', 'PPN', 'N'); die "Can't import API GetShortPathNameA:\n$!" unless defined $GetSh +ortPathName; my $longpath = shift; my $length = 1 + length $longpath; my $shortpath = ' ' x ($length); $length = $GetShortPathName->Call($longpath, $shortpath, $length); return substr($shortpath, 0, $length); }

        I could not find Win32::GetShortPathName($fullpath);

        What does that mean?

        Win32::GetShortPathName has been available since perl 5.6

Re^2: I go crazy with windows filenames with spaces!
by Anonymous Monk on Jan 19, 2010 at 15:09 UTC
    All right! My first code works if I chop what I get from cd, so:
    $path = `cd`; chop $path; opendir MYDIR, $path; print "Path:\n$path\n"; while ( $file = readdir(MYDIR)) { + print "File: ", $file, "\n"; }
    One can really go mad when dealing with filenames in Windows. Greetings Bill... Thanks!

      Your problem is less with Windows but with your roundabout way of using Perl as if it were a command shell instead of using the Perl built-in ways.

        I try to use module when coding in Perl only if there is no other way. I just like to know what is happening in my script in every line. If you "overuse" other people's work you can run into trouble one day :)

        (This is my general opinion about mass using modules/classes when developing new application, we can discuss it here if you want.)

      None of the problems you experienced are in any way related to Windows. The program would behave identically on any other OS*.

      {Bashing someone else for your own errors}--

      The space you are getting is not a space at all. It's the linefeed at the end of the line cd outputted. chomp is a better way of removing it.

      * — Actually, it'll work even less because `cd` won't work on other OSes. It only works on Windows because of a bug in the Windows part of Perl. If `cd` did work elsewhere, the rest of the program would behave identically.

Re^2: I go crazy with windows filenames with spaces!
by ikegami (Pope) on Jan 19, 2010 at 15:22 UTC

    If you are running on windows XP or higher you don't need to escape the blank space.

    OS doesn't matter. opendir always takes a path for argument. Not a path that's been escaped into something else.

    As I remember older versions of windows only support 8 characters long file/folder name so you'll have to use 6 characters and add ~1 at the end of the file name.

    You're saying there exists a version of Windows that both supported long file names and didn't support long file names.

    DOS was what didn't support long file names. DOS applications running on Windows had to use the short path names. Adding ~1 is not the correct way of getting the short file name. And finally, the problem at hand has nothing to do with long file names. Or file names at all, really.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-10-21 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls