472,342 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Check if a file is open in VB Script

283 100+

I have been searching and searching to try and find a way to check and see if a file is open or not but I have had no luck so far.

The over all bigger picture is that I wrote a vb script to run as an update, but before running the update I want to check if the program is open or not and if it is to prompt the user that the program is open and that they need to close it before the update will run.

Any help would be much appreciated!



Here is what I have so far. Just trying to figure out how I can add to this to check if the file is open...

Expand|Select|Wrap|Line Numbers
  1. Dim x  
  2. x=MsgBox("Do you wish to install the Program?",1,"File Install")
  4. If x = vbOK then 
  6. Const FULL_PATH = "C:\DatabaseTestFolder"
  7. Set fso = CreateObject("Scripting.FileSystemObject")
  9. BuildPath FULL_PATH
  10. CreateFile 
  12. End if
  14. Sub CreateFile()
  15. dim filesys
  16. dim strFile
  17. dim strDest
  19. strFile = "C:\Documents and Settings\Desktop\Test.accde" 
  20. strDest = "C:\DatabaseTestFolder\Test.accde"
  23. set filesys=CreateObject("Scripting.FileSystemObject")
  24. If filesys.FileExists(strFile) Then
  25.    filesys.CopyFile strFile, strDest
  26.    MsgBox("The file has been saved to your C:\Drive as " & strDest)
  28.    End If
  30. If err.Number <> 0 then
  31.     msgbox(err.Description &  " " & err.Number)
  32. Else
  33.     msgbox("Testing")
  35. End if
  37. End Sub
  39. Sub BuildPath(ByVal Path)
  40. If Not fso.FolderExists(Path) Then
  41. BuildPath fso.GetParentFolderName(Path)
  42. fso.CreateFolder Path
  43. End If
  44. End Sub
Feb 14 '12 #1
4 15219
283 100+
Well I guess I will answer this one myself since no one else replied and I figured it out.

If you should have the same problem as me and you are trying to check if a file is open this is what i did...

Expand|Select|Wrap|Line Numbers
  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  3. File1 = "C:File location\file name.Open file type"
  4. 'example since i was looking for a database that was open I did this
  5. 'File1 = "C:\TestFolder\Database1.laccdb" 
  7. If objFSO.FileExists (FILE1) then
  9. MsgBox "The Program is Currently Open." 0 "Warning Program Open"
  11. Else
  13. 'Some other Code
  15. End if
Hope this helps anyone else out that has the same problem.

Take care
Feb 17 '12 #2
12,516 Expert Mod 8TB
Just so you're aware, that doesn't do what you think it does. It only checks if a file exists, not if it's open. In this case, I think your true purpose is to check whether or not someone had a .mdb file open. So you're checking whether or not the lock file exists. However, you need to be aware that Access is bad at cleaning up the lock file so it may exist even if the file is not open.

I am also moving the thread to the Access forum where you may get more help.
Feb 17 '12 #3
8,832 Expert 8TB
The following In-Line Code will check and see if a File, (in this case an Access Database), is Open or not:
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Dim strFileName As String
  4. 'Absolute PATH to File
  5. strFileName = "C:\Test\Test.mdb"
  7. If Dir$(strFileName) = "" Then
  8.   MsgBox strFileName & " is not in the specified PATH!"
  9.     Exit Sub
  10. End If
  12. Open strFileName For Binary Access Read Write Lock Read Write As #1
  13. Close #1
  15. 'If an error occurs, the File/Document is Open
  16. If Err.Number <> 0 Then
  17.   MsgBox "Error# " & CStr(Err.Number) & " - " & Err.Description
  18.     Err.Clear
  19. End If
Feb 17 '12 #4
32,511 Expert Mod 16PB
I've reset the Best Answer as we don't encourage members to set their own posts as Best Answer anyway, but particularly when it doesn't even answer the question adequately (or at all).
Feb 20 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Giraud, Sebastien | last post by:
I should work cause it's made for it :) I had same problem checking socket bind and try did help me well :) -----Message d'origine----- De :...
by: Neil MacGaffey | last post by:
Hi - I'm new to python and am stymied by something that should be simple. The file open command below works fine on my desktop machine, but I...
by: Dfenestr8 | last post by:
Earlier I posted asking for a file browsing script, for a GUI app I'd written. Basically, I was after a graphical tool with which I could search...
by: Farshid Lashkari | last post by:
Hi, My goal is to detect all (or most) file dependencies of a script (i.e. modules, dlls, data files). Currently, after a script is finished...
by: fatjoez | last post by:
Hey there. I've been trying to modify my file upload script so that it handles 10 files instead of one. i was thinking the most straightforward...
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object....
by: bvidinli | last post by:
is there a way to find out if file open in system ? - please write if you know a way other than lsof. because lsof if slow for me. i need a faster...
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am able to upload files to a server with a .net web application. The problem is I am providing a hyperlink so the user can open the file after...
by: apurvaG | last post by:
Hi All, I wanted to know, can I check in action script whether the browser window which loads my swf file has been closed? Basically I wanted to...
by: kumarboston | last post by:
Hi All, How do we open certain number of files in for loop. In my case I have couple of files with name scd_1_2.dat here, the number 1 goes till...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.