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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Current sub deletes from root, need to delete from subfolders 1

Status
Not open for further replies.

radiance

Programmer
Jan 4, 2003
164
US
Hello.

The following sub deletes files in the root. How can I build this out so that expired files in subfolders are deleted as well?

I have several main folders at the top level and each main folder has 2 subfolders. Those subfolders contain the files.


Public Module deleteFiles

Public Sub deleteExpFiles()

Dim fso, Folder, File1, FileCount
Dim intDel = 2
fso = CreateObject("Scripting.FileSystemObject")

'set this as a case statement to loop through all folders
Folder = fso.GetFolder("C:\Documents and Settings\orbit\My Documents\rotation")
FileCount = Folder.Files

'Create log file and then delete file
Dim objOutputFile = fso.CreateTextFile("C:\Documents and Settings\orbit\My Documents\rotation\ExpiredFiles.log")
With objOutputFile
.WriteLine("=======================================================")
.WriteLine("Expired Files for " & Date.Now)
.WriteLine("=======================================================")
.WriteLine()
End With

'loop
For Each File1 In FileCount
'delete files that are older than 7 days, for testing, do 3
If DateDiff("d", File1.DateLastModified, Now) > intDel Then
objOutputFile.WriteLine("Deleted " & vbCrLf & File1.Path & vbCrLf & "Original file created on " & File1.DateLastModified & vbCrLf & "Removed at " & Date.Now & vbCrLf & vbCrLf)
File1.Delete()
End If
Next

'end writing to log file and loop
End Sub
End Module
 
Sorry, Why don't you use:
System.IO.FileInfo
System.IO.DirectoryInfo

You could use a function loops with this kind of structure:

Code:
   Public Sub DeleteExpFiles()

      Dim FolderPath As String = "C:\....."

      Dim OutputFile As New IO.StreamWriter("C:\...")

      OutputFile.WriteLine("Starting...") 'Or something

      DeleteInnerFiles(FolderPath, OutputFile)

      OutputFile.WriteLine("Finished...")
   End Sub

   Public Sub DeleteInnerFiles(FolderPath As String, OutputFile As IO.StreamWriter)

      Dim Folder As New IO.DirectoryInfo(FolderPath)

      For Each File As IO.FileInfo In Folder.GetFiles
         If [Needs Deleting] Then
             OutputFile.WriteLine("Deleting File: " & File.FullName)
             File.Delete()
         End If
      Next

      For Each SubFolder As IO.DirectoryInfo In Folder.GetDirectories
          DeleteInnerFiles(SubFolder.FullName, OutputFile)
      Next

   End Sub
 
Before anyone else spots it, you'll need to add:

OutputFile.Close()

to the end of the DeleteExpFiles() Sub :~/
 
thank you for your reply.

would this process enable me to delete the files within all of the subfolders, or would I have to create a separate FolderPath for each sub?

For example (I have several main level folders)
Main Folder 1
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\

Shared (Subfolder 1 in Main Folder 1)
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Shared\

Private (Subfolder 2 in Main Folder 1)
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Private\
 
It will first process the files in the current folder, then if the folder contains sub folders, it will go through the sub folders, then throught the sub folder's folders:

C:\Documents and Settings\orbit\My Documents\rotation\sequence1\
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Shared\
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Shared\Other
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Shared\Other\Another
....
Then it will come back out and do:

C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Private
C:\Documents and Settings\orbit\My Documents\rotation\sequence1\Private\Other
....

You may need to add Application.DoEvents() in the For...Next loops, if your app seems to hang.
 
ok...it doesn't seem to be writing anything to the log file (although there are files in the subfolders...
 
I also had this in my original sub:

For Each file As IO.FileInfo In Folder.GetFiles
If DateDiff("d", file.DateLastModified, Now) > intDel Then
OutputFile.WriteLine("Deleted " & vbCrLf & file.FullName & vbCrLf & "Original file created on " & file.DateLastModified & vbCrLf & "Removed at " & Date.Now & vbCrLf & vbCrLf)
file.Delete()
Else
OutputFile.WriteLine("Nothing Deleted")
End If
Next


but file.Datelastmodified is not a recognized member of file.io.system....

i don't think creationDate is the best either...
 
also, I had to put in the path that the check should begin, but I get an error on the path...

C:\Documents and Settings\orbit\My Documents\rotation\ is where the check should begin. All subfolders fall from this path...

the error indicates that the path may be full

here, i have...

Dim FolderPath As String = "C:\Documents and Settings\orbit\My Documents\rotation\"

Dim OutputFile As New IO.StreamWriter("C:\Documents and Settings\orbit\My Documents\rotation\")

'there was a log file too, should I place that back in the outputfile??
 
It will delete your log file, you'll need to put it somewhere else.

The error is because the outputfile is locking the file, if you change the log file's location it will correct this error.

Have you got it working now?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top