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!

Deleted file if not in list 1

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
Hi,

I have a file(C:\temp\filelist_out.txt) that has a list of files. I need to compare the filelist_out to C:\temp and any file that isn't on the filelist_out that's in the dir, delete it from c:\temp.

Also, I need to take another file(C:\temp\filelist_in.txt) and do a compare to the same dir and if the file there is a file on the list that isn't in the dir, the copy it from C:\Archive and write that filename to C:\temp\filelist_out.txt.
 
This should help you with your first issue:

Code:
' Add a reference to Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim dict As Dictionary
Dim InStream As TextStream
Dim InputData As String
Dim fso_folder As Folder
Dim fso_file As File

Set fso = New FileSystemObject
Set dict = New Dictionary

Set InStream = fso.OpenTextFile("C:\temp\filelist_out.txt", ForReading)

Do
    InputData = InStream.ReadLine
    dict.Add InputData, InputData
Loop Until InStream.AtEndOfStream

Set fso_folder = fso.GetFolder("C:\temp")

For Each fso_file In fso_folder.Files
    If dict.Exists(fso_file) = False Then fso.DeleteFile fso_file, True
Next fso_file

Set fso_folder = Nothing
Set fso_file = Nothing
dict.RemoveAll
Set dict = Nothing
InStream.Close
Set InStream = Nothing
Set fso = Nothing
End Sub

Please clarify your second request as I am having trouble understanding it.

Swi
 
Thank you very much... the second part is as follows...

I need to read in a file(C:\temp\filelist_in.txt) and do a compare to the same dir(C:\temp) and if there is a file in the list that isn't in the dir, then copy it from C:\Archive to C:\temp.
 
I will post back a solution tonight. I am on my way out the door.

Swi
 
This should do it. I just switched some of the code around.

Code:
' Add a reference to Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim dict As Dictionary
Dim InStream As TextStream
Dim InputData As String
Dim fso_folder As Folder
Dim fso_file As File

Set fso = New FileSystemObject
Set dict = New Dictionary

Set fso_folder = fso.GetFolder("C:\temp")

For Each fso_file In fso_folder.Files
    dict.Add fso_file, fso_file
Next fso_file

Set InStream = fso.OpenTextFile("C:\temp\filelist_in.txt", ForReading)

Do
    InputData = InStream.ReadLine
    If dict.Exists(InputData) = False Then
        fso.CopyFile "C:\Archive\" & InputData, _
        "C:\temp\" & InputData, True
Loop Until InStream.AtEndOfStream

Set fso_folder = Nothing
Set fso_file = Nothing
dict.RemoveAll
Set dict = Nothing
InStream.Close
Set InStream = Nothing
Set fso = Nothing
End Sub

Swi
 
Looks like the first solution is deleting everything because it reading the file names with the full path... ie c:\test\testfile.txt instead of just testfile.txt.
 
One other thing is if the file isn't in the archive, i need it to write a log message that it file not found. Right now it errors out and crashes.
 
I used the following to remove the full path but it's still deleting everything in the directory.

Dfile = CreateObject

("Scripting.FileSystemObject").GetBaseName(fso_file)

If dict.Exists(Dfile) = False Then fso.DeleteFile fso_file, True
Next fso_file

 
For the first problem. It needs to read in the first file name in the directory then open and loop through the text file and if it finds the file name within the text file, do nothing but if it doesn't find the file name... delete that file.
 
If you want to delete the files in "C:\Temp\" simple change your lin of code to:

Code:
If dict.Exists(Dfile) = False Then fso.DeleteFile "C:\Temp\" & Dfile, True

Swi
 
This seems to work. The only problem now is that when it's reading in the file names from C:\Temp, isn't not reading the file extension.. ie .txt .dat

For Each fso_file In fso_folder.Files
Dfile = CreateObject("Scripting.FileSystemObject").GetBaseName(fso_file)
If dict.Exists(Dfile) = False Then fso.DeleteFile (fso_file), True
Next fso_file
 
I am getting an error running the delete process when i run this error code "runtime 457" on a win 2003 server.
 
That is because the key is already associated with another element in the associative array. In other words, you have duplicate file name entries in your list of files. To correct this change the following:

Code:
Do
    InputData = InStream.ReadLine
    [red]If dict.Exists(InputData) = False Then
      dict.Add InputData, InputData
    End If[/red]
Loop Until InStream.AtEndOfStream

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top