Home All Groups Group Topic Archive Search About

Deleting Files and folders



Author
20 Nov 2007 2:50 PM
po_boy
I have some files and folders under C:\LogFiles. This folder has many
subfolders and files. I need to find an easy way to delete the files or
folders(subfolder) that are older than X days.  Most scripts only do a folder
and files but not a group and since I am not that good with scripts it is not
working well for my needs. Please assist.

Author
20 Nov 2007 8:45 PM
Jeffery Hicks [MVP]
See if this helps get you in the right direction.

On Error Resume NextstrFolder="f:\data"
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(strFolder)
Set colFiles=objFolder.Files
For Each file In colFiles
    If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
    WScript.Echo "Deleting " & file & " (" &_
     file.DateLastModified & ")"
     objFSO.DeleteFile file,True
     If Err.Number<>0 Then
        WScript.Echo "**Failed to delete " & file & ". Error# " &_
         Err.Number & " " & Err.Description & "**"
     End If
    End If
Next
'end of script

This script finds all files in F:\Data that are > 60 days old based on date
last modified and deletes them. You could take this code and create a
function so you could recurse through subfolders.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.

Show quote
"po_boy" <po***@discussions.microsoft.com> wrote in message
news:DDFA0136-97B9-4D00-B847-0614060EF44F@microsoft.com...
>I have some files and folders under C:\LogFiles. This folder has many
> subfolders and files. I need to find an easy way to delete the files or
> folders(subfolder) that are older than X days.  Most scripts only do a
> folder
> and files but not a group and since I am not that good with scripts it is
> not
> working well for my needs. Please assist.
Author
20 Nov 2007 9:40 PM
po_boy
Thanks Jeffery, but that is the problem. I am not versed enought in functions
to make it delete the sub-folders and or files.

Show quote
"Jeffery Hicks [MVP]" wrote:

> See if this helps get you in the right direction.
>
> On Error Resume NextstrFolder="f:\data"
> Set objFSO=CreateObject("Scripting.FileSystemObject")
> Set objFolder=objFSO.GetFolder(strFolder)
> Set colFiles=objFolder.Files
> For Each file In colFiles
>     If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
>     WScript.Echo "Deleting " & file & " (" &_
>      file.DateLastModified & ")"
>      objFSO.DeleteFile file,True
>      If Err.Number<>0 Then
>         WScript.Echo "**Failed to delete " & file & ". Error# " &_
>          Err.Number & " " & Err.Description & "**"
>      End If
>     End If
> Next
> 'end of script
>
> This script finds all files in F:\Data that are > 60 days old based on date
> last modified and deletes them. You could take this code and create a
> function so you could recurse through subfolders.
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>
> "po_boy" <po***@discussions.microsoft.com> wrote in message
> news:DDFA0136-97B9-4D00-B847-0614060EF44F@microsoft.com...
> >I have some files and folders under C:\LogFiles. This folder has many
> > subfolders and files. I need to find an easy way to delete the files or
> > folders(subfolder) that are older than X days.  Most scripts only do a
> > folder
> > and files but not a group and since I am not that good with scripts it is
> > not
> > working well for my needs. Please assist.
>
Author
20 Nov 2007 10:28 PM
Jeffery Hicks [MVP]
I don't have time to write you a complete script. Basically you need to do
something like this:

On Error Resume Next
strFolder="f:\data"
Call DeleteOldFiles(strFolder)

wscript.quit 'end main script

Sub DeleteOldFiles(strFolder)
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(strFolder)
Set colFiles=objFolder.Files
For Each file In colFiles
     If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
     WScript.Echo "Deleting " & file & " (" &_
      file.DateLastModified & ")"
      objFSO.DeleteFile file,True
      If Err.Number<>0 Then
        WScript.Echo "**Failed to delete " & file & ". Error# " &_
        Err.Number & " " & Err.Description & "**"
      End If
  End If
Next
Set colSubs=-objFolder.SubFolders
For Each subFldr In colSubs
    Call DeleteOldFiles(subFldr)
next
end sub
'end of script

I strongly suggest you pick up one or two books on scripting.  I always
recommend VBScript, WMI and ADSI Unleashed by Don Jones as a good tutorial.
My WSH and VBScript Core: TFM is a complete language reference with
real-world and practical examples of every object, function, method,
property and statement you are likely to need in VBScript.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.
"po_boy" <po***@discussions.microsoft.com> wrote in message
news:0C9E58D5-98D5-4535-B406-B5DD7EAF1998@microsoft.com...
> Thanks Jeffery, but that is the problem. I am not versed enought in
> functions
> to make it delete the sub-folders and or files.
>
> "Jeffery Hicks [MVP]" wrote:
>
>> See if this helps get you in the right direction.
>>
>> On Error Resume Next
   strFolder="f:\data"
