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

delete files wscript

Status
Not open for further replies.

Sasquatch69

Programmer
Sep 20, 2002
19
CA
Hi, i try to delete all file in a directory where the date is less than a specific date(date - 10 day)
My Code:

If CStr( Day((Now()-NbDay))) < 10 Then
jour = &quot;0&quot; + CStr( Day((Now()-NbDay)))
Else
jour = CStr( Day((Now()-NbDay)))
End if

mois = CStr( Month((Now()-NbDay)))
annee = CStr( Year((Now()-NbDay)))
'creationDate format
dtmTargetDate = annee + mois + jour + &quot;000000.000000+***&quot;

strComputer = &quot;.&quot;
Set objWMIService = GetObject(&quot;winmgmts:&quot; _
& &quot;{impersonationLevel=impersonate}!\\&quot; & strComputer & &quot;\root\cimv2&quot;)

Set colFiles = objWMIService.ExecQuery _
(&quot;SELECT * FROM CIM_DataFile WHERE and CreationDate < '&quot; & dtmTargetDate & &quot;'&quot;)
For Each objFile in colFiles
'display file for now..
WScript.StdOut.WriteLine objFile.creationdate
Next

It does'nt work! I Know i can use FileSystemObject(vbscript)
but i want to know which one is more fastest!!(1000 files to delete)

The error is in the select... problem with date!!

Thanks!!

Sorry for my bad english.
 
>(&quot;SELECT * FROM CIM_DataFile WHERE and CreationDate < '&quot; & dtmTargetDate & &quot;'&quot;)
IMHO you have a SQL syntax error as there is no condtion between the WHERE and the AND.

Hope This Help
PH.
 
ok, just a error when i paste the code, the &quot;and&quot; is not in the code. I thing the format of the date is not ok. any ideas...
 
Hello Sasquatch69,

[1] Your dtmTargetDate contains wildcard. It won't work for the wql---this is to the letter of documentation. (The documentation is often not accurate still for wmi.)
[2] Documentation says &quot;with clause&quot; accept comparison operator and datetime two(2) or several format. This does not work universally. So better to avoid comparison in the wql setting on the datetime. This can well be considered as a bug still.
[3] If you leave the creationdate part out of the with clause and screen the collection from the creationdate, you can still get what you want. But now, the comparison (>, < etc) should better be done with the yyyymmddHHMMSS.mmmmmmsUUU format. Other format claimed to be acceptable may not work---some always evaluate to true!!! (In a word, very incomprehensible/buggy still.)
[4] I think you can get the job done with pure wmi scripting. But, it is real slow in not trim down the retrieved objects sufficiently. My gut feeling is fso will do better. But, it is good exercise.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top