Group:  English: Windows Server ยป microsoft.public.windows.server.scripting
Thread: Purge Old Files

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Purge Old Files
Praful Varsani 09.07.2007 14:30:01
Hi

I have a script here that delete old files from a Windows 2000 server and
keep a log of what been deleted by a log file created but
it does not work on XP or windows 2003 server.

On the windows 2000 server - i run the file like this cscript scripname.vbs

Please can you help me get this working on a xp and windows 2003.

Error message it get is this

D:\test>cscript test.vbs
Input Error: Can not find script file "D:\test\test.vbs".
Thanks

here a copy of the script file in .vbs

'
' Script to delete files older than a given number of days.
'
' '
'
Set StdOut = WScript.StdOut

Main

Sub Main()
'
' DropOldFiles
' folderspec - Folder where you want to delete files
' logfile - File where logfile will be generated.
' days - Number of days to keep files.
'
Call DropOldFiles("D:\test","D:\test\log1.txt", 6)
End Sub

Sub DropOldFiles(folderspec, logfile, days)
Dim fso,f,f1,s,fi, cnt, tot

StdOut.WriteLine "Starting DropOldFiles..."

LogOpen(logfile)
cnt = 0
tot = 0

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
If DateDiff ("d",f1.DateLastModified, Now ) > days Then
LogWrite f1.name & " DateLastModified:" & f1.DateLastModified & "
DaysOld:" & DateDiff ("d",f1.DateLastModified, Now )
fso.DeleteFile (folderspec & f1.name)
cnt = cnt + 1
End If
tot = tot + 1
Next
LogWrite "--------------------------------------------------------------"
LogWrite " Processed " & tot & " file(s). Deleted " & cnt & " file(s)."
LogWrite "--------------------------------------------------------------"
LogClose
StdOut.WriteLine "Ending DropOldFiles..."

End Sub

'
'
*****************************************************************************************
'
'
' Write to the Logfile
'

Dim LogFile

Sub LogWrite (Text)
LogFile.WriteLine "[" & Now & "] " & Text
End Sub

Sub LogClose ()
LogFile.Close
Set Logfile=nothing
End Sub

Sub LogOpen (LogPath)
Dim fso, f1
Const ForAppend = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile = fso.OpenTextFile(LogPath, ForAppend, True)
Set fso = nothing
End Sub
Re: Purge Old Files
"D.R." <a[ at ]b.c.d> 09.07.2007 20:13:14
Maybe you have file types hidden, and really the file's true name is
test.vbs.txt
Try disabling "Hide know file extensions",

Start / Control Panel / Folder Options /
....and untick "Hide extensions for known file types"

....then go back to the folder and rename the file to test.vbs and then try
running it again.

Regards,
Dave.


"Praful Varsani" <PrafulVarsani[ at ]discussions.microsoft.com> wrote in message
news:438C8E48-2233-4610-9154-6297A0E04E37[ at ]microsoft.com...
[Quoted Text]
> Hi
>
> I have a script here that delete old files from a Windows 2000 server and
> keep a log of what been deleted by a log file created but
> it does not work on XP or windows 2003 server.
>
> On the windows 2000 server - i run the file like this cscript
> scripname.vbs
>
> Please can you help me get this working on a xp and windows 2003.
>
> Error message it get is this
>
> D:\test>cscript test.vbs
> Input Error: Can not find script file "D:\test\test.vbs".
> Thanks
>
> here a copy of the script file in .vbs
>
> '
> ' Script to delete files older than a given number of days.
> '
> ' '
> '
> Set StdOut = WScript.StdOut
>
> Main
>
> Sub Main()
> '
> ' DropOldFiles
> ' folderspec - Folder where you want to delete files
> ' logfile - File where logfile will be generated.
> ' days - Number of days to keep files.
> '
> Call DropOldFiles("D:\test","D:\test\log1.txt", 6)
> End Sub
>
> Sub DropOldFiles(folderspec, logfile, days)
> Dim fso,f,f1,s,fi, cnt, tot
>
> StdOut.WriteLine "Starting DropOldFiles..."
>
> LogOpen(logfile)
> cnt = 0
> tot = 0
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set f = fso.GetFolder(folderspec)
> Set fc = f.Files
> For Each f1 in fc
> If DateDiff ("d",f1.DateLastModified, Now ) > days Then
> LogWrite f1.name & " DateLastModified:" & f1.DateLastModified & "
> DaysOld:" & DateDiff ("d",f1.DateLastModified, Now )
> fso.DeleteFile (folderspec & f1.name)
> cnt = cnt + 1
> End If
> tot = tot + 1
> Next
> LogWrite "--------------------------------------------------------------"
> LogWrite " Processed " & tot & " file(s). Deleted " & cnt & " file(s)."
> LogWrite "--------------------------------------------------------------"
> LogClose
> StdOut.WriteLine "Ending DropOldFiles..."
>
> End Sub
>
> '
> '
> *****************************************************************************************
> '
> '
> ' Write to the Logfile
> '
>
> Dim LogFile
>
> Sub LogWrite (Text)
> LogFile.WriteLine "[" & Now & "] " & Text
> End Sub
>
> Sub LogClose ()
> LogFile.Close
> Set Logfile=nothing
> End Sub
>
> Sub LogOpen (LogPath)
> Dim fso, f1
> Const ForAppend = 8
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set LogFile = fso.OpenTextFile(LogPath, ForAppend, True)
> Set fso = nothing
> End Sub


