vb code to show who has a file open on a 2012r2 file server, from a 2008R2 Remote desktop server

I need vb or vba code that can tell me what user has a file open on a 2012R2 file Server Share, from a 2008R2 Remote Desktop Session.

Currently the call IT and we open Shared Folders-Open Files and scroll down to find who has the file open.  I want them to be able to either, right click the file and have an option to "Who has this file open"  or open a vbs script and pick the file and it tell them who has the file open.

Any help is greatly appreciated.
BFanguyAsked:
Who is Participating?
 
NVITCommented:
You could make a .bat file of it. When you run the .bat:
Whohasfile.bat "filename.doc"

Open in new window


Whohasfile.bat
openfiles /query /v /fo table | find /i %1| sort /+27 >files.txt

Open in new window

0
 
NVITCommented:
Does it have to be vbs? A simple cmd could do it. e.g. This sends the results to a csv that you can view in excel:
openfiles /query /fo "csv">files.csv

Open in new window


To query a remote server:
openfiles /s servername /query /fo "csv">files.csv

Open in new window


Sorted by username. My sort column is 27. Yours may differ. Results to a txt file, readable by notepad:
openfiles /query /v /fo table | sort /+27 >files.txt

Open in new window

0
 
BFanguyAuthor Commented:
Thanks, I tried: openfiles /S fileserver /U admin /p password /FO CSV>OpenFiles.csv
it list all the open files on our fileserver (34,000).
I just need to know who has 1 file open.

I'll try to figure out if i can search through the .csv file from vbscript - worth a shot.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
BFanguyAuthor Commented:
sweet.

i will give them an interface to run the command: openfiles /query /v /fo table | find /i %1| sort /+27 >files.txt and then open the txt file from vbscript.  I'll post my results.

thank you very much.
0
 
NVITCommented:
Hi BFanguy,

Just wondering if you still need help with this...
0
 
BFanguyAuthor Commented:
I have not had the time to write the vb interface, leaving this open for now, but will award the points to you after i post it.
thank you very much!
0
 
BFanguyAuthor Commented:
So i took your advise and created the following vbscript code to prompt the user for the file name:
Option Explicit
Dim objNet
Dim Network_User
Dim objUser
DIM fso    
Dim Message, result, Title
Dim Shell
Dim sTheProgram

Message = "Enter the filename (or partial filename) to see who has the file open"
Title = "File Open By"
result = InputBox(Message, Title)
If result = "" Then
   msgbox "No File name entered" 
Else
   Set objNet = CreateObject("WScript.NetWork") 
   Network_User = objNet.UserName
   set shell = createobject("WSCRIPT.SHELL")
   sTheProgram = "c:\Whohasfile.bat """ & result & """,""" & Network_User & """"
   shell.Run sTheProgram,1,True
   Set fso = CreateObject("Scripting.FileSystemObject")
   If fso.FileExists("C:\Open_Files-" & Network_User & ".txt") Then
      shell.Run "notepad.exe C:\Open_Files-" & Network_User & ".txt"
      WScript.Sleep 1000
      Shell.AppActivate "Open_Files-" & Network_User & ".txt - Notepad"
   Else
      msgbox "No open files for: " & result
   End If
   Set Shell = Nothing
   set fso = Nothing
End If

Open in new window


this is the .bat file
C:\Windows\System32\openfiles.exe /query /S FILE /U XXX\user /P password /v /fo table | find /i %1| sort /+27 >c:\Open_Files-%2.txt

Open in new window


Works like a charm on Windows 2008R2 Remote Desktop Servers and Windows 7 workstations.
0
 
BFanguyAuthor Commented:
Thanks for all of your help.
0
 
NVITCommented:
Hi BFanguy,

Thanks for the update. I'm glad it worked out for you.
Your code looks good. I could use it.

Have a good one. Aloha!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.