Check if a file is open in VB Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    Check if a file is open in VB Script

    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...

    Code:
    Dim x  
    x=MsgBox("Do you wish to install the Program?",1,"File Install")
    
    If x = vbOK then 
    
    Const FULL_PATH = "C:\DatabaseTestFolder"
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    BuildPath FULL_PATH
    CreateFile 
    
    End if
    
    Sub CreateFile()
    dim filesys
    dim strFile
    dim strDest
    
    strFile = "C:\Documents and Settings\Desktop\Test.accde" 
    strDest = "C:\DatabaseTestFolder\Test.accde"
    
    
    set filesys=CreateObject("Scripting.FileSystemObject")
    If filesys.FileExists(strFile) Then
       filesys.CopyFile strFile, strDest
       MsgBox("The file has been saved to your C:\Drive as " & strDest)
    	
       End If
    
    If err.Number <> 0 then
    	msgbox(err.Description &  " " & err.Number)
    Else
        msgbox("Testing")
    
    End if
    
    End Sub
    
    Sub BuildPath(ByVal Path)
    If Not fso.FolderExists(Path) Then
    BuildPath fso.GetParentFolderName(Path)
    fso.CreateFolder Path
    End If
    End Sub
  • slenish
    Contributor
    • Feb 2010
    • 283

    #2
    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...

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

    Take care

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      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.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        The following In-Line Code will check and see if a File, (in this case an Access Database), is Open or not:
        Code:
        On Error Resume Next
        Dim strFileName As String
        
        'Absolute PATH to File
        strFileName = "C:\Test\Test.mdb"
        
        If Dir$(strFileName) = "" Then
          MsgBox strFileName & " is not in the specified PATH!"
            Exit Sub
        End If
        
        Open strFileName For Binary Access Read Write Lock Read Write As #1
        Close #1
        
        'If an error occurs, the File/Document is Open
        If Err.Number <> 0 Then
          MsgBox "Error# " & CStr(Err.Number) & " - " & Err.Description
            Err.Clear
        End If

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          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).

          Comment

          Working...