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

Windows folder access error

by ArifS (Beadle)
on Oct 24, 2014 at 14:31 UTC ( [id://1104870]=perlquestion: print w/replies, xml ) Need Help??

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

I am getting the following error when try to execute the following code-
# c:\Folders\1Folder\1aFolder my $directory = "\\Folders\\1Folder\\1aFolder"; print "Folder: ", $directory, "\n"; opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { print "Files & Folders: ", $fldr, "\n"; }
Error-
Folder: \Folders\1Folder\1aFolder Invalid argument at c:\temp\dir1A9A.tmp\read.pl line ...##. Press any key to continue . . .
Pointing to line - opendir (DIR, $directory) or die $!;

Please let me know

Replies are listed 'Best First'.
Re: Windows folder access error ($^E)
by tye (Sage) on Oct 24, 2014 at 14:45 UTC

    Add $^E to your error report and you might get a better idea what is going wrong.

    My only guess is that your script is running in a context where it doesn't have a current "working drive" and if you prepend the "C:" to your string, that it might fix the problem.

    You can see the code that is producing this error at p5git://win32/win32.c., in particular:

    dirp->handle = FindFirstFileW(PerlDir_mapW(wscanname), &wFindData) +; if (dirp->handle == INVALID_HANDLE_VALUE) { DWORD err = GetLastError(); /* FindFirstFile() fails on empty drives! */ switch (err) { case ERROR_FILE_NOT_FOUND: return dirp; case ERROR_NO_MORE_FILES: case ERROR_PATH_NOT_FOUND: errno = ENOENT; break; case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break; default: errno = EINVAL; break; } Safefree(dirp); return NULL; }

    So EINVAL (invalid argument) just means "not ERROR_NO_MORE_FILES, ERROR_PATH_NOT_FOUND, nor ERROR_NOT_ENOUGH_MEMORY". $^E should tell you what GetLastError() returned.

    - tye        

      I can use "." as root folder, and the script works just fine.
      I can also get to the folder from cmd using -
      \Folders\1Folder\1aFolder
      So, it's a valid path for windows. It seems like doesn't like the folder command.

      Please let me know.
        I can use "." as root folder, and the script works just fine.
        Can you rephrase what you mean by that and why u cannot use the "." in your script to rectify the (relative path) issue ? : Its obvious as anything the path/relative path is not getting resolved,for which you need to do the error checking mentioned above.
        are u running the script from cmd prompt ?
        if so what is the output of your pwd (present working directory)command ? and
        is ure script able to run in a non-windows environment(if you have the luxury of checking that)
        Do not wait to strike when the iron is hot! Make it hot by striking - WB Yeats
Re: Windows folder access error
by GotToBTru (Prior) on Oct 24, 2014 at 14:41 UTC

    Is c:\Folders\1Folder\1aFolder\ a valid directory? It appears not.

    1 Peter 4:10
Re: Windows folder access error
by Lotus1 (Vicar) on Oct 24, 2014 at 19:52 UTC

    What operating system is this running on?

    When I run your code with an invalid directory the message tells me "No such file or directory...". One way to get the message you show is if you don't have permission to open that directory. http://www.tek-tips.com/viewthread.cfm?qid=346695

    use warnings; use strict; my $directory = "\\usr\\pm\\filefind1"; print "Folder: ", $directory; print -d $directory ? " found" : " not found", ".\n"; opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { print "$fldr\n"; } __END__ output: Folder: \usr\pm\filefind1 not found. No such file or directory at C:\usr\pm\filefind\1104870.pl line 8.

    Updated to use $^E.

    use warnings; use strict; my $directory = "\\usr\\pm\\filefind1"; print "Folder: ", $directory; print -d $directory ? " found" : " not found", ".\n"; ###### added $^E opendir (DIR, $directory) or die "$!\n***********\n$^E\n"; while (my $fldr = readdir(DIR)) { print "$fldr\n"; } __END__ output: Folder: \usr\pm\filefind1 not found. No such file or directory *********** The system cannot find the path specified
      Yes, I have permission to that folder. I can access using command prompt - cd \Folders\1Folder\1aFolder.

      As I mentioned above... it recognizes "." as the root but doesn't like when I look for a specific folder like- "\\Folders\\1Folder\\1aFolder". My understanding is it has to do with windows vs Linux pattern-

      Linux: /folders/.... etc
      Windows: \\Folders... etc.

        So you continue to ignore my advise and are left to run through whatever guesses people come up with as to the root cause?

        If Lotus1 had followed my advice, his test case would have also said 'Access is denied' (which identifies the problem rather clearly). Why don't you want to know the useful error message for your case?

        - tye        

Re: Windows folder access error
by RAJESHWAR.MUKUND (Initiate) on Oct 24, 2014 at 23:31 UTC

    use escape character '\' before special characters as

    my $directory = "c\:\\Folders\\1Folder\\1aFolder";

    and open directory should be closed after job complition.

    ---------------------------------------------------- # c:\Folders\1Folder\1aFolder # my $directory = "\\Folders\\1Folder\\1aFolder"; my $directory = "c\:\\Folders\\1Folder\\1aFolder"; print ("Folder: ", $directory, "\n"); opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { print "Files & Folders: ", $fldr, "\n"; } closedir(DIR);
    ----------------------------------------------------

    o/p

    c:\Folders\1Folder>perl read.pl Folder: c:\Folders\1Folder\1aFolder Files & Folders: . Files & Folders: .. Files & Folders: TEMP.PL

      use escape character '\' before special characters as

      Or just use single quotes as they do not interpolate like double quotes  'C:\ro\sham\bo';

      perlintro, perlquote

      I knew it was the my $directory line.... Thank you for sharing the correct format.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104870]
Approved by Corion
Front-paged by toolic
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: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found