Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Searching And Saving Files

AlonskiAlonski Member Posts: 12
What I want to do is to have a dialog open up that will take a folder and then save all of its contents of a certain type( lets say .txt) into another .txt file( lets say list.txt) where my program is executed( lets say C:/)

After that is over I want to be able to enter a name of a file and then after I enter the name of the file a search is made on list.txt. If the file is found a boolean( lets say found) is set to true, else it is set to false.

Anyone know what I am talking about?

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : What I want to do is to have a dialog open up that will take a
    : folder and then save all of its contents of a certain type( lets say
    : .txt) into another .txt file( lets say list.txt) where my program is
    : executed( lets say C:/)
    :
    : After that is over I want to be able to enter a name of a file and
    : then after I enter the name of the file a search is made on
    : list.txt. If the file is found a boolean( lets say found) is set to
    : true, else it is set to false.
    :
    : Anyone know what I am talking about?
    :

    VB.NET or VB6?

    VB6 has a Dir() function for searching and an Open statement for opening files.
    VB.NET uses Classes for this.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • AlonskiAlonski Member Posts: 12
    Using VB6

    Thanks
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Using VB6
    :
    : Thanks
    :

    Alright. Look into how to work the Dir function.
    Basically, you can pass it a search string "C:Dir*.txt" and then consecutive calls will list all the files.

    [code]
    Dim sDir As String
    Dim sFound As String

    'TODO: You might have to try options at the second parameter
    sDir = Dir("C:Some dir*.txt")
    Do While sDir <> ""
    sFound = sFound & sDir & vbCrLf
    'Call Dir() without parameters to continue the search
    sDir = Dir()
    Loop
    [/code]
    This code might need some tweaking... Dir has a second parameter with some search options you might want to look into.

    Then to save it to a file:
    [code]
    On Error Resume Next

    'Try and delete the file. If it does not exist, an error is
    ' raised and this error is discarded by 'On Error Resume Next'
    Kill "C:File list.txt"

    Open "C:File list.txt" For Binary Access Read Write Lock Write As #1

    Put #1, , sFound

    Close #1
    [/code]

    Then to load the file again, use a similair statement:
    [code]
    Dim sList As String

    Open "C:File list.txt" For Binary Access Read Write Lock Write As #1

    'With Binary access, the string length determines the amount of
    ' characters read
    sList = Space$(LOF(1))
    Get #1, , sList

    Close #1
    [/code]

    Now you have to search the string for the filename you enter.
    If you're comfortable with arrays, use Split with delimiter vbCrLf (new line), and for each entry in the array you get, check if it matches the search string.
    If not, you could use the InStr functions. This is actually a bit harder because you have to do additional checking.
    For instance, if you search for "textfile.txt", and use InStr, then "my textfile.txt" will also show up as a hit.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • AlonskiAlonski Member Posts: 12
    Aight I think it is working correctly thanks alot for the help will keep you updated if it works
Sign In or Register to comment.