VBScript to replace DOS Batch that deletes old files 
Author Message
 VBScript to replace DOS Batch that deletes old files

I am attempting to write a VBScript that will look at each file in a
specified directory and if a file is older than 15 days old, and begins
with text (text*.*) delete it.  I currently have a for loop that looks
at each file, determines if it is older than 15 days.  But the problem
is that if "any" file is old, then it runs the delete and deletes all my
text*.* files even if they aren't old.

Dim fso, wrkfile, Expires
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.GetFolder("H:\Development\LogCleanUp\Logist")

'DELETE FILES OLDER THAN 15 DAYS
Expires = Now() - 15

For Each file In f.Files
  IF file.DateLastModified < Expires THEN  'IS FILE OLD?
    MsgBox file.Name
    On Error Resume Next
    Dim objFSO
    Set objFSO = CreateObject ("Scripting.FileSystemObject")
    objFSO.DeleteFile ("H:\Development\LogCleanUp\Logist\text*.*")      
  END IF
Next

Any Help would be appreciated.
Thanks

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!



Wed, 21 Jul 2004 09:41:46 GMT  
 VBScript to replace DOS Batch that deletes old files
Here... you didn't have a question or a problem stated but this should work

Dim fso, wrkfile, Expires
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.GetFolder("H:\Development\LogCleanUp\Logist")

'DELETE FILES OLDER THAN 15 DAYS
Expires = DateAdd("d",-15,Now())
For Each x In f.Files
    IF x.DateLastModified < Expires THEN  'IS FILE OLD?
        MsgBox x.Name
        On Error Resume Next
        objFSO.DeleteFile "H:\Development\LogCleanUp\Logist\" & x.Name
     END IF
 Next

Brian


Quote:
> I am attempting to write a VBScript that will look at each file in a
> specified directory and if a file is older than 15 days old, and begins
> with text (text*.*) delete it.  I currently have a for loop that looks
> at each file, determines if it is older than 15 days.  But the problem
> is that if "any" file is old, then it runs the delete and deletes all my
> text*.* files even if they aren't old.

> Dim fso, wrkfile, Expires
> Set fso = CreateObject ("Scripting.FileSystemObject")
> Set f = fso.GetFolder("H:\Development\LogCleanUp\Logist")

> 'DELETE FILES OLDER THAN 15 DAYS
> Expires = Now() - 15

> For Each file In f.Files
>   IF file.DateLastModified < Expires THEN  'IS FILE OLD?
>     MsgBox file.Name
>     On Error Resume Next
>     Dim objFSO
>     Set objFSO = CreateObject ("Scripting.FileSystemObject")
>     objFSO.DeleteFile ("H:\Development\LogCleanUp\Logist\text*.*")
>   END IF
> Next

> Any Help would be appreciated.
> Thanks

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Wed, 21 Jul 2004 10:07:04 GMT  
 VBScript to replace DOS Batch that deletes old files
This script should do exactly that! Sorry comments in german

wolfgang


Quote:
> I am attempting to write a VBScript that will look at each file in a
> specified directory and if a file is older than 15 days old, and begins
> with text (text*.*) delete it.  I currently have a for loop that looks
> at each file, determines if it is older than 15 days.  But the problem
> is that if "any" file is old, then it runs the delete and deletes all my
> text*.* files even if they aren't old.

> Dim fso, wrkfile, Expires
> Set fso = CreateObject ("Scripting.FileSystemObject")
> Set f = fso.GetFolder("H:\Development\LogCleanUp\Logist")

> 'DELETE FILES OLDER THAN 15 DAYS
> Expires = Now() - 15

> For Each file In f.Files
>   IF file.DateLastModified < Expires THEN  'IS FILE OLD?
>     MsgBox file.Name
>     On Error Resume Next
>     Dim objFSO
>     Set objFSO = CreateObject ("Scripting.FileSystemObject")
>     objFSO.DeleteFile ("H:\Development\LogCleanUp\Logist\text*.*")
>   END IF
> Next

