Purge Files in a Directory

Posted on 2007-11-29
Last Modified: 2010-04-21
I have a directory called C:\Program Files\RXServer\RXServer6\Cache 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\purgefilescript.vbs(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.


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--
'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

Open in new window

Question by:OSXFreak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 30

Accepted Solution

SteveGTR earned 500 total points
ID: 20377897
Here's the code. I've disabled the processing. You can enable it by removing the echo from the set debug line as the comment explains.
@echo off
REM ** To enable the code remove echo from the following line. Like: set debug=
set debug=echo
set workDir=C:\Program Files\RXServer\RXServer6\Cache
set fileMask=*.tpj
call :GETDATEPARTS "%date%"
set cutOff=%yy%%mm%%dd%
pushd "%workDir%"
for /f "tokens=*" %%a in ('dir /b /a-d "%fileMask%" 2^>NUL') DO call :PROCESS "%%a" %%~ta
goto :EOF
if /i "%yy%%mm%%dd%" GTR "%cutOff%" goto :EOF
%debug%  del "%~1"
goto :EOF
set dt=%~1
set tok=1-3
if "%dt:~0,1%" GTR "9" set tok=2-4
set yyyy=
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
if not "%yyyy%"=="" set yy=%yyyy%
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
goto :EOF
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
if /I %dd% GTR 0 goto DONESUBTRACT
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yy=%yy% - 1
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
set /A dd=31 + %dd%
set /A dd=30 + %dd%
set /A tt=%yy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yy% %% 400
if %tt%==0 goto SET29
set /A dd=28 + %dd%
set /A dd=29 + %dd%
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
goto :EOF

Open in new window


Author Comment

ID: 20378731
I took your scripting, and BTW thanks for all that hard work Sheesh... Anyhow I took the script and deleted the echo from the script, then saved it from textpad to the name of purge.vbs. I then dropped in to c:\program files\rxlaser\rxserver6
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\purge.vbsLine: 6
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.

echo off
REM ** To enable the code remove echo from the following line. Like: set debug=
set debug=
set workDir=C:\Program Files\RXServer\RXServer6\Cache
set fileMask=*.tpj
call :GETDATEPARTS "%date%"

Open in new window

LVL 30

Expert Comment

ID: 20379405
My code is not VBS. Save it as .bat or .cmd.

Author Closing Comment

ID: 31411816
Thanks for the Script, once you told me it was bat I was perfectly fine worked like a charm.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question