GetSubFolders with .vbs

Microsoft Windows xp professional w/serv...
August 3, 2010 at 12:01:49
Specs: Windows XP
I'm trying to delete files and folders in a directory older than 7 days old. It sees the Root folder being modified so it doesn't delete it, but if i change it so it will see the root being older than 7 days it deletes the root. How can I get it to delete the sub folders and sub files instead. Some of this is still just theory. Any help?

my code in vbs that is not working:
================================

dim fs,fo


set fs=CreateObject("Scripting.FileSystemObject")

'This is what I will want to do down the line
'set fo=fs.GetFolder("C:\Backup\%UserProfile%\%date%")

'This is what I've figured out so far
set fo=fs.GetFolder("C:\Backup")

For Each Modified in Fo.Files
If DateDiff("d", fo.DateLastModified, Now) > 7 Then Fo.Delete
Next

For Each Modified in Fo.subfolders
If DateDiff("d", fo.DateLastModified, Now) > 7 Then Fo.Delete
Next

set fo=nothing
set fs=nothing
===========================

I have a backup.bat that does this:

===========================
@echo off
:: variables
set drive=c:\Backup
set backupcmd=xcopy /s /c /e /h /i /r /y
set date=%date:/=-%\

%backupcmd% "C:\test" "%drive%\%UserName%\%date%\test"


See More: GetSubFolders with .vbs

Report •


#1
August 3, 2010 at 15:32:58
If DateDiff("d", Modified.DateLastModified, Now) > 7 Then Modified.Delete

On both instances.

Report •

#2
August 3, 2010 at 19:15:08
Your Awesome! I've tired that like a million times and have been working on this for 2 months reading everything online and I would always get errors. I don't remember exactly what I was doing before but that works like a charm! Thanks so much! :D

Report •

#3
August 3, 2010 at 20:02:16
How would you exclude, lets say the 5th and the 20th of each month from deletion? (payroll days)

Or better yet... how would you copy those days to a different directory then delete the 30 days Old?

what I've got but doesn't work:
================================

dim fs,fo

set fs=CreateObject("Scripting.FileSystemObject")

set fo=fs.GetFolder("C:\Backup")

For Each Modified in Fo.Files
If Date("d" , DateModified) = 5 Then DateModified.Move("c:\backup001")
Next

For Each Modified in Fo.Files
If Date("d" , DateModified) = 20 Then DateModified.Move("c:\backup001")
Next

For Each Modified in Fo.Files
If DateDiff("d", Modified.DateLastModified, Now) > 30 Then Modified.Delete
Next

For Each Modified in Fo.subfolders
If DateDiff("d", Modified.DateLastModified, Now) > 30 Then Modified.Delete
Next

set fo=nothing
set fs=nothing


Report •

Related Solutions

#4
August 6, 2010 at 07:53:26
Well, you haven't double posted yet today, so I might as well stop stalling and post this:

With CreateObject("Scripting.FileSystemObject").GetFolder("C:\Backup")
  For Each arr In Array(.Files, .SubFolders)
    For Each f In arr
      If DatePart("d", f.DateLastModified) = 5 Or DatePart("d", f.DateLastModified) = 20 Then
        f.Move "c:\backup001\"
      ElseIf DateDiff("d", f.DateLastModified, Now) > 30 Then
        f.Delete
      End If
    Next 'f
  Next 'arr
End With


Report •

#5
August 7, 2010 at 10:19:39
Thanks, Works like a charm! :D

Report •


Ask Question