Help getting started with Lanman

D

David Sanabria

Greetings!

I have just discovered the Win32::Lanman package and hope to use it to
solve a problem I am sure has already been solved...

I have an Excel file that is used as a data source across a network.
This file is commonly opened for reading from other Spreadsheets but I
need to disconnect any users before I can update the source.

If anyone has a script that demonstrates how this can be done, I would
greatly appreciate it.

What I need is to be able to use the NetFileEnum() function to get the
open files in directory c:\foo where my data.xls file lives. From there,
I would use the NetFileClose() function to close the file just before
copying a new one over it.

Any help or suggestions would be greatly appreciated!

Regards,
Dave

PS. I will post my own script once I'm done hacking on it.
 
D

David Sanabria

David Sanabria wrote:
[Snip]
PS. I will post my own script once I'm done hacking on it.

Here is the code I came up with in case anyone else needs something like
it. Enjoy!

#!perl -W

# **********************************************************************
# * NAME: MoveTemp.pl TYPE: Perl Script
# * VIS: script
# * AUTHOR: David Sanabria at thehartford CREATED: 22-Apr-2005
# *---------------------------------------------------------------------
# * PURPOSE: Look for an open file and close it before replacing with a
# * temp file
# *---------------------------------------------------------------------
# * CHANGE HISTORY:
# *
# *
# **********************************************************************

use strict;
use warnings;

use Win32::Lanman;
use Filehandle;
use File::Copy;


my $rootDir = "c:\\olsid\\reports\\r00031\\";
my $targetFile = "latest.xls";
my $tempFile = "Latest_tmp.xls";
my $targetID = '';
my $targetServer = "erdsimrem002";

my @fileEnums; #collection of enums
my $fileEnum; #iterator/individual object
my @fileInfo; #hash? with info from fileEnum() call
my $fileAttr; #hash element


#get Enums from server
if ( Win32::Lanman::NetFileEnum( $targetServer, $rootDir, '', \@fileEnums ))
{

#Loop through hash to find the file name if possible

open (RESULTS, ">Results.txt");

#copied & modified from Lanman example
foreach $fileEnum (@fileEnums)
{

if ( ${$fileEnum}{pathname} = ( "$rootDir$targetFile" ))
{
$targetID = ${$fileEnum}{id};

if( ! Win32::Lanman::NetFileClose( $targetServer, $targetID ) )
{
print RESULTS "Could not close file! Error: " .
Win32::Lanman::GetLastError();
close( RESULTS );
# Try to Replace old file
move( "$rootDir$tempFile", "$rootDir$targetFile");

die "Could not close file! Error: " .
Win32::Lanman::GetLastError();
}
}
}
# Try to Replace old file
move( "$rootDir$tempFile", "$rootDir$targetFile");

close( RESULTS );

}
else
{
#error handling here!
print RESULTS "Could not close file! Error: " .
Win32::Lanman::GetLastError();
close( RESULTS );
# Try to Replace old file
move( "$rootDir$tempFile", "$rootDir$targetFile");
die "An error occurred trying to get enums: " .
Win32::Lanman::GetLastError();
}

# Try to Replace old file
move( "$rootDir$tempFile", "$rootDir$targetFile");


__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top