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

O great monks,

How does one deal with files over a fileshare on a Win32 system? I need to do things like listing directories, determining files vs directories, copying/moving/deleting files, getting file dates, and the like. I've gotten reasonably proficient at this stuff on Unix systems by using Net::FTP, Net::Telnet, and/or the file test operators, but most of that doesn't apply here. The Win32 server(s) I'm dealing with aren't running FTP or Telnet daemons, and many of the standard file/directory functions seem to only work on local files/directories. For example:
$dir = "\\\\server\\share"; @files = <$dir/*>; print "$_\n" for @files;

This returns nothing, presumably because the angle operator only works on local files. (Correct?)

$dir = "\\\\server\\share"; opendir DH, $dir or die "Cannot open $dir: $!"; @files = ( readdir DH ); print "$_\n" for @files;

This prints a directory listing, but the file test operators (-d, -M, etc.) can't be used to determine files from directories, timestamps, and such.
$dir = "\\\\server\\share"; @files = `dir $dir /b /a-d`; print @files;

This works to list only the remote files, but please don't tell me I need to call DOS commands for this kind of stuff! If I want to copy/move/delete remote files, will I need to use external commands again?

There's clearly a huge Win32 hole in my Perl knowledge here. There must be modules to help with this, but I haven't found them. As always, I appreciate your advice/experience/gentle beatings in the right direction.

-E

Replies are listed 'Best First'.
Re: Manipulating files on Windows shares
by BrowserUk (Patriarch) on May 01, 2003 at 22:58 UTC

    Try print $_, $/ for <//hostname/sharname/*>

    Or print $_, $/ for <//${ENV{COMPUTERNAME}}/perl/*>; to access a local share without hardcoding the local hostname. This work under NT, but I'm not sure about other flavours 95/98 etc.

    or

    open DH, '\\hostname\sharename\' or die $!; # NB: single quotes # or open DH, "\\\\hostname\\sharename\\" ... if you use double quotes print $_, $/ for readdir DH;

    You could also net use x: \\server\share from the command line or the "Map network drive" from the explorer or Win32::NetResource* to map a local drive specifier to the remote share and then use the standard

     print $_, $/ for <x:/*>; syntax to access it.

    * That said, I don't understand the pod for this module enough to offer a code sample for doing this.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Thanks BrowserUk! Apparently I had a slash/backslash problem -- the angle operator only likes foward slashes, but your example works, and I was able to fix my code. The file test operators work on the shares as well, which is terrific! Sorry it took so long to follow up on this.

      -E

Re: Manipulating files on Windows shares
by atnonis (Monk) on May 01, 2003 at 22:07 UTC
    Hi!
    I've searched my ActiveState Documentantion and I've found the following modules:
    use Win32::File;
    which you can get the attritubes of folders/files and
    use Win32::FileSecurity;
    which you can for example set or unset permission and such things of a file
    it should be good if you search Win32 on cpan for further modules/documentation. Hope that helps.
    Atnonis!
Re: Manipulating files on Windows shares
by TVSET (Chaplain) on May 02, 2003 at 16:37 UTC