Link to home
Start Free TrialLog in
Avatar of Who Dat
Who DatFlag for United States of America

asked on

Batch file to delete files after certain time period

Posted question in wrong area:
https://www.experts-exchange.com/questions/21969910/Batch-file-to-delete-files-after-certain-time-period.html
But some EE comments may be helfpul; just don't know how to execute.


I've created an Upload capability on one of the company servers - running Windows 2003; using a program called ASPUpload.

So for Inetpub and IIS, there is a main folder called Files w/subfolders for external clients and one folder for internal users.

I wanted to know whether there was some way of creating a batch file, script file or some program file, for one particular folder (the internal user folder) to delete its contents after 72 hours? This folder's content will include .doc, .xls, .jpg; every known file extension.

Also, if someone is able to help me w/a bathc file, would you also please let me know where the batch file should be stored and how it can run automatically?

I'd also like to know if there's a way to keep a "seperate" log to show who has been logging into this folder to upload files.  I know the logs are located (at least for us) \\servername\c$\WINDOWS\system32\LogFiles\W3SVC1,  but these logs contain user logins to mailboxes as well as logging into the upload server pages.

Thanks!
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

This batch file will delete all files in a directory that are older than 3 days:

@echo off

setlocal

if "%~1"=="" echo Usage: %0 directory&goto :EOF
if not exist "%~1" echo Directory %~1 does not exist.&goto :EOF

call :GETDATEPARTS "%date%"

call :SUBTRACTDAYS 3

set compDate=%yy%%mm%%dd%

pushd "%~1"

for /f "delims=" %%a in ('dir /b /a-d 2^>NUL') do call :PROCESS "%%a" "%%~ta"

popd

goto :EOF

:PROCESS

call :GETDATEPARTS %2

if /i "%compDate%" GTR "%yy%%mm%%dd%" echo del "%~1"

goto :EOF

:GETDATEPARTS

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

:SUBTRACTDAYS

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

:CHKDAY

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

:ADJUSTDAY

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

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

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

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

You'd place this batch file in a common directory. Not the directory that you want to process. If you named the batch file DeleteOldFiles.bat and you wanted to run it against the directory C:\My Files\Internal Files you'd say:

DeleteOldFiles "C:\My Files\Internal Files"

I'd recommend setting up a scheduled task in Windows to do this processing. To run the batch file you'd say:

cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files"

As for the other question, I'd need more information.

Good Luck,
Steve
Avatar of Who Dat

ASKER

Ok, so the above batch file would be keeped in say the Windows Directory.  As for the coding above it does not specify a directory to delete files, correct?  It's this that sets what to delete:

                            "You'd place this batch file in a common directory. Not the directory that you want to process.
                             If you named the batch file DeleteOldFiles.bat and you wanted to run it against the directory C:\My Files\Internal Files you'd say:
                             DeleteOldFiles "C:\My Files\Internal Files"
                             I'd recommend setting up a scheduled task in Windows to do this processing. To run the batch file you'd say:
                             cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files"

Is there a way to create a log file which will show the users who are logging into the folder?
Not that I know of via batch processing. There are .NET programming options that will monitor folder usages. The application could run as a service on the computer and log access by user to a file.
And yes the batch file above accepts the folder name as a parameter. In fact the batch processing will not run if a valid directory is not passed to it.
Avatar of Who Dat

ASKER

When you say this, "And yes the batch file above accepts the folder name as a parameter. In fact the batch processing will not run if a valid directory is not passed to it." You're referring to entering this command line code: "cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files" b/c the folder name is not in the actual batch file coding. Is that correct?
That is correct. Also, I forgot to mention that the batch processing is currently disabled. I've placed an echo statement prior to del command so that you can verify that it is working like you want. To enable the processing remove the echo statement.

So this line:

if /i "%compDate%" GTR "%yy%%mm%%dd%" echo del "%~1"

Would look like this:

if /i "%compDate%" GTR "%yy%%mm%%dd%" del "%~1"
Avatar of Who Dat

ASKER

one more question. i'm going to run the cmd for the windows scheduler, but here's my question.  The files/folders I want deleted are on the D: drive, so would the setup look like this?

1)  cmd /c d:\pathtothebatchfile\DeleteOldFiles "D:\My Files\Internal Files" -> keeping the cmd /c    OR
2)  cmd /d d:\pathtothebatchfile\DeleteOldFiles "D:\My Files\Internal Files" -> changing the cmd /c to cmd /d
Use /c.

The /c option of cmd carries out the command and then terminates. Check cmd /? at the command prompt for more information on the parameters.
Avatar of Who Dat

ASKER

I just ran a test to see if it would delete the info in the folder and it did not. I used the [cmd /c d:\pathtothebatchfile\DeleteOldFiles "D:\My Files\Internal Files"] from the command prompt.

How much of a difference does it make to put the bat file seperate from the other files?
If you place the code with the other files the batch file will delete itself after 3 days. I'd recommend creating a Utility directory and place this batch file there.
Avatar of Who Dat

ASKER

i created a subfolder w/in the main FILES folder.  So the subfolders are like, user1, user2, user3, images and a batch folder where I put the batch file.  Is that good, or should I place it further away?

When you said this "That is correct. Also, I forgot to mention that the batch processing is currently disabled. I've placed an echo statement prior to del command so that you can verify that it is working like you want. To enable the processing remove the echo statement."

