|
-
Apr 2nd, 2010, 04:06 PM
#1
Thread Starter
Fanatic Member
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?
-
Apr 2nd, 2010, 04:10 PM
#2
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
-
Apr 2nd, 2010, 05:00 PM
#3
Thread Starter
Fanatic Member
Re: Reading From A File
Do I have to use the FileListBox?
-
Apr 2nd, 2010, 05:01 PM
#4
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?
-
Apr 2nd, 2010, 05:07 PM
#5
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.
-
Apr 2nd, 2010, 06:18 PM
#6
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
-
Apr 3rd, 2010, 07:31 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|