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

delete folders that being with caa that are older than 14 days 2

Status
Not open for further replies.

moogeboo

MIS
Aug 7, 2003
28
US
Hi,

I'm trying to write this script that would delete all folders that begin with caa if the folders are older than 14 days. The script runs, but the folders are not deleted, even if they are older than 14 days. I want to be able to delete all files and subfolders within the caa* folder, as well as the caa* folder itself

here is the script:

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\")
OlderThanDate = DateAdd("d", -14, Date)
Set sf = f.SubFolders
For Each f In sf
If InStr(f, "caa" Then
If f.DateCreated < OlderThanDate Then
f.Delete
End If
End If
Next
msgbox "done"

Thanks for any tips,

Moogeboo
 
For Each f In sf

you reuse the f variable in your For Each Loop, which is, in some respects, still in use as you have Set it to nothing. whilst on it looks like it will work regardless personally i wouldnt reuse it in such a way.
you have a typo with your InStr statement (no ")"). I would be more explicit with the InStr statement with somethign like
If InStr(LCase(f.Name), "caa") Then
infact is the InStr really doing what you want? if you you want begins with then
If Left(LCase(f.Name), Len("caa")) = "caa" Then???

I would add some debugging information so you can see what is going on or not as the case may be

For Each f In sf
Wscript.Echo "parsing " & f.Name
If InStr(f, "caa" Then
Wscript.Echo "have a match with caa"
If f.DateCreated < OlderThanDate Then
Wscript.Echo "will try and delete " & f.Name
f.Delete
End If
Else
Wscript.Echo "do not have a match with caa"
End If
Next


 
>i wouldnt reuse it in such a way

This is a fairly valid reuse of a variable
 
beg to differ

If the code presented looked more like

Set f = fso.GetFolder("c:\")
OlderThanDate = DateAdd("d", -14, Date)
Set sf = f.SubFolders
Set f = Nothing
For Each f In sf

then i might be more inclined to agree with you. whilst strictly speaking you could then get away with it being classed as 'valid' i have to say i find it ugly to look at and has poor 'style' (lacking in good composition and symmetry) personally i like to think and visualize code in more dimensions than words on a page
 

[in a pretentious voice]

Well, allow me to add my 2 cents. If I were writing this, I would:
- add 3 char type identifiers to objects/variable names
- NEVER reuse an object/variable name in the same execution scope.
- everything lowercase and substantiated (explicit use of Order of Operations). If an object/variable/function name is comprise of multiple words, the first char of each subsequent word is capitalized (e.g. strMyString, getTimeStamp)
- program extensively (as mrmovie did)

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objRootFolder = objFSO.GetFolder("c:\")
set colSubFolders = objRootFolder.SubFolders

strExpirationDate = DateAdd("d", -14, Date)

strNamePrefix = "caa"
for each objFolder in colSubFolders
   if (left(objFolder.Name, len(strNamePrefix)) = strNamePrefix) then
      if (objFolder.DateCreated < strExpirationDate) then
         objFolder.Delete
      end if
   end if
next

msgbox "done"

[under my breath]
God, I hope it's right so I don't have to respond in a naive voice

-Geates
 
I like to capitalize on all key words, For Each In Set etc etc
woudl be nice to set some explicit garbage collection

Set colSubFolders = Nothing
Set objRootFolder = Nothing
Set objFSO = Nothing

the use of 'hungarian' style naming convention is an interestig one, in some circles it is recommended to avoid this practice as it leads to ugly implications when you want to cast your variable to a different type later on

the first IT related book i read was brilliant and i owe a lot to it 'HTML, Manual of Style'
 
I wasn't commenting on programming style. I was commenting on the assertion that this particular reuse of a variable was somehow wrong; it isn't, at least no more so than

i = "c:\example.txt"
'do something with i
i = "c:\anotherexample.txt"
'do something with new i

is wrong.

Neither does "i find it ugly to look at and has poor 'style' (lacking in good composition and symmetry)" make the reuse of f wrong, no matter how many dimensions you like to think in - although it is a perfectly reasonable personal opinion to hold concerning the conventions the OP has adopted. It is rather like suggesting that adding two integers together is wrong because someone used the wrong font size; the two are not related.



 
just because something doesnt cause a run time it does make it right? progamming is all about style, if your not interested in style then life in IT is pretty boring


+ the code shown isnt very readable and could cause confusion over the state of 'f'
+ the reuse of a variable in the current scope is perfectly acceptable but should be done so when it makes 'sense' or serves some purpose. having an additional variable in this case to represent the iterated subfolder would make things more readable and avoid any confusion and i can see no negative (other than a little memory usage)
+ in this instance colSubFolders is derived directly from the original instance of f. the use of colSubFolders after f is reassigned leads to the obvious question "but what if colSubFolders were dependant on the instance of f still being available?" (yes i appreaciate in this instance this not a problem and in general programing terms 'stack and heap considerations blaa blaa' this is highly unlikely to ever be a problem, in the literal sense logically this could be an issue and therefore the mind drifts to it)

my statement was 'personally i wouldnt use it that way'

 
thanks for the pointers mrmovie and geates. the script works well!

Moogeboo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top