We help IT Professionals succeed at work.

How do I get who has opened a file in a shared folder using a windows shell script or dos script

238 Views
Last Modified: 2010-04-16
Here's my situation:

My application acceses a csv file during the job run. However the same file can be accessed by business users at the same time. If that happens, my application has to send out an email notification to the user. I can integrate the script file with my application, which in turn can send an email notice.

Thanks
Raj
Comment
Watch Question

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Unless they have the file locked for write by something like Excel it quite possibly won't be possible to tell, e.g.

dir > test.csv
notepad test.csv
del test.csv

Will allow you to delete the file.  If you open it with Excel using START test.csv though it will be locked.

One way is to try and rename it and if it fails then you know it is unavailable, e.g.

rename test.csv test.xxx
if errorlevel 1 goto :errorcode

REM continue with your process using test.xxx or rename back with  rename test.xxx test.csv
goto :eof
:errorcode
echo There has been an error
goto :Eof

Commented:
Here is a vbscript adapted from the second one from this article:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0216.mspx

Paste it into a text file with a .vbs extension.  Customize the value of the strComputer variable with the name of the server hosting the .csv file.  Customize the value of the strFile variable with the path to the .csv on that server.  Running the script will echo the name of the user who has that file open.


strComputer = "server01"
strFile = "d:\files\filename.csv"
 
On Error Resume Next
 
Set objConnection = GetObject("WinNT://" & strComputer & "/LanmanServer")
Set colResources = objConnection.Resources
 
For Each objResource in colResources
    If LCase(objResource.Path) = LCase(strFile) Then
        strUser = objResource.User
        WScript.Echo strUser
    End If
Next

Open in new window

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Hmm, sorry came up for me in MSDOS section and didn't read it right there!

Steve

Author

Commented:
Hi Shift-3:

Thanks for the link and code you sent. Looks like this doesn't work with XP. However I am still testing.

Thanks
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.