Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch file or script to delete files or folders older than 30 days.

Posted on 2007-10-03
16
Medium Priority
?
718 Views
Last Modified: 2008-01-09
I currently have a public shared folder that temp users can save files on the network. I need to have a batch file or a script to delete files or folders older than 30 days. Hope someone can write a simple batch or script to help me out. I know how to write very simple batch files for logins etc. But that's pretty much the extent of my knowledge.
The folder is on a Windows 2003 server. PCs are all Windows XP.

Now this might be a bit much but here goes: If your feeling patient enough can you write comments to explain each line or section so I know exactly what is happening at each stage?
0
Comment
Question by:bigd563
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 8

Expert Comment

by:JSoup
ID: 20008753
0
 
LVL 8

Expert Comment

by:JSoup
ID: 20008756
sorry wrong os
0
 
LVL 8

Expert Comment

by:JSoup
ID: 20008773
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 20008959
It's not simple, but give it a try. The batch processing accepts a directory as a parameter. That is the base directory where the directories and files hang off of. This directory will not be removed.

@echo off

setlocal

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 30

set cutoff=%yy%%mm%%dd%

set workDir=.

if not "%~1"=="" set workDir=%~1

if not exist "%workDir%" echo %workDir% does not exist&goto :EOF

call :PROCDIR "%workDir%"

goto :EOF

:PROCDIR

pushd "%~1"

for /f "tokens=*" %%a in ('dir /ad /b 2^>NUL') do call :PROCDIR "%%a" %%~ta

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

set removeDir=

if not "%~2"=="" call :PROCESS dir "%~1" %~2

popd

if "%removeDir%"=="" goto :EOF

rd "%~1" >NUL 2>&1

if not exist "%~1" echo %~1 directory removed

goto :EOF

:PROCESS

echo %~1 %~2 %~3 %~4

call :GETDATEPARTS "%~3"

if /i "%yy%%mm%%dd%" GTR "%cutoff%" goto :EOF

if "%~1"=="dir" set removeDir=Y&goto :EOF

del /F "%~2">NUL

echo %~2 file removed

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
0
 
LVL 23

Expert Comment

by:Jeremy Weisinger
ID: 20008980
Perhaps this is what you're looking for? Go through the thread as they talk alot about what the script is doing.
http://www.petri.co.il/forums/showthread.php?t=16630

If you have any questions let us know.

HTH
0
 
LVL 8

Expert Comment

by:JSoup
ID: 20008997
SteveGTR
You are using the same information I am providing, But calling it your own. Bad boy.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20009035
The information you reference is my information and it does only the directory processing, not the file part that the user requested.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20009055
To further clarify...

Check it out and you'll see I wrote that code and it does only part of what the user wants.

I've got nothing against pointing to a reference that does what the author requests, even if it isn't your original idea, but what you point to should provide a complete answer. My previous answer does provide a complete answer.
0
 

Author Comment

by:bigd563
ID: 20009110
SteveGTR: How would I parse the results (of the files or folder deleted) to a text file? Also how would I enter the path if a space(s) were in the path?
BTW the tool works. Thanks. The above pieces would be perfect.  Thanks again.
0
 
LVL 8

Expert Comment

by:JSoup
ID: 20009142
If you own it, its your call.  You should give me ack for not steeling your code or the original posting credit.
 
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20009144
To pass a directory with spaces on the command line do this:

delOldFiles.bat "c:\Directory with spaces"

I can add the processing to dump the results to a file. I'll post it in the next message.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 total points
ID: 20009177
@echo off

setlocal

call :GETDATEPARTS "%date%"
call :SUBTRACTDAYS 30

set cutoff=%yy%%mm%%dd%

set outFile=%cd%

if not "%outFile:~-1%"=="\" set outFile=%outFile%\

set outFile=%outFile%results.txt

if exist "%outFile%" del "%outFile%" >NUL

set workDir=.

if not "%~1"=="" set workDir=%~1

if not exist "%workDir%" echo %workDir% does not exist&goto :EOF

call :PROCDIR "%workDir%"

if not exist "%outFile%" echo Nothing processed&goto :EOF

echo Results in %outFile%

goto :EOF

:PROCDIR

pushd "%~1"

for /f "tokens=*" %%a in ('dir /ad /b 2^>NUL') do call :PROCDIR "%%a" %%~ta

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

set removeDir=

if not "%~2"=="" call :PROCESS dir "%~1" %~2

popd

if "%removeDir%"=="" goto :EOF

rd "%~1" >NUL 2>&1

if exist "%~1" goto :EOF

echo %~1 directory removed
echo %~1 directory removed>>"%outFile%"

goto :EOF

:PROCESS

echo %~1 %~2 %~3 %~4

call :GETDATEPARTS "%~3"

if /i "%yy%%mm%%dd%" GTR "%cutoff%" goto :EOF

if "%~1"=="dir" set removeDir=Y&goto :EOF

del /F "%~2">NUL

echo %~2 file removed
echo %~2 file removed>>"%outFile%"

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
back to top
0
 

Author Comment

by:bigd563
ID: 20009205
GREAT thanks a lot.  Here's what I did I ran tool on a folder with spaces then parse it within the same command line and it seems to have worked.
i.e. DelOldFile.bat "C:\Documents and Settings\temp\Desktop\Current Work" >results.txt

Send your solution anyways it would be great to have also.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20009209
That is an even simpler solution than mine. Good job :)
0
 

Author Comment

by:bigd563
ID: 20009269
I guess the set outFile=%cd% drops the results file into the workdir?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20009533
It places it in whatever the current directory is when the batch is run.
0

Featured Post

Independent Software Vendors: 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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 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