![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Microsoft Windows xp error all errors and bugs related to Microsoft winxp error |
![]() |
|
VBS question for deleting files in folder
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
Option Explicit '---------------------------------------------------------------------------- '----- Current Date Variables '---------------------------------------------------------------------------- '- date/time variables Dim DateTime ' will hold date and time (to current second) Dim dteMonth ' string to hold MONTH Dim dteDay ' string to hold DAY Dim dteYear ' string to hold YEAR Dim dteHour ' string to hold Hour Dim dteMin ' string to hold Minute Dim dteCutoff ' will hold date and time of cuttoff Dim strkeep1 ' First file to keep Dim strKeep2 ' Second file to keep '---------------------------------------------------------------------------- '----- FSO and FILE Variables '---------------------------------------------------------------------------- '- FileScripting Object Dim fso '- text file object Dim ts '---------------------------------------------------------------------------- '----- Initialize Date Variables '---------------------------------------------------------------------------- '- get the date and time and initialize the date/time variables DateTime = Now() dteYear = Year(DateTime) dteMonth = Month(DateTime) dteDay = Day(DateTime) dteHour = Hour(DateTime) dteMin = Minute(DateTime) CutOff = DateAdd("n",-30,DateTime) dteYear2 = Year(CutOff) dteMonth2= Month(CutOff) dteDay2 = Day(CutOff) dteHour2 = Hour(CutOff) dteMin2 = Minute(CutOff) If (Len(dteMonth) = 1) Then dteMonth = "0" & dteMonth End If If (Len(dteDay) = 1) Then dteDay = "0" & dteDay End If If (Len(dteMonth2) = 1) Then dteMonth2 = "0" & dteMonth2 End If If (Len(dteDay2) = 1) Then dteDay2 = "0" & dteDay2 End If strkeep1 = dteYear & dteMonth & dteDay & dteHour & dteMin & "_PeriodicDump.xls" strkeep2 = dteYear2 & dteMonth2 & dteDay2 & dteHour2 & dteMin2 & "_PeriodicDump.xls" If EXIST "C:\Test" & strkeep1 & "_PeriodicDump.xls" Then Delete; It is not complete. Basically I have a program that writes files with this format 200701170815_PeriodicDump.xls into a folder. Every 15 minutes another file gets written in the same format but with the time (0815 )would have changed to (0830). This is all done with Java which I have no idea about nor do I have the source code. I would just like to look into the folder where these files are stored, and keep the most recent ones (at least two) but I would like the flexability to change that number as needed (not important at this point). This vbs code would have to run right after the files where created to make it as automated as possible. Right not the java class files are being called by a .bat file. |
|
|
|
|
|
|
|
|
#2 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
Certainly, using my script...between the End With and With objOutputFile section, replace with this: This will delete files that were created more than one hour ago...be 'very' careful using it. 'intDel is the number of hours old you want to check for. intDel = 1 dtOld = DateAdd("h", -intDel, Now) Set objFolders = objFSO.GetFolder(strSourceFolder) 'Get the files in the current folder For Each fil In objFolders.Files 'If a file exists that hasn't been accessed in intDel days or more, delete it If DateDiff("h", fil.DateCreated, dtOld) >= intDel Then objOutputFile.WriteLine fil.Path & " was removed at " & Now objFSO.DeleteFile fil.Path |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|