Delete Folder Script - error: path not found

G

Guest

Dear Experts,

The following script deletes folders that are
more than 1 day old:

dtmYesterday = Date - 1

strYear = Year(dtmYesterday)

strMonth = Month(dtmYesterday)
If Len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If

strDay = Day(dtmYesterday)
If Len(strDay) = 1 Then
strDay = "0" & strDay
End If

strYesterday = strYear & strMonth & strDay

strFolderName = "D:\Reports\" & strYesterday

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(strFolderName)

and my folders are as follows:
D:\Reports\20051102
D:\Reports\20051103

If I run the above script on Nov 9 it would return me
the following message:
error: path not found
code: 800A004C
Source: Microsoft VBScript runtime error
(as 20051108 does not exist)

Question - how do I add a script to ignore folders
that do not exist?

Thanks in advance.
 
G

Guest

Thank you, Ramesh.

As the appended script deletes the folder which is I day old,
those that are more than 1 day old does not gets deleted.

Was wondering how do I script a looping to look for folders
older than I day old and then deletes it.

Thanks in advance.
 
G

Guest

Dear Experts,

I double click on the following script and there is no error
hilighted and the folders is not deleted, after many days
I am still puzzled. Example of the folders are
20051102, 20051103, 20051104.
Kindly help.
Thanks.

Sub Main()
Dim fso, folderobj, fldr, calcResult
Set fso = CreateObject("Scripting.FileSystem Object")
Set folderobj = fso.GetFolder("C:\Test")
For Each fldr in FolderObj.SubFolders
calcResult = DateDiff("d",fldr.DateCreated,Now)
If calcResult>2 Then fldr.Delete True
Next
End Sub
 
R

Ramesh, MS-MVP

Ken,

Try this:

Dim i, fso, f, f1, s, sf, BasePath, CalcResult, fNameArray()
BasePath = "C:\Test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(BasePath)
Set sf = f.SubFolders
For Each f1 in sf
calcResult = DateDiff("d",f1.DateCreated,Now)
if CalcResult > 2 then
ReDim preserve fNameArray(i)
fNameArray(i) = f1.Name
i = i + 1
end if
Next

For Each fName in fNameArray
FSO.DeleteFolder(BasePath & "\" & fName)
Next


--
Ramesh, Microsoft MVP
Windows XP Shell/User

Windows XP Troubleshooting
http://www.winhelponline.com
 
G

Guest

Dear Experts,

I have a screnario:

For example,

Today is 2006 Jan 9,

Remaining folders are:
20051201
20051202
20051205
20051206
20051207

From 20051208 thru 20060108 there are no folders created.

Number of folder_days to keep is 5.

Based on the critera of folders_days older than 7 days
based on the creation date, all the above folders would be
deleted but there is a need to keep the last 5 folder_days
as above.

How to prevent the last 5 days from deletion?

Any clues would be helpful.

Thanks in advance.
 
B

billious

Ken said:
Dear Experts,

I have a screnario:

For example,

Today is 2006 Jan 9,

Remaining folders are:
20051201
20051202
20051205
20051206
20051207

From 20051208 thru 20060108 there are no folders created.

Number of folder_days to keep is 5.

Based on the critera of folders_days older than 7 days
based on the creation date, all the above folders would be
deleted but there is a need to keep the last 5 folder_days
as above.

How to prevent the last 5 days from deletion?

Any clues would be helpful.

Thanks in advance.

Simple batch job:

for /f "skip=5delims=" %i in ('dir /a:d/o:-n/b "d:\parentfolderpathname" ')
do ECHO %i

* change "5" to number of generations you require to hold
* change each "%" to "%%" if this line is used WITHIN a batch file
* Change 'ECHO' to 'rd /s /q' (Remove-Directory, and Subdirectories,
Quietly) once you've verified that the process works.

Batch techniques : alt.msdos.batch.nt

HTH

....Bill
 
G

Guest

Taylor and Billious,

Many Thanks for the suggestions.

Billious,

I tried the following:

FOR /F "SKIP=5DELIMS=" %i IN ('DIR /A:D/O:-N/B "D:\Testing"')
do RD /S /Q %i

and it does not work (have also tried in small caps).

(the above script is in notepad and save as .bat, then in
the windows explorer double click)

I am new to scripts and was wondering what's wrong.

Any clues?

Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top