I was under the impression if I ran the statement you posted, it would immediately delete the files in the folder?  Did I misunerstan that?
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Who Dat

ASKER

i'd like to add comments to the top of the batch file, can i do that with just using REM or something like :: ?  Example:
REM      DC.BAT (DELETE CONTENTS)
REM      Created by MDM; August 30, 2006.
REM      This batch file will delete all files in the Ime directory older than 3 days \\webmail1\d$\inetpub\wwwroot\files\Ime.  
REM      Batch file folder content deletion only currently set to Ime files folder.
REM      To run program from cmd prompt -> DC "D:\inetpub\wwwroot\files\Ime"
REM      Setting up a scheduled task in Windows: cmd /c D:\BatchForUploadServer\DC.BAT "D:\inetpub\wwwroot\files\Ime"

followed by the coding..................also is it necessary to type the .bat.  And I've removed the batch file from within inetpub and just placed it into its own folder on the D: drive.

@echo off

setlocal
Your REM statement will work just fine. I'd place them after the @echo off statement.

"also is it necessary to type the .bat."

Do you mean manually type in all the command in notepad? If so then please do not do that. Just cut and paste from the EE.
Avatar of Who Dat

ASKER

ok, thanks for being so patient - gonna raise the points for you.

So, i should place my REM statements after @echo off? Like this?

@echo off

REM     DC.BAT (DELETE CONTENTS)
REM     Created by MDM; August 30, 2006.
REM     This batch file will delete all files in the Ime directory older than 3 days \\webmail1\d$\inetpub\wwwroot\files\Ime.  
REM     Batch file folder content deletion only currently set to Ime files folder.
REM     To run program from cmd prompt -> DC "D:\inetpub\wwwroot\files\Ime"
REM     Setting up a scheduled task in Windows: cmd /c D:\BatchForUploadServer\DC.BAT "D:\inetpub\wwwroot\files\Ime"

setlocal

Is that correct?

And what I was referring to w/the .bat was when I set the scheduler in Windows this is all I have to type:
cmd /c D:\BatchForUploadServer\DC "D:\inetpub\wwwroot\files\Ime"

DC is the DC.bat file, but adding the extension is not necessary, right?
Everything looks good with the comment placement.

You don't need the .bat extension, but I'd go ahead a put it there. Doesn't hurt to be as specific as possible when scheduling a task.
Avatar of Who Dat

ASKER

i typed this to set up a windows task scheduler:
cmd /c D:\BatchForUploadServer\DC.BAT "D:\inetpub\wwwroot\files\Ime" just as you suggested:
cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files"
but it does not appear as a Windows Task.  Should it?  Did I do something wrong?
It will show up in the scheduled tasks. It won't show as a Windows Task until it is actually run.
Avatar of Who Dat

ASKER

So since I set it, it'll show up in 3 days?
No, when you schedule a task you schedule it for a certain day or series of days.
For this processing I'd recommend running it every weekday at an off hour, say 11PM or so.
Avatar of Who Dat

ASKER

i thought the cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files" set the scheduler.

So I need to add a task and include the bat file, but I don't want to run it everyday b/c wouldn't it delete all the files in the folder?
No that just runs the job. The batch file will only delete files older than 3 days.

If you want to just run the job manually.
Avatar of Who Dat

ASKER

You definitely have me confused.
I typed this:  cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files"

and i have now set a task schedule to run the batch file every three days.  Right or wrong?
Based on your original description I'd recommend the following:

Setup a task to run the batch file every week day night at 11PM.

Your original description said that you want to delete files that are 72 hours old. File will continuously become that old as times goes by.
As for your confusion, I'll restate my prior response to your questions on an individual basic:

"i thought the cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files" set the scheduler."

No, that just runs the batch file. It doesn't add a job to the scheduler.

"So I need to add a task and include the bat file..."

Yes, that is what I recommend. See my prior note for the reason why.

"... but I don't want to run it everyday b/c wouldn't it delete all the files in the folder?"

No, the job was created based on your requirements to only delete files that are older than 72 hours (3 days).

I hope that is more clear.
Avatar of Who Dat

ASKER

Believe this has worked; unless someone went in and deleted the files :o)

Thanks again for your patience.....& hope I run across you again!
Excellent :)
Avatar of Who Dat

ASKER

ok....sorry....

i honestly don't know whether this batch is working.

I just uploaded a file to the folder I wanted deleted every three days.  I entered the command w/my proper syntax and folder location: cmd /c c:\pathtothebatchfile\DeleteOldFiles "C:\My Files\Internal Files"  and then went to the folder to see if the file had been deleted but it had not.  I was under the assumption, this cmd would delete the files immediately.  Am I again misunderstanding?  Will this command not immediately delete the files w/in the folder b/c the batch coding states three days before deletion or is something missing someplace?

Thanks, and sorry!!
The files will be deleted if they were updated prior to three days ago. When you do a dir command from the path, the date that is displayed will be used to compare against the cutoff date.
Remember that the batch file can (and should) be run every day and it will only ever delete files that are at least three days old. The act of simply running the batch file will not delete a file unless it meets the three day cutoff requirement. Otherwise the processing could be trimmed down to a simple del *.*.
Avatar of Who Dat

ASKER

Ok, got it!