Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for Open File

Status
Not open for further replies.

SamBozo

Programmer
Mar 15, 2007
3
US
How do you check to see if the file is all ready open before opening the file? I need to display a message if it is all ready open.
 
What do you mean by file? Checking for an open session? If so you need to loop through the session collection and checking its name property.
 
I'm screen scraping information off of the screen, opening a notepad file and printing to the notepad file. If the file is already open, the information does not get written and the macro stops and I have no clue. If I try to open the file and it is all ready open I want to display a messagebox. I thought I needed to do it before I tryed to open the file, but I don't. I can do it before or after, but I don't know how??
 
Is there a reason you open notepad and print to it? With EB you can write to a file without having to use an external program.
Code:
sFile = "C:\test.txt"
sData = "A String!"
Open sFile For Output As #1
Print #1,sData
Close #1
 
I'm not opening notepad (that's my brain, I use it to view file), my code looks pretty much like yours. But, I need to check to see if the file is open before I try to open it. The code "If Dir$..." will tell me if the file exists, but not if it is open.

Code:
Dim ScreenID As string
Dim IIFID1 As string
Dim IIFID2 As string
Dim IIFID3 As string
Dim IIFID4 As string
Dim IIFID5 As string
Dim IIFID  As string

ScreenID = Sess0.Screen.getstring(1, 2, 7)
If ScreenID = "UISP23M" Then
   IIFID1 = Sess0.Screen.getstring(3, 53, 1)
   IIFID2 = Sess0.Screen.getstring(3, 55, 7)
   IIFID3 = Sess0.Screen.getstring(3, 63, 2)
   IIFID4 = Sess0.Screen.getstring(3, 66, 3)
   IIFID5 = Sess0.Screen.getstring(3, 70, 3)
   IIFID  = ("E" + IIFID1 + IIFID2 + IIFID3 + IIFID4 + IIFID5)
            
Else
        
   If ScreenID = "UISP26M" Then
      IIFID1 = Sess0.Screen.getstring(6, 13, 1)
      IIFID2 = Sess0.Screen.getstring(6, 15, 7)
      IIFID3 = Sess0.Screen.getstring(6, 23, 2)
      IIFID4 = Sess0.Screen.getstring(6, 26, 3)
      IIFID5 = Sess0.Screen.getstring(6, 30, 3)
      IIFID  = ("H" + IIFID1 + IIFID2 + IIFID3 + IIFID4 + IIFID5)
   Else
      MsgBox "Image Button is only accessable from         Exception Screen or History Screen."
      Exit Sub
   End If
End If
If Dir$("C:\Program Files\IIFUIImageDLN.txt") = ("IIFUIImageDLN.txt") Then
   FileExist = True
   MsgBox "File C:\Program Files\IIFUIImageDLN.txt all ready exists"
   Exit sub
End If
Open "C:\Program Files\IIFUIImageDLN.txt" for Output As #1
Print #1, (IIFID)
Close #1
 
So, are you wanting to check if the file is open with notepad? Or are you wanting to check if the code has already opened the file?

This code would allow you check if it's open in notepad. It's in vbscript, but should work in EB.
Code:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'notepad.exe'")
If colItems.Count > 0 Then
  For Each objItem In colItems
    MsgBox objItem.commandLine
  Next
Else
  MsgBox "Notepad is not open."
End If
 
Here's one way.
Code:
Sub Main()
   Dim curr_line As String
   
   On Error GoTo FILE_OPEN_ERR
   
   Open "C:\Input.txt" For Input As #1
      Open "C:\Input.txt" For Input As #1
      
      Close #1

CONTINUE:
      Do While Not EOF(1)
         Line Input #1, curr_line
         MsgBox curr_line
      Loop
   Close #1
 
   Exit Sub
FILE_OPEN_ERR:
   If Err = 55 Then
      MsgBox "FILE IS OPEN"
      GoTo CONTINUE
   ElseIf Err = 53 Then
      MsgBox "FILE NOT FOUND"
      Exit Sub
   End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top