469,293 Members | 1,319 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

Check if a file is open in VB Script

283 100+
Hello,

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!

Thanks,

Slen

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")
  3.  
  4. If x = vbOK then 
  5.  
  6. Const FULL_PATH = "C:\DatabaseTestFolder"
  7. Set fso = CreateObject("Scripting.FileSystemObject")
  8.  
  9. BuildPath FULL_PATH
  10. CreateFile 
  11.  
  12. End if
  13.  
  14. Sub CreateFile()
  15. dim filesys
  16. dim strFile
  17. dim strDest
  18.  
  19. strFile = "C:\Documents and Settings\Desktop\Test.accde" 
  20. strDest = "C:\DatabaseTestFolder\Test.accde"
  21.  
  22.  
  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)
  27.  
  28.    End If
  29.  
  30. If err.Number <> 0 then
  31.     msgbox(err.Description &  " " & err.Number)
  32. Else
  33.     msgbox("Testing")
  34.  
  35. End if
  36.  
  37. End Sub
  38.  
  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
  45.  
Feb 14 '12 #1
4 13924
slenish
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")
  2.  
  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" 
  6.  
  7. If objFSO.FileExists (FILE1) then
  8.  
  9. MsgBox "The Program is Currently Open." 0 "Warning Program Open"
  10.  
  11. Else
  12.  
  13. 'Some other Code
  14.  
  15. End if
  16.  
Hope this helps anyone else out that has the same problem.

Take care
Feb 17 '12 #2
Rabbit
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
ADezii
8,800 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
  3.  
  4. 'Absolute PATH to File
  5. strFileName = "C:\Test\Test.mdb"
  6.  
  7. If Dir$(strFileName) = "" Then
  8.   MsgBox strFileName & " is not in the specified PATH!"
  9.     Exit Sub
  10. End If
  11.  
  12. Open strFileName For Binary Access Read Write Lock Read Write As #1
  13. Close #1
  14.  
  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
NeoPa
32,173 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

Post your reply

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

Similar topics

reply views Thread by Giraud, Sebastien | last post: by
1 post views Thread by Neil MacGaffey | last post: by
1 post views Thread by Dfenestr8 | last post: by
3 posts views Thread by Farshid Lashkari | last post: by
5 posts views Thread by bvidinli | last post: by
5 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
2 posts views Thread by kumarboston | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.