Results 1 to 7 of 7

Thread: Reading From A File

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Reading From A File

    I have a text file and it contains a list of file names. Is there a way where I can pull each file name one by one and check to see if the files exists then delete it? Then if the process gets to the end of the file it can just stop?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Reading From A File

    Use FileListBox, open it to the folder with the files then use For Next loop to go through all of the files (or are the files in a classic ListBox). You can use Kill function to delete a file and Dir to check if it exists.

    There are a lot of examples of using those in VB6, just use Search on the forums http://www.vbforums.com/search.php

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Re: Reading From A File

    Do I have to use the FileListBox?

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Reading From A File

    I may have missunderstood what you were trying to do at that point. Is this correct: You have a list of files (possibly in a list box) and you want to check if each of them exists on the computer, and if it does delete the file?

  5. #5
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Reading From A File

    Well basically it's just a matter of loading the textfile into a variable, an array preferably.
    The method for this depends on the format of the file.
    Then you look for a FileExist function; try codebank, EllisDee had something like it if I recall correctly
    And finally you'll run the array through the FileExist function.
    Delete it. They just clutter threads anyway.

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Reading From A File

    Try this that uses just basic VB functions:
    Code:
    Sub KillFiles()
        Dim sFileListPath As String
        Dim arFiles() As String
        Dim i As Long
        
        sFileListPath = "C:\MyFolder\FileList.txt"
        
        Open sFileListPath For Input As #1
        arFiles = Split(Input(LOF(1), 1), vbCrLf)
        Close #1
        For i = 0 To UBound(arFiles)
            arFiles(i) = Trim(arFiles)
            If Len(arFiles(i)) Then
                If Len(Dir(arFiles(i), vbHidden)) Then
                    Kill arFiles(i)
                    Debug.Print "Killed:    "; arFiles(i)
                Else
                    Debug.Print "Not Found: "; arFiles(i)
                End If
            End If
        Next
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Reading From A File

    This approach uses two string arrays. You could also
    open a 2nd file & write to that when an existing file is found.
    Another approach might be to use an ArrayDelete function to
    weed out the non-existing files.

    Code:
    Option Explicit
    Private Sub Form_Load()
     Dim i As Long
     Dim FA() As String 
     Dim NFA() As String
     Dim NewCnt As Long
     Dim FileText As String
     Dim hFile As Long
     'read the entire file
     '******change to proper path**********
     FileText = ReadFileBinary("C:\Files.txt")
     If Len(FileText) Then 'any files?
      FA = Split(FileText, vbNewLine) 'create zero based array
      For i = 0 To UBound(FA)
       If Len(FA(i)) Then 'in case of empty lines
        If FileExists(FA(i)) Then
         NewCnt = NewCnt + 1 'yes, add to NewFile array NFA
         ReDim Preserve NFA(1 To NewCnt)
         NFA(NewCnt) = FA(i)
        End If 'FileExists(FA(i))
       End If 'Len(FA(i))
      Next
      If NewCnt Then 'any existing files?
       hFile = FreeFile 'yes, write the array to file
       'here you might want to write to a different file
       Open "C:\Files.txt" For Output As hFile 'effectively kills the old file
       For i = 1 To NewCnt
        Print #hFile, NFA(i)
       Next
       Close hFile
      Else
       'none of the files exist
       'so do whatever--perhaps kill the textfile
      End If 'NewCnt
     End If 'Len(FileText)
    End Sub
    
    Private Function ReadFileBinary(ByVal sFilePath As String) As String
     Dim hFile As Long
     hFile = FreeFile
     Open sFilePath For Binary As #hFile
     ReadFileBinary = String$(LOF(hFile), Chr$(0))
     Get #hFile, , ReadFileBinary
     Close #hFile
    End Function
    Public Function FileExists(ByVal sFile As String) As Boolean
     Dim eAttr As Long
     On Error Resume Next
     eAttr = GetAttr(sFile)
     FileExists = (Err.Number = 0) And ((eAttr And vbDirectory) = 0)
     On Error GoTo 0
    End Function

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