Show quote
>> Set objFSO=CreateObject("Scripting.FileSystemObject")
>> Set objFolder=objFSO.GetFolder(strFolder)
>> Set colFiles=objFolder.Files
>> For Each file In colFiles
>>     If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
>>     WScript.Echo "Deleting " & file & " (" &_
>>      file.DateLastModified & ")"
>>      objFSO.DeleteFile file,True
>>      If Err.Number<>0 Then
>>         WScript.Echo "**Failed to delete " & file & ". Error# " &_
>>          Err.Number & " " & Err.Description & "**"
>>      End If
>>     End If
>> Next
>> 'end of script
>>
>> This script finds all files in F:\Data that are > 60 days old based on
>> date
>> last modified and deletes them. You could take this code and create a
>> function so you could recurse through subfolders.
>>
>> --
>> Jeffery Hicks
>> Microsoft PowerShell MVP
>> http://www.scriptinganswers.com
>> http://www.powershellcommunity.org
>>
>> Now Available: WSH and VBScript Core: TFM
>> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>>
>> "po_boy" <po***@discussions.microsoft.com> wrote in message
>> news:DDFA0136-97B9-4D00-B847-0614060EF44F@microsoft.com...
>> >I have some files and folders under C:\LogFiles. This folder has many
>> > subfolders and files. I need to find an easy way to delete the files or
>> > folders(subfolder) that are older than X days.  Most scripts only do a
>> > folder
>> > and files but not a group and since I am not that good with scripts it
>> > is
>> > not
>> > working well for my needs. Please assist.
>>
Author
21 Nov 2007 1:05 PM
po_boy
Thanks. I will keep working on it.

Show quote
"Jeffery Hicks [MVP]" wrote:

> I don't have time to write you a complete script. Basically you need to do
> something like this:
>
> On Error Resume Next
>  strFolder="f:\data"
> Call DeleteOldFiles(strFolder)
>
> wscript.quit 'end main script
>
> Sub DeleteOldFiles(strFolder)
>  Set objFSO=CreateObject("Scripting.FileSystemObject")
>  Set objFolder=objFSO.GetFolder(strFolder)
>  Set colFiles=objFolder.Files
>  For Each file In colFiles
>      If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
>      WScript.Echo "Deleting " & file & " (" &_
>       file.DateLastModified & ")"
>       objFSO.DeleteFile file,True
>       If Err.Number<>0 Then
>         WScript.Echo "**Failed to delete " & file & ". Error# " &_
>         Err.Number & " " & Err.Description & "**"
>       End If
>   End If
> Next
> Set colSubs=-objFolder.SubFolders
> For Each subFldr In colSubs
>     Call DeleteOldFiles(subFldr)
> next
> end sub
>  'end of script
>
> I strongly suggest you pick up one or two books on scripting.  I always
> recommend VBScript, WMI and ADSI Unleashed by Don Jones as a good tutorial.
> My WSH and VBScript Core: TFM is a complete language reference with
> real-world and practical examples of every object, function, method,
> property and statement you are likely to need in VBScript.
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
> "po_boy" <po***@discussions.microsoft.com> wrote in message
> news:0C9E58D5-98D5-4535-B406-B5DD7EAF1998@microsoft.com...
> > Thanks Jeffery, but that is the problem. I am not versed enought in
> > functions
> > to make it delete the sub-folders and or files.
> >
> > "Jeffery Hicks [MVP]" wrote:
> >
> >> See if this helps get you in the right direction.
> >>
> >> On Error Resume Next
>    strFolder="f:\data"
> >> Set objFSO=CreateObject("Scripting.FileSystemObject")
> >> Set objFolder=objFSO.GetFolder(strFolder)
> >> Set colFiles=objFolder.Files
> >> For Each file In colFiles
> >>     If Int(DateDiff("d",file.DateLastModified,Now)) >= 60 Then
> >>     WScript.Echo "Deleting " & file & " (" &_
> >>      file.DateLastModified & ")"
> >>      objFSO.DeleteFile file,True
> >>      If Err.Number<>0 Then
> >>         WScript.Echo "**Failed to delete " & file & ". Error# " &_
> >>          Err.Number & " " & Err.Description & "**"
> >>      End If
> >>     End If
> >> Next
> >> 'end of script
> >>
> >> This script finds all files in F:\Data that are > 60 days old based on
> >> date
> >> last modified and deletes them. You could take this code and create a
> >> function so you could recurse through subfolders.
> >>
> >> --
> >> Jeffery Hicks
> >> Microsoft PowerShell MVP
> >> http://www.scriptinganswers.com
> >> http://www.powershellcommunity.org
> >>
> >> Now Available: WSH and VBScript Core: TFM
> >> Coming Soon: Windows PowerShell: TFM 2nd Ed.
> >>
> >> "po_boy" <po***@discussions.microsoft.com> wrote in message
> >> news:DDFA0136-97B9-4D00-B847-0614060EF44F@microsoft.com...
> >> >I have some files and folders under C:\LogFiles. This folder has many
> >> > subfolders and files. I need to find an easy way to delete the files or
> >> > folders(subfolder) that are older than X days.  Most scripts only do a
> >> > folder
> >> > and files but not a group and since I am not that good with scripts it
> >> > is
> >> > not
> >> > working well for my needs. Please assist.
> >>
>

AddThis Social Bookmark Button