1.

Solve : Deleting Files with .vbs script?

Answer»

I found this .vbs script in some old posts and have modified it for my application.  Under my main folder C:\Archive_Data I have subfolders that I would like the script to delete as well as the FILES in them.  As written the code deletes the files but leaves the empty folders.  Could "Sidewinder" or another show me what to add to accomplish this?  Thank you for your help.

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("E:\Archive_Data")           'point to your directory
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
   ShowFiles objFolder
Next

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
         If objFile.DateCreated < date - 38 Then
            'WScript.Echo objFile & " " & objFile.DateCreated
                                fso.Deletefile(objFile)    'commented out
         End If
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
End Subwell, try changing:
Code: [Select]   For Each SubFolder In s
      ShowFiles SubFolder
   Next

to:

Code: [Select]   For Each SubFolder In s
      ShowFiles SubFolder
      if Subfolder.Files.count=0 and subfolder.subfolders.count=0 then
          subfolder.deletefolder
      end if
   Next



another possible way would be to place, instead of the above:

Code: [Select]if k.files.count=0 and k.subfolders.count=0 then
    k.DeleteFolder
end if
at the end of the showfiles procedure.

I didn't test this however.

The idea is to delete the folder if it no longer contains any files or folders itself.

Here would be the fill script:

Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("E:\Archive_Data")           'point to your directory
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
   ShowFiles objFolder
Next

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
         If objFile.DateCreated < date - 38 Then
            'WScript.Echo objFile & " " & objFile.DateCreated
                                fso.Deletefile(objFile)    'commented out
         End If
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next

    if k.files.count=0 and k.subfolders.count=0 then
        k.DeleteFolder
    end if

End Sub


I haven't used microsoft's filesystemobject in a while though, I rolled my own hope this works!BC_Programmer, thank you so much for your quick reply, here is what I ENDED up with and it works perfect!!!  I hope someday I can learn enough from this site to offer help RATHER than ask for it!!  Very much appreciate your TIME!!
Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("E:\Archive_Data")           'point to your directory
Set colSubFolders = f.SubFolders

For Each objFolder in colSubFolders
   ShowFiles objFolder
Next

Sub ShowFiles(Fld)
   Set k = fso.GetFolder(Fld)
   Set s = k.SubFolders
   Set kf = k.Files
   
   For Each objFile In kf
         If objFile.DateCreated < date - 45 Then
            WScript.Echo objFile & " " & objFile.DateCreated
                                'fso.Deletefile(objFile)    'commented out
         End If
   Next
   
   For Each SubFolder In s
      ShowFiles SubFolder
   Next
      If k.files.count=0 and k.subfolders.count=0 then
         fso.DeleteFolder(objFolder)
   end if
End SubCool! glad I could help!



Discussion

No Comment Found