Results 1 to 6 of 6

Thread: [RESOLVED] Help with files!

  1. #1

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Resolved [RESOLVED] Help with files!

    I have a form which opens a file when a button is clicked. I want to find out from another form, if the file has been opened and still open. How can I do that?

    Can I just set a variable to true when the user clicks to open the file and set it to false when it is closed? Or is there a better way of doing it?

    Also I have a txt file with contents like

    Some text
    %%%%
    text1
    %%%%
    a
    b
    c
    d
    %%%%

    The '%%%%' is a separator. I need to take every line between the separator and put it into a text box. How can I restrict strings according to the separator?

    Please help

    Cheers!
    Kumar

  2. #2
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Help with files!

    you may be able to do this with a couple of properties,
    say an FileOpened property and a FileClosed property.

    VB Code:
    1. private pFileOpened as boolean
    2. private pFileClosed as boolean
    3.  
    4. public property get FileOpened() as boolean
    5.   FileOpened = pFileOpened
    6. end property
    7.  
    8. public property get FileClosed() as boolean
    9.    FileClosed = pFileClosed
    10. end property

    This code can be placed in one form and called from the other form.

    for example:

    in one form:

    VB Code:
    1. filenumber = freefile()
    2. open "c:\yourFile.dat" for append as #filenumber
    3.  
    4. ' right after opening your file set the property
    5. pFileOpened = true

    this can be called from your second form like:

    VB Code:
    1. if form1.FileOpened = true then
    2.    ' do your stuff
    3. end if

    when checking if file is still opened:

    VB Code:
    1. Close #filenumber
    2. pFileClosed = true

    this can be checked from your second form like:
    VB Code:
    1. if form1.FileClosed = true then
    2.    ' file is closed -
    3. end if

    can also be checked for both properties:
    VB Code:
    1. if form1.FileOpened and not form1.FileClosed then
    2.     ' file is opened but not yet closed... do something
    3. end if

  3. #3
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Help with files!

    for text file you can split on the separator:

    VB Code:
    1. Public Function TextFromFile(fInStream As String) As String
    2.   'Returns text in file fInStream
    3.   'Example usage: Debug.Print TextFromFile_
    4.        ("C:\WINDOWS\Programs.txt")
    5.  
    6.   Dim i As Long, strText As String
    7.   i = FreeFile
    8.   strText = ""
    9.   Open fInStream For Input Lock Write As #i
    10.   Screen.MousePointer = vbHourglass
    11.   DoEvents
    12.   strText = StrConv(InputB$(LOF(i), i), vbUnicode)
    13.   Close #i
    14.   Screen.MousePointer = vbDefault
    15.   TextFromFile = strText
    16. End Function
    17.  
    18.  
    19. dim vFileContents as variant
    20. dim iLine as long
    21. vFileContents = split(TextFromFile , "%%%%")
    22.  
    23. ' now you have an array of lines that have been separated
    24. ' based on your separator and can access them as an array
    25.  
    26. text1.text = vFileContents(0)
    27. text2.text = vFileContents(1)
    28.  
    29. etc...

  4. #4

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: Help with files!

    Thanks a bunch, That worked.

    Cheers!
    Kumar

    Quote Originally Posted by ZeBula8
    for text file you can split on the separator:

    VB Code:
    1. Public Function TextFromFile(fInStream As String) As String
    2.   'Returns text in file fInStream
    3.   'Example usage: Debug.Print TextFromFile_
    4.        ("C:\WINDOWS\Programs.txt")
    5.  
    6.   Dim i As Long, strText As String
    7.   i = FreeFile
    8.   strText = ""
    9.   Open fInStream For Input Lock Write As #i
    10.   Screen.MousePointer = vbHourglass
    11.   DoEvents
    12.   strText = StrConv(InputB$(LOF(i), i), vbUnicode)
    13.   Close #i
    14.   Screen.MousePointer = vbDefault
    15.   TextFromFile = strText
    16. End Function
    17.  
    18.  
    19. dim vFileContents as variant
    20. dim iLine as long
    21. vFileContents = split(TextFromFile , "%%%%")
    22.  
    23. ' now you have an array of lines that have been separated
    24. ' based on your separator and can access them as an array
    25.  
    26. text1.text = vFileContents(0)
    27. text2.text = vFileContents(1)
    28.  
    29. etc...

  5. #5

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: Help with files!

    Super, That will do. Now I have another question. Here is my code:

    If Std0.pFileOpened = False Then
    SaveDialog.ShowSave
    Name_File = SaveDialog.FileName

    'opens a file, if file doesnt exist, it is created
    Open Name_File + ".dat" For Output As #3
    'opens a file, if file doesnt exist, it is created
    Std0.pFileOpened = True
    End If

    'write into file

    'Form1
    Print #3, Std1.desc1
    Print #3, "%%%%"
    Print #3, Std1.desc2a
    Print #3, "%%%%"
    Print #3, Std2.desc2b
    Print #3, "%%%%"
    Print #3, Std2.desc3
    Print #3, "%%%%"

    This code opens if the file is not open and appends to a file if a file is open. But how can i erase the contents of a file if it is open, and write the new stuff again?

    CHeers!
    Kumar

    Quote Originally Posted by ZeBula8
    you may be able to do this with a couple of properties,
    say an FileOpened property and a FileClosed property.

    VB Code:
    1. private pFileOpened as boolean
    2. private pFileClosed as boolean
    3.  
    4. public property get FileOpened() as boolean
    5.   FileOpened = pFileOpened
    6. end property
    7.  
    8. public property get FileClosed() as boolean
    9.    FileClosed = pFileClosed
    10. end property

    This code can be placed in one form and called from the other form.

    for example:

    in one form:

    VB Code:
    1. filenumber = freefile()
    2. open "c:\yourFile.dat" for append as #filenumber
    3.  
    4. ' right after opening your file set the property
    5. pFileOpened = true

    this can be called from your second form like:

    VB Code:
    1. if form1.FileOpened = true then
    2.    ' do your stuff
    3. end if

    when checking if file is still opened:

    VB Code:
    1. Close #filenumber
    2. pFileClosed = true

    this can be checked from your second form like:
    VB Code:
    1. if form1.FileClosed = true then
    2.    ' file is closed -
    3. end if

    can also be checked for both properties:
    VB Code:
    1. if form1.FileOpened and not form1.FileClosed then
    2.     ' file is opened but not yet closed... do something
    3. end if

  6. #6

    Thread Starter
    Member midway's Avatar
    Join Date
    Apr 2006
    Posts
    39

    Re: Help with files!

    any1?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width