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

cannot get the exist or glob or size function work with UNC path folders So to work with long folders i used the UNC way to create smaller paths on windows machine. exampel showsn below doesnot have long path. But testing it out to see why these functions don't work

my $directory = "C:\\test\\test2"; $directory =~ s/\//\\/g; if ($directory !~ /^\\\\?\\/ ) {$directoryUNC ="\\\\?\\".$direct +ory }; $len = length($directoryUNC ); $directoryshort = Win32::GetShortPathName($directoryUNC ); print "short name for $directory is $directoryshort \n"; chdir "$directoryshort" or die "Failed to go to $directoryshort: $ +!"; $FolderName = getcwd(); opendir DIR, $FolderName or die "Cannot open directory: $!"; while ( defined( my $file = readdir(DIR) ) ) { $fileSize= -s $file; print "file size for $file is $fileSize\n"; }

this does not print the file size. Have tried other things like exists function doesn't work either. Would someone know how to use these functions with the short UNC path

Replies are listed 'Best First'.
Re: size and exist doesnot work with UNC folders/dir
by kcott (Archbishop) on Oct 17, 2013 at 02:48 UTC

    G'day mukes,

    Welcome to the monastery.

    In order to provide you with some help on this, it would be useful if you posted the actual output (rather than just stating what isn't printed).

    For instance, "this does not print the file size", could mean that the statement "print "file size for $file is $fileSize\n";" is never executed, or that it prints something like one of these:

    file size for some_filename is file size for some_filename is 0 file size for some_filename is not_a_file_size_value file size for some filename with spaces is

    Note that last example. If any of your files, directories, or other path components, contain spaces, you'll probably need to quote them.

    I see one other print statement. You could add more, right after each variable assignment: this is often a good way to locate problems.

    While it doesn't appear to be an issue in this particular code, be aware that readdir returns just the filename, not a full path. You often need to prepend the path.

    You haven't shown all of your code. While I can guess there's a "use Cwd;" line somewhere, I don't actually know. I also don't know what else may (or may not) be present that could affect how your code runs.

    There's guidelines for all of these things, and more, in "How do I post a question effectively?".

    You might also be interested in the results of a search for 'UNC' on CPAN.

    -- Ken

Re: size and exist doesnot work with UNC folders/dir
by bulk88 (Priest) on Oct 17, 2013 at 03:29 UTC
    Look at Win32API::File for raw access to the Win32 file API and not dealing with any Perl posixy compatible code in the engine.
Re: size and exist doesnot work with UNC folders/dir
by marinersk (Priest) on Oct 17, 2013 at 17:48 UTC
    You have good advice from others regarding the use of modules. I won't harp there.

    Sometimes we write code because we don't know about modules; sometimes we write code because we have trouble using modules; sometimes we write code because we want to understand how something works. The questions arising from the do-it-yourself approach are, in my humble but arrogant opinion, still valid.

    That said, to answer your specific questions, three things are needed:

    1. What is the input (in this case a DIRor similar would suffice);
    2. What is the output you received, and then merely highlight how that is different than what you expected, and;
    3. Working code.

    Here's what I get when I run your code:

    C:\Steve\Dev\PerlMonks\P-2013-10-17@1130-FileSize>testfsize.pl Global symbol "$directoryUNC" requires explicit package name at C:\Ste +ve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 9. Global symbol "$len" requires explicit package name at C:\Steve\Dev\Pe +rlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 10. Global symbol "$directoryUNC" requires explicit package name at C:\Ste +ve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 10. Global symbol "$directoryshort" requires explicit package name at C:\S +teve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 11. Global symbol "$directoryUNC" requires explicit package name at C:\Ste +ve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 11. Global symbol "$directoryshort" requires explicit package name at C:\S +teve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 12. Global symbol "$directoryshort" requires explicit package name at C:\S +teve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 13. Global symbol "$directoryshort" requires explicit package name at C:\S +teve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 13. Global symbol "$FolderName" requires explicit package name at C:\Steve +\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 14. Global symbol "$FolderName" requires explicit package name at C:\Steve +\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 15. Global symbol "$fileSize" requires explicit package name at C:\Steve\D +ev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 17. Global symbol "$fileSize" requires explicit package name at C:\Steve\D +ev\PerlMonks\P-2013-10-17@1130-FileSize\testfsize.pl line 18. Execution of C:\Steve\Dev\PerlMonks\P-2013-10-17@1130-FileSize\testfsi +ze.pl aborted due to compilation errors.

    If your problem is you don't know how to fix these errors, then that should be the question you are asking.

    Until these are fixed, there's no point in trying to see if maybe your code has a logic error -- it's not even storing the data correctly.

    Let's get the code to a state where it runs without complaints from the perl compiler, and then we can pursue any remaining malfunctions.

    Hint:

    Every Perl script should have start like this (or variations on the theme):
    #!/usr/bin/perl use strict; use warnings;

    Get perl to quit barking at you first. Then ask the humans (or the Monks :-)) for help.

Re: size and exist doesnot work with UNC folders/dir (yes it does)
by Anonymous Monk on Oct 17, 2013 at 02:27 UTC

    use Path::Tiny for working with paths. no readdir, readdir is too low-level, and too confusing for noobs