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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
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

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.
NVITEnd-user supportCommented:
You could make a .bat file of it. When you run the .bat:
Whohasfile.bat "filename.doc"

Open in new window

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

BFanguyAuthor Commented:

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.
NVITEnd-user supportCommented:
Hi BFanguy,

Just wondering if you still need help with this...
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!
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" 
   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"
      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.
BFanguyAuthor Commented:
Thanks for all of your help.
NVITEnd-user supportCommented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.