OSXFreak
asked on
Purge Files in a Directory
I have a directory called C:\Program Files\RXServer\RXServer6\C ache and in that cache dir we get upwards of 100000 files. I want a script that will purge *.tpj files older the 30 days. I downloaded a script and installed it but it gives an error of
C:\WINDOWS\Scripts\purgefi lescript.v bs(46, 29) Microsoft VBScript compilation er
ror: Cannot use parentheses when calling a Sub
Any info on the error or a script that will work would be apperciated.
Chip
C:\WINDOWS\Scripts\purgefi
ror: Cannot use parentheses when calling a Sub
Any info on the error or a script that will work would be apperciated.
Chip
PurgeFile.Bat
echo off
set pgmpath="c:\Windows\Scripts"
REM This batch file is used to purge files in a specified directory
REM that are over a given age. The arguments to this batch file are:
REM %1 = The path to the directory containing the files to be purged.
REM %2 = The pattern of the files to be selected within the directory.
REM The pattern may contain wild cards as needed.
REM %3 = The name of the file that this batch file will create to contain
REM the list of candidate files.
REM %4 = The age above which files should be purged. This argument is not
REM used directly in the batch file, but is passed to the VB script
REM called to perform the tests and actual purge.
set fpath=%1\%2
dir %fpath% > %1\%3 /B /A:-D-S
cscript %pgmpath%\purgefilescript.vbs %1 %3 %4
------------------------------- below is another script--
purgefilescript.vbs
'This script is used to purge files over a specified age in days.
'It uses a file containing a list of candidate files as input.
'The arguments to this script are:
' (0) the path to the directory to be checked and purged.
' No files are specified here, just the directory
' (1) the name of the file containing the candidate list of files.
' Must be in the above directory
' (2) the age in days above which files should be purged.
'
'To execute this script, use a command line entry like:
' cscript PurgeFiles.vbs "c:\My Directory\My Subdirectory"
'Dirfile.txt 7
'
'This will purge all files older than seven days with an entry in
'Dirfile.txt from c:\My Directory\My Subdirectory
Dim fso, f, path, candidates, cpath, fpath, retstring
Const ForReading = 1
Set objArgs = Wscript.Arguments 'Prepare to parse the arguments
path = objArgs(0) 'This is the path without the file names
candidates= objArgs(1) 'This is the name of the candidate list file.
'Any file older than age days old will be purged.
'The argument must be converted to integer.
age = CInt(objArgs(2))
cpath = path & "\" & candidates
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(cpath, ForReading)
Wscript.Echo "Processing files in " & cpath
Do While f.AtEndOfStream <> True
retstring = f.ReadLine
fpath = path & "\" & retstring
Set f2 = fso.GetFile(fpath)
'Extract the file's creation date-time
dcr = f2.DateCreated
'Look for the first space in the creation date string
i = InStr(1,dcr," ")
'Extract just the date from the date-time string
dcr = left(dcr, i-1)
'Calculate the number of days between today and the create date
d = datediff("d", dcr, Date)
'Compare age and file name
If (d > age) and (retstring <> candidates) then
Wscript.Echo "Deleting " & retstring & " which is " & d & " days old."
'Remove the comment quote from the following line to really delete.
fso.DeleteFile(fpath, true) 'True means force the delete if read only.
End If
Loop
f.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My code is not VBS. Save it as .bat or .cmd.
ASKER
Thanks for the Script, once you told me it was bat I was perfectly fine worked like a charm.
ASKER
then I went int to CMD and change dir to c:\program files\rxlaser\rxserver6 then once there I typed in purge.vbs and got an error.
The first line had an error so I removed the @ and then it processed on and stopped at the set debug =
below is that portion.. the error pop up says,
script: c:\Program files\RxLaser\RxServer6\pu
Char : 11
Error: Syntax Error
Code: 800A03EA
Source: Microsoft VBScript compilation error
So not sure what next as I'm so GREEN on this kind of things.
Thanks
Chip
Open in new window