> Any Help would be appreciated.
> Thanks

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Thu, 22 Jul 2004 01:17:16 GMT  
 VBScript to replace DOS Batch that deletes old files
Uups- forgot to insert the script

Option Explicit
' Die Funktion ***DeleteRecursive*** l?scht alle  Dateien in einem
vorgegeben
'              Verzeichnis (inkl. in Subverzeichnissen)
'              die ?lter als x Tage sind

Dim omFso: ' As Scripting.FileSystemObject
Dim omWshShell:' AS WScript.Shell
   Set omFso = CreateObject("Scripting.FileSystemObject")
   Set omWshShell = WScript.CreateObject("WScript.Shell")

 '  Call DeleteRecursive("v:\sqlbackup","BAK",2)
 '  Call DeleteRecursive("v:\sqlbackup","TRN",2)
 '  Call DeleteRecursive("v:\BamAlarm\bernommen","MDB",2)
  Call DeleteRecursive("C:\Temp","BAK",2)
'Sub DeleteRecursive(Folder As Scripting.Folder,Extension as String
,OlderasDays as Long )
Sub DeleteRecursive(Folder,Extension,OlderasDays )

Dim oStartFolder:' As Scripting.Folder
'On Error Resume Next

   Set oStartFolder = omFso.GetFolder(Folder)
   ProcessFolder oStartFolder,Extension,OlderasDays
End Sub

'Sub ProcessFolder(Folder As Scripting.Folder,Extension as String
,OlderasDays as Long ))
Sub ProcessFolder(Folder,Extension,OlderasDays)
Dim oFile: ' As Scripting.File
Dim oFolder: ' As Scripting.Folder
Dim aDate: ' As Date

For Each oFile In Folder.Files
   aDate = DateAdd("d", - OlderasDays, Now)
   If oFile.DateLastModified < aDate and
UCase(omFso.GetExtensionName(oFile.Path)) =  UCase(Extension) then
      omWshShell.PopUp "L?sche Datei" &  oFile.Path & "vom " &
oFile.DateLastModified,1
      oFile.Delete True
   End If
Next

For Each oFolder In Folder.SubFolders
   omWshShell.PopUp "Verarbeite Folder " & oFolder.Path,2
   Call ProcessFolder(oFolder,Extension,OlderasDays)
Next
End Sub



Thu, 22 Jul 2004 01:18:17 GMT  
 VBScript to replace DOS Batch that deletes old files
Thanks Brian for the cleaner code.  The problem that I should have more
clearly stated is that my script must also check the filename.  
If the filename begins with "text" (e.g. textfilebad.tmp), and is older
than 15 days; delete it.  Otherwise if the file is older than 15 days
and the filename does not begin with "text" (e.g. filenamegood.tmp);
leave it.  I am a junior programmer, and have created a few simple
scripts to perform administrative tasks.  How do I add to the script
below so that it also checks the filename and only deletes the files
whose filenames begin with "text"?

'CODE BEGINS HERE (Thanks to Brian Roberds)
Dim fso, wrkfile, Expires
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.GetFolder("H:\Development\LogCleanUp\Logist")

'DELETE FILES OLDER THAN 15 DAYS
Expires = DateAdd("d",-15,Now())
For Each x In f.Files
    IF x.DateLastModified < Expires THEN 'IS FILE OLD?
        MsgBox x.Name
        On Error Resume Next
        objFSO.DeleteFile "H:\LogCleanUp\Logist\" & x.Name
     END IF
Next

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sat, 24 Jul 2004 00:14:29 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. VBScript to Replace Dos Batch....Deleting Old Files

2. Batch Job to Replace Old Setup

3. Help: hiding a DOS window when executing a DOS batch file

4. question on hiding a DOS window when executing a DOS batch file

5. DOS Batch File to Copy Files from an Access Application

6. Bad Command or File name when Shelling DOS Batch File

7. Old dosbased qb and batch files

8. Replace a batch file

9. Replacing Batch files

10. Installation Problem when old system files need replacing

11. Setup does not replace older files

12. Installing crpe32.dll - doesn't replace older file

 

 
Powered by phpBB® Forum Software