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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now