Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > scheduling deletion of specific files from folders

Reply
Thread Tools Display Modes

scheduling deletion of specific files from folders

 
 
Rj
Guest
Posts: n/a

 
      10-28-2008
hi , everyone I have a script that deletes specific file extension. I now
wanna run it as a batch file and schedule it. The problem is that at the end
it pops up a message box to report the no files that it deleted. How do I
remove the msgbox from the script so that it runs fine as a scheduled task.

Credit for this script goes to the person who wrote it, not me. I found it
on some website while looking for it.

Option Explicit
'****
'* Delete all files with the extension "cEXT" in
'* folders under "cFOL" older than "cDAZ" days.
'****
'*
'* Declare Constants
'*
Const cVBS = "DeleteOldTmpFiles.vbs"
Const cLOG = "DeleteOldTmpFiles.log"
Const cFOL = "C:\temp\"
Const cDAZ = 90
Const cEXT = ".tmp"
'*
'* Declare Variables
'*
Dim strDAT
Dim intDAZ
Dim arrFIL()
ReDim arrFIL(0)
Dim intFIL
intFIL = 0
Dim strFIL
Dim strFOL
Dim strLOG
Dim strMSG
strMSG = " " & cEXT & " files deleted under " & cFOL
Dim dtmNOW
dtmNOW = Now
'*
'* Declare Objects
'*
Dim objFSO
'*
'* Delete_Files()
'*
MsgBox Delete_Files(cFOL) & strMSG,vbInformation,cVBS


Function Delete_Files(folder)
Delete_Files = 0
'*
'* Process folder
'*
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(folder) Then
Call Get_Files(folder)
'*
'* Delete Files
'*
For intFIL = 1 To UBound(arrFIL)
strFIL = arrFIL(intFIL)
objFSO.DeleteFile strFIL, True
strLOG = strLOG & strFIL & vbCrLf
Next
End If
objFSO.CreateTextFile(cLOG,True).Write(strLOG)
Set objFSO = Nothing
'*
'* Return Results
'*
Delete_Files = UBound(arrFIL)
End Function


Sub Get_Files(folder)
'*
'* Get Files
'*
For Each strFIL In objFSO.GetFolder(folder).Files
If LCase(Right(strFIL.Name,Len(cEXT)))= cEXT Then
strDAT = strFIL.DateLastModified
intDAZ = DateDiff("d",strDAT,dtmNOW)
If intDAZ > cDAZ Then
intFIL = intFIL + 1
ReDim Preserve arrFIL(intFIL)
arrFIL(intFIL) = folder & "\" & strFIL.Name
End If
End If
Next
'*
'* Get Subfolders
'*
For Each strFOL In objFSO.GetFolder(folder).SubFolders
Call Get_Files(strFOL.Path)
Next
End Sub

 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      10-28-2008

