Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Trouble using a Win32 file path in an array

by geo (Acolyte)
on Sep 25, 2009 at 13:44 UTC ( [id://797509]=perlquestion: print w/replies, xml ) Need Help??

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

perl, version 5.8.8
Microsoft Windows XP Pro, SP2

I have a data file containing one or more windows folder paths which I load into an array. When I work through the array and attempt to use the folder path it is not recognized as a valid path. Each line in the data file looks like the following.

C:\HELP\Docs\R2009\sp01\

For this exercise I am using a simple file test to verify the existance of the folder. The actual script uses strict, warnings, and error checking.

my $datafile = "C:\\HELP\\PerlScripts\\Test2.dat"; my (@PATHS); open (DATAFILE,"< $datafile"); while (defined (my $line = <DATAFILE>)) { chomp($line); push (@PATHS,$line); } foreach my $path (@PATHS) { if (-d $path) { print "Found $path \n"; } else { print "DID NOT FIND $path \n"; } }

The previous example code prints "DID NOT FIND C:\HELP\Docs\R2009\sp01\"

The result is the same when the data file entry is C:\\HELP\\Docs\\R2009\\sp01\\

I even went so far as to split the path on "\\" and used join to reconstruct the file path but the results were the same. I assume that I am missing sommething (hopefully something simple) and that one of you fine folks will be able to point that out to me.

Thanks in advance.

Replies are listed 'Best First'.
Re: Trouble using a Win32 file path in an array
by ramlight (Friar) on Sep 25, 2009 at 14:17 UTC
    Is there perhaps some code around this that you are not showing to us? I tried your example on Perl 5.10.0 and it ran fine for me:
    >type \help\perlscripts\test2.dat C:\HELP\Docs\R2009\sp01\ >dir C:\HELP\Docs\R2009\sp01\ Volume in drive C has no label. Volume Serial Number is 74E6-4409 Directory of C:\HELP\Docs\R2009\sp01 09/25/2009 09:57 AM <DIR> . 09/25/2009 09:57 AM <DIR> .. 0 File(s) 0 bytes 2 Dir(s) 224,589,967,360 bytes free >perl pathq.pl Found C:\HELP\Docs\R2009\sp01\ >perl -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread
      No there is no other code. After your response I recreated everything on a different system and it works fine. At least I know that I was on the right track with Perl, now all I have to do is figure out why it fails on the first system. Thank you for taking the time to look at this and to respond. -geo
Re: Trouble using a Win32 file path in an array
by kennethk (Abbot) on Sep 25, 2009 at 14:58 UTC
    If you want to try to avoid this in the future, you might consider using File::Spec, one of Perl's core modules. Given a list of directories, it will assemble the complete path for you:

    use File::Spec qw(catdir catpath); my $volume = 'C:\\'; my @directories = 'HELP', 'PerlScripts'; my $file = 'Test2.dat'; $directory = catdir( @directories ); my $datafile = catpath( $volume, $directory, $file );
Re: Trouble using a Win32 file path in an array
by almut (Canon) on Sep 25, 2009 at 14:28 UTC

    If you're sure the directories actually exist, print out $path using Devel::Peek (or Data::Dumper using $Data::Dumper::Useqq = 1;) to check if there are any unexpected chars in the string.

Re: Trouble using a Win32 file path in an array
by Herkum (Parson) on Sep 25, 2009 at 14:27 UTC

    Have you tried testing your directory path at each level? The reason I ask, is that what you have in the file may not match what you see in Explorer.

    The code should work fine, usually the mistake is a problem which the data does not matching exactly like you think it does.

Re: Trouble using a Win32 file path in an array
by ramlight (Friar) on Sep 25, 2009 at 14:30 UTC
    And I tried this on a system with Perl 5.8.0 with similar success:
    SEA32895-N2 c:\HELP\PerlScripts>perl pathq.pl Found C:\HELP\Docs\R2009\sp01\ SEA32895-N2 c:\HELP\PerlScripts>perl -v This is perl, v5.8.0 built for MSWin32-x86-multi-thread
    Is it possible that you do not have permission to read one of the directories in the path \HELP\Docs\R2009\sp01 and so are failing on permission rather than not seeing the directory?
Re: Trouble using a Win32 file path in an array
by ikegami (Patriarch) on Sep 25, 2009 at 14:26 UTC
    trailing spaces?
Re: Trouble using a Win32 file path in an array
by Marshall (Canon) on Sep 27, 2009 at 22:37 UTC
    This doesn't appear to have been mentioned before so here is a Windows tidbit:

    Perl on windows can use "/" instead of the windows backslash in path names just fine. So when calling a Perl function on Windows, C:/dir/dir2 is just fine.A Perl function under Windows will also return forward slash names instead of backslash names.

    You need the backslash when you go to the windows shell (with backticks, etc), but otherwise use the forwardslash "/". The code is more portable and this solves some problems when running regex'es on pathnames since you don't have to worry about "escaping" the backslash with yet another backslash.

      You need the backslash when you go to the windows shell

      Windows accepts both "\" and "/" as directory separators.

      The shell and many shell commands treat "/" as an option starter, but quoting usually removes that meaning:

      >"c:\progs\perl5101\bin\perl" -le"print 'Hi!'" Hi! >"c:/progs/perl5101/bin/perl" -le"print 'Hi!'" Hi!
      >echo foo > "/foo.txt" >move "/foo.txt" "./bar.txt" >type "c:/documents and settings/ikegami/bar.txt" foo >del "./bar.txt"
        Great info! On older versions of Windows, it didn't work that way, but you are correct on Win XP.

        Update: not quite right...

        C:\PROJECTS\xyz2009>cd c:/projects The system cannot find the path specified. C:\PROJECTS\xyz009>cd c:\projects C:\PROJECTS>
        Another Update:
        Revising my post in response to ikegami's post.
        Apologize for being too cryptic.

        1. First there are a whole lot of Windows things between Win 3.1 and Win XP: Win 98, WinNT, Win2K. I was thinking more about NT and 2K when I made the comment about previous versions of windows. At one time I had all 4 versions (98,NT,2K,XP) on my network for testing, but am down to just XP now. There are lots of quirks between these various versions.

        2. I did some testing at the command prompt on XP and found:

        C:\> C:\>cd projects/replyto C:\PROJECTS\replyto>
        That was a "WOW" moment as I hadn't expected that forward slash to work with the cd command! But it did!

        3. Then I found out that there is something special about the Drive: root- the forward slash doesn't work in this situation:

        C:\PROJECTS\replyto>cd c:/projects The system cannot find the path specified. (didn't work) C:\PROJECTS\replyto>cd c:\projects (does work) C:\PROJECTS>
        That little discovery is what prompted the update: "not quite right" comment.

        4. More examples:

        C:\PROJECTS>type testing/test.pl (doesn't work) The syntax of the command is incorrect. C:\PROJECTS>type "testing/test.pl" (doesn't work either) The system cannot find the file specified. C:\PROJECTS>type "testing\test.pl" (works) C:\PROJECTS>type testing\test.pl (works) C:\PROJECTS>cd ./testing (works) C:\PROJECTS\testing> C:\PROJECTS>cd /testing (works) C:\PROJECTS\testing>
        Evidently sometimes the "forwards slash" works at the command line and sometimes it doesn't. There appears to be something special about drive root and I suppose some of these commands like "type" can get confused between path and "/" options.

        5. In an attempt to try and avoid confusion, when writing Perl, I always use forward slash. That works with all Perl functions even with a path like c:/projects. As long as you don't go the the shell with say a command('blah'), forward slash is the way to go.

      A Perl function under Windows will also return forward slash names instead of backslash names.

      Depends on the function.

Log In?
Username:
Password:

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

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

    No recent polls found