Re: Purge Old Files
Praful Varsani 10.07.2007 10:44:05

"P.V." Wrote:
Thanks David, i did that and renamed is to .vbs and re-run the script but
another error message now
Starting DropOldFiles...
D:\test\Purging Script\delete\delete.vbs(33, 3) Microsoft VBScript runtime
error: File not found (Windows 2003 server and XP)


"D.R." wrote:

[Quoted Text]
> Maybe you have file types hidden, and really the file's true name is
> test.vbs.txt
> Try disabling "Hide know file extensions",
>
> Start / Control Panel / Folder Options /
> ....and untick "Hide extensions for known file types"
>
> ....then go back to the folder and rename the file to test.vbs and then try
> running it again.
>
> Regards,
> Dave.
>
>
> "Praful Varsani" <PrafulVarsani[ at ]discussions.microsoft.com> wrote in message
> news:438C8E48-2233-4610-9154-6297A0E04E37[ at ]microsoft.com...
> > Hi
> >
> > I have a script here that delete old files from a Windows 2000 server and
> > keep a log of what been deleted by a log file created but
> > it does not work on XP or windows 2003 server.
> >
> > On the windows 2000 server - i run the file like this cscript
> > scripname.vbs
> >
> > Please can you help me get this working on a xp and windows 2003.
> >
> > Error message it get is this
> >
> > D:\test>cscript test.vbs
> > Input Error: Can not find script file "D:\test\test.vbs".
> > Thanks
> >
> > here a copy of the script file in .vbs
> >
> > '
> > ' Script to delete files older than a given number of days.
> > '
> > ' '
> > '
> > Set StdOut = WScript.StdOut
> >
> > Main
> >
> > Sub Main()
> > '
> > ' DropOldFiles
> > ' folderspec - Folder where you want to delete files
> > ' logfile - File where logfile will be generated.
> > ' days - Number of days to keep files.
> > '
> > Call DropOldFiles("D:\test","D:\test\log1.txt", 6)
> > End Sub
> >
> > Sub DropOldFiles(folderspec, logfile, days)
> > Dim fso,f,f1,s,fi, cnt, tot
> >
> > StdOut.WriteLine "Starting DropOldFiles..."
> >
> > LogOpen(logfile)
> > cnt = 0
> > tot = 0
> >
> > Set fso = CreateObject("Scripting.FileSystemObject")
> > Set f = fso.GetFolder(folderspec)
> > Set fc = f.Files
> > For Each f1 in fc
> > If DateDiff ("d",f1.DateLastModified, Now ) > days Then
> > LogWrite f1.name & " DateLastModified:" & f1.DateLastModified & "
> > DaysOld:" & DateDiff ("d",f1.DateLastModified, Now )
> > fso.DeleteFile (folderspec & f1.name)
> > cnt = cnt + 1
> > End If
> > tot = tot + 1
> > Next
> > LogWrite "--------------------------------------------------------------"
> > LogWrite " Processed " & tot & " file(s). Deleted " & cnt & " file(s)."
> > LogWrite "--------------------------------------------------------------"
> > LogClose
> > StdOut.WriteLine "Ending DropOldFiles..."
> >
> > End Sub
> >
> > '
> > '
> > *****************************************************************************************
> > '
> > '
> > ' Write to the Logfile
> > '
> >
> > Dim LogFile
> >
> > Sub LogWrite (Text)
> > LogFile.WriteLine "[" & Now & "] " & Text
> > End Sub
> >
> > Sub LogClose ()
> > LogFile.Close
> > Set Logfile=nothing
> > End Sub
> >
> > Sub LogOpen (LogPath)
> > Dim fso, f1
> > Const ForAppend = 8
> > Set fso = CreateObject("Scripting.FileSystemObject")
> > Set LogFile = fso.OpenTextFile(LogPath, ForAppend, True)
> > Set fso = nothing
> > End Sub
>
>
>

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net