Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Long Directory Names

by markwild (Sexton)
on Dec 12, 2000 at 09:02 UTC ( [id://46213]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Perlish People,
I know this is a problem that's been conquered many times before, but the answer, at present, eludes me. I'm working on a Win2k box with Activestate Perl 5.6. Disk is NTFS format. I need to process all of the files in a directory. The directory name is very long and has spaces in it.
I have followed the recipes in the Cookbook, and they work as long as there are *no* spaces in the path. But when I have a path name like "d:\my documents\my other stuff\*.*" it fails.
Specifically, here is my code:
$thispath = 'd:\my documents\my other stuff\*.*'; print "thispath is [$thispath]\n"; @thisdir = glob($thispath); foreach $thisfile (@thisdir) { print " Found [$thisfile]\n"; }
And the output I get is:
thispath is [d:\my documents\my other stuff\*.*] Found [d:./my] Found [docsmy]
The actual directory has this:
12/11/2000 07:49p <DIR> . 12/11/2000 07:49p <DIR> .. 12/11/2000 07:42p 480 dir.pl 12/11/2000 07:52p 185 dir2.pl 12/11/2000 07:53p 0 out.txt 12/11/2000 11:00a 1,125 populate.pl
However, if I replace the full path with ".\*.*", I get this:
thispath is [.\*.*] Found [.\dir.pl] Found [.\dir2.pl] Found [.\out.txt] Found [.\populate.pl]
I can't seem to find anything on CPAN for this, though I know there must be something somewhere. FYI: I've also tried using opendir, as suggested in another article on this site. Is this a problem with Globbing, or with how the file names are stored in the list?
Any random thoughts? Any specific ones?
Much Thanks.
--Mark

Replies are listed 'Best First'.
Re: Long Directory Names
by clemburg (Curate) on Dec 12, 2000 at 15:48 UTC

    If you use the glob() function on Windows, you need to use File::DosGlob - otherwise globbing won't work the way you expect it to work. This also applies to hidden uses of glob() with the angle operator, like @files = <*.*>;.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Long Directory Names
by myocom (Deacon) on Dec 12, 2000 at 09:35 UTC

    I might suggest using readdir for something like this. The following snippet should get you started...

    my $path='d:\my documents\my other stuff'; opendir (DIR, $path) || die "Couldn't open $path: $!\n"; my @files=readdir (DIR); # Slurp! print join("\n",@files); closedir (DIR);
Re: Long Directory Names
by zzspectrez (Hermit) on Dec 12, 2000 at 12:20 UTC

    How about something like this...

    #!/usr/bin/perl -w use strict; my $thispath = 'c:\Documents and Settings\bch\My Documents\mp3s\\'; print "PATH: $thispath"; chdir ($thispath); my @files = <*.*>; map { print "FOUND: $_\n"; } @files;

    Here is my output on my Win2K machine.

    FOUND: Liquid Soul - 03 - Salt Peanuts-Chocolate Covered Nut.mp3 FOUND: Liquid Soul - 10 - No Cents.mp3 FOUND: Ronny Jordan feat Mos Def - A Brighter Day (remix).mp3

    Hope this helps..
    zzSPECTREz

Re: Long Directory Names
by repson (Chaplain) on Dec 12, 2000 at 12:25 UTC
    You might also be interested in looking at the File:: series of modules. Several come with Activestate which may be of use. File::Spec is supposed to do lots of thing portably, but I didn't see what you wanted there so File::Glob may work better. Or better yet just use opendir(FOO,$dirname); @files = readdir(FOO); closedir(FOO);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found