"Rj" <> wrote in message
news:1FFF9C6B-FC50-499F-8CA0-...
> hi , everyone I have a script that deletes specific file extension. I now
> wanna run it as a batch file and schedule it. The problem is that at the
> end
> it pops up a message box to report the no files that it deleted. How do I
> remove the msgbox from the script so that it runs fine as a scheduled
> task.
>
> Credit for this script goes to the person who wrote it, not me. I found it
> on some website while looking for it.
>
> Option Explicit
> '****
> '* Delete all files with the extension "cEXT" in
> '* folders under "cFOL" older than "cDAZ" days.
> '****
> '*
> '* Declare Constants
> '*
> Const cVBS = "DeleteOldTmpFiles.vbs"
> Const cLOG = "DeleteOldTmpFiles.log"
> Const cFOL = "C:\temp\"
> Const cDAZ = 90
> Const cEXT = ".tmp"
> '*
> '* Declare Variables
> '*
> Dim strDAT
> Dim intDAZ
> Dim arrFIL()
> ReDim arrFIL(0)
> Dim intFIL
> intFIL = 0
> Dim strFIL
> Dim strFOL
> Dim strLOG
> Dim strMSG
> strMSG = " " & cEXT & " files deleted under " & cFOL
> Dim dtmNOW
> dtmNOW = Now
> '*
> '* Declare Objects
> '*
> Dim objFSO
> '*
> '* Delete_Files()
> '*
> MsgBox Delete_Files(cFOL) & strMSG,vbInformation,cVBS
>
>
> Function Delete_Files(folder)
> Delete_Files = 0
> '*
> '* Process folder
> '*
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> If objFSO.FolderExists(folder) Then
> Call Get_Files(folder)
> '*
> '* Delete Files
> '*
> For intFIL = 1 To UBound(arrFIL)
> strFIL = arrFIL(intFIL)
> objFSO.DeleteFile strFIL, True
> strLOG = strLOG & strFIL & vbCrLf
> Next
> End If
> objFSO.CreateTextFile(cLOG,True).Write(strLOG)
> Set objFSO = Nothing
> '*
> '* Return Results
> '*
> Delete_Files = UBound(arrFIL)
> End Function
>
>
> Sub Get_Files(folder)
> '*
> '* Get Files
> '*
> For Each strFIL In objFSO.GetFolder(folder).Files
> If LCase(Right(strFIL.Name,Len(cEXT)))= cEXT Then
> strDAT = strFIL.DateLastModified
> intDAZ = DateDiff("d",strDAT,dtmNOW)
> If intDAZ > cDAZ Then
> intFIL = intFIL + 1
> ReDim Preserve arrFIL(intFIL)
> arrFIL(intFIL) = folder & "\" & strFIL.Name
> End If
> End If
> Next
> '*
> '* Get Subfolders
> '*
> For Each strFOL In objFSO.GetFolder(folder).SubFolders
> Call Get_Files(strFOL.Path)
> Next
> End Sub
>


The simplest method would be to remove the msgbox statement. It serves no
purpose other than informing you that the file has been deleted.

Instead of re-inventing the wheel with a long-winded script that a novice
might struggle to understand, you could use a different wheel that has been
in use for a long time. Here is an example:
@echo off
set Limit=90
set Source=C:\Temp
set Dest=c:\Temp%random%
robocopy /Mov /MinAge:%Limit% "%Source%" "%Dest%" *.tmp
rd /s /q "%Dest%"

The batch file will move all .tmp files older than 90 days to a temporary
folder before deleting them permanently. You can download robocopy from
here:
http://www.microsoft.com/downloads/d...displaylang=en


 
Reply With Quote
 
Rj
Guest
Posts: n/a

 
      10-28-2008
that worked great. Thankyou very much. I just modifed it a bit to get the
output to logfile.

@echo off
set logfile=C:\Backup.log
Echo %date% %time% >>%logfile%
set Limit=1
set Source=C:\Temp
set Dest=c:\Temp%random%
robocopy /NP /NJH /Mov /MinAge:%Limit% "%Source%" "%Dest%" *.bak >>%logfile%
rd /s /q "%Dest%" >>%logfile%

echo --------------------------------- >>%logfile%

"Pegasus (MVP)" wrote:

>
> "Rj" <> wrote in message
> news:1FFF9C6B-FC50-499F-8CA0-...
> > hi , everyone I have a script that deletes specific file extension. I now
> > wanna run it as a batch file and schedule it. The problem is that at the
> > end
> > it pops up a message box to report the no files that it deleted. How do I
> > remove the msgbox from the script so that it runs fine as a scheduled
> > task.
> >
> > Credit for this script goes to the person who wrote it, not me. I found it
> > on some website while looking for it.
> >
> > Option Explicit
> > '****
> > '* Delete all files with the extension "cEXT" in
> > '* folders under "cFOL" older than "cDAZ" days.
> > '****
> > '*
> > '* Declare Constants
> > '*
> > Const cVBS = "DeleteOldTmpFiles.vbs"
> > Const cLOG = "DeleteOldTmpFiles.log"
> > Const cFOL = "C:\temp\"
> > Const cDAZ = 90
> > Const cEXT = ".tmp"
> > '*
> > '* Declare Variables
> > '*
> > Dim strDAT
> > Dim intDAZ
> > Dim arrFIL()
> > ReDim arrFIL(0)
> > Dim intFIL
> > intFIL = 0
> > Dim strFIL
> > Dim strFOL
> > Dim strLOG
> > Dim strMSG
> > strMSG = " " & cEXT & " files deleted under " & cFOL
> > Dim dtmNOW
> > dtmNOW = Now
> > '*
> > '* Declare Objects
> > '*
> > Dim objFSO
> > '*
> > '* Delete_Files()
> > '*
> > MsgBox Delete_Files(cFOL) & strMSG,vbInformation,cVBS
> >
> >
> > Function Delete_Files(folder)
> > Delete_Files = 0
> > '*
> > '* Process folder
> > '*
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > If objFSO.FolderExists(folder) Then
> > Call Get_Files(folder)
> > '*
> > '* Delete Files
> > '*
> > For intFIL = 1 To UBound(arrFIL)
> > strFIL = arrFIL(intFIL)
> > objFSO.DeleteFile strFIL, True
> > strLOG = strLOG & strFIL & vbCrLf
> > Next
> > End If
> > objFSO.CreateTextFile(cLOG,True).Write(strLOG)
> > Set objFSO = Nothing
> > '*
> > '* Return Results
> > '*
> > Delete_Files = UBound(arrFIL)
> > End Function
> >
> >
> > Sub Get_Files(folder)
> > '*
> > '* Get Files
> > '*
> > For Each strFIL In objFSO.GetFolder(folder).Files
> > If LCase(Right(strFIL.Name,Len(cEXT)))= cEXT Then
> > strDAT = strFIL.DateLastModified
> > intDAZ = DateDiff("d",strDAT,dtmNOW)
> > If intDAZ > cDAZ Then
> > intFIL = intFIL + 1
> > ReDim Preserve arrFIL(intFIL)
> > arrFIL(intFIL) = folder & "\" & strFIL.Name
> > End If
> > End If
> > Next
> > '*
> > '* Get Subfolders
> > '*
> > For Each strFOL In objFSO.GetFolder(folder).SubFolders
> > Call Get_Files(strFOL.Path)
> > Next
> > End Sub
> >

>
> The simplest method would be to remove the msgbox statement. It serves no
> purpose other than informing you that the file has been deleted.
>
> Instead of re-inventing the wheel with a long-winded script that a novice
> might struggle to understand, you could use a different wheel that has been
> in use for a long time. Here is an example:
> @echo off
> set Limit=90
> set Source=C:\Temp
> set Dest=c:\Temp%random%
> robocopy /Mov /MinAge:%Limit% "%Source%" "%Dest%" *.tmp
> rd /s /q "%Dest%"
>
> The batch file will move all .tmp files older than 90 days to a temporary
> folder before deleting them permanently. You can download robocopy from
> here:
> http://www.microsoft.com/downloads/d...displaylang=en
>
>
>

 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      10-28-2008
Nice job! Thanks for the feedback.

"Rj" <> wrote in message
news:BD0CEDA8-03F5-42E4-BF4C-...
> that worked great. Thankyou very much. I just modifed it a bit to get the
> output to logfile.
>
> @echo off
> set logfile=C:\Backup.log
> Echo %date% %time% >>%logfile%
> set Limit=1
> set Source=C:\Temp
> set Dest=c:\Temp%random%
> robocopy /NP /NJH /Mov /MinAge:%Limit% "%Source%" "%Dest%" *.bak
> >>%logfile%

> rd /s /q "%Dest%" >>%logfile%
>
> echo --------------------------------- >>%logfile%
>
> "Pegasus (MVP)" wrote:
>
>>
>> "Rj" <> wrote in message
>> news:1FFF9C6B-FC50-499F-8CA0-...
>> > hi , everyone I have a script that deletes specific file extension. I
>> > now
>> > wanna run it as a batch file and schedule it. The problem is that at
>> > the
>> > end
>> > it pops up a message box to report the no files that it deleted. How do
>> > I
>> > remove the msgbox from the script so that it runs fine as a scheduled
>> > task.
>> >
>> > Credit for this script goes to the person who wrote it, not me. I found
>> > it
>> > on some website while looking for it.
>> >
>> > Option Explicit
>> > '****
>> > '* Delete all files with the extension "cEXT" in
>> > '* folders under "cFOL" older than "cDAZ" days.
>> > '****
>> > '*
>> > '* Declare Constants
>> > '*
>> > Const cVBS = "DeleteOldTmpFiles.vbs"
>> > Const cLOG = "DeleteOldTmpFiles.log"
>> > Const cFOL = "C:\temp\"
>> > Const cDAZ = 90
>> > Const cEXT = ".tmp"
>> > '*
>> > '* Declare Variables
>> > '*
>> > Dim strDAT
>> > Dim intDAZ
>> > Dim arrFIL()
>> > ReDim arrFIL(0)
>> > Dim intFIL
>> > intFIL = 0
>> > Dim strFIL
>> > Dim strFOL
>> > Dim strLOG
>> > Dim strMSG
>> > strMSG = " " & cEXT & " files deleted under " & cFOL
>> > Dim dtmNOW
>> > dtmNOW = Now
>> > '*
>> > '* Declare Objects
>> > '*
>> > Dim objFSO
>> > '*
>> > '* Delete_Files()
>> > '*
>> > MsgBox Delete_Files(cFOL) & strMSG,vbInformation,cVBS
>> >
>> >
>> > Function Delete_Files(folder)
>> > Delete_Files = 0
>> > '*
>> > '* Process folder
>> > '*
>> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> > If objFSO.FolderExists(folder) Then
>> > Call Get_Files(folder)
>> > '*
>> > '* Delete Files
>> > '*
>> > For intFIL = 1 To UBound(arrFIL)
>> > strFIL = arrFIL(intFIL)
>> > objFSO.DeleteFile strFIL, True
>> > strLOG = strLOG & strFIL & vbCrLf
>> > Next
>> > End If
>> > objFSO.CreateTextFile(cLOG,True).Write(strLOG)
>> > Set objFSO = Nothing
>> > '*
>> > '* Return Results
>> > '*
>> > Delete_Files = UBound(arrFIL)
>> > End Function
>> >
>> >
>> > Sub Get_Files(folder)
>> > '*
>> > '* Get Files
>> > '*
>> > For Each strFIL In objFSO.GetFolder(folder).Files
>> > If LCase(Right(strFIL.Name,Len(cEXT)))= cEXT Then
>> > strDAT = strFIL.DateLastModified
>> > intDAZ = DateDiff("d",strDAT,dtmNOW)
>> > If intDAZ > cDAZ Then
>> > intFIL = intFIL + 1
>> > ReDim Preserve arrFIL(intFIL)
>> > arrFIL(intFIL) = folder & "\" & strFIL.Name
>> > End If
>> > End If
>> > Next
>> > '*
>> > '* Get Subfolders
>> > '*
>> > For Each strFOL In objFSO.GetFolder(folder).SubFolders
>> > Call Get_Files(strFOL.Path)
>> > Next
>> > End Sub
>> >

>>
>> The simplest method would be to remove the msgbox statement. It serves no
>> purpose other than informing you that the file has been deleted.
>>
>> Instead of re-inventing the wheel with a long-winded script that a novice
>> might struggle to understand, you could use a different wheel that has
>> been
>> in use for a long time. Here is an example:
>> @echo off
>> set Limit=90
>> set Source=C:\Temp
>> set Dest=c:\Temp%random%
>> robocopy /Mov /MinAge:%Limit% "%Source%" "%Dest%" *.tmp
>> rd /s /q "%Dest%"
>>
>> The batch file will move all .tmp files older than 90 days to a temporary
>> folder before deleting them permanently. You can download robocopy from
>> here:
>> http://www.microsoft.com/downloads/d...displaylang=en
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hide/unhide files/folders to a specific directory - minifilter teDeRt Windows Vista Drivers 1 11-24-2008 12:27 PM
Re: Script to delete all files and folders except some specific folders Pegasus \(MVP\) Scripting 0 02-27-2008 10:07 PM
Re: Delete all files and folders except some specific folders Pegasus \(MVP\) Windows Server 0 02-27-2008 10:01 PM
password protect folders and specific files. David Windows Media Center 1 11-13-2007 02:31 PM
Roaming Profile won't allow deletion of files or folders I.S. newbie Windows Server 0 09-15-2004 08:40 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59