|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Deleting files older than 14 days using Batch file script
Hi,
This sounds easy, but I'm a newbie so it's a lot harder for me. I am attempting to write a Batch script to delete any files that are older than 14 days in my D:\xxdirectory\ and D:\yydirectory\ and subdirectories. Here is what I have so far... HUNT D:\xxdirectory\*.* s q b#-21 "del" e u I'm not sure if this is even correct. Please help! |
|
#2
|
|||
|
|||
|
What is the hunt command???
I don't think you can actually delete files with standard DOS commands through a batch, but what we can do is XCOPY the files with a certain date to a folder, then delete the folder... xcopy has a /d switch that you can use to specify the date of the files you want to move, for example.... xcopy *.* /d:04-12-2006 e:\temp would move all files in the current directory with the date of april 12 2006 on them to e:\temp.... So move your files over to a certain location then run rmdir e:\temp That will remove the temp folder, and all the files within it. Hope this helps. Last edited by Dameon51 : June 29th, 2006 at 11:48 AM. |
|
#3
|
|||
|
|||
|
What about DELTREE? I found the below script online and was wondering if this will work?
Code:
@ECHO OFF
IF "%1" == "" GOTO NO-DIRECTORY Prompts if No Directory was Specified
ECHO. Displays a Blank Line.
ECHO. Displays a Blank Line.
TREE %1 Displays the Directory Structure
to be Deleted.
DELTREE %1 Deletes Directory Structure.
DR
GOTO END Directs DOS to End the Batch File
Operation.
:NO-DIRECTORY
ECHO.
ECHO No Directory Specified
ECHO.
:END
|
|
#4
|
||||
|
||||
|
with a name like that you're lucky, instant brand recognition ... sure, you're so cute, any self confessed geek would fall over backwards to ... oooh, my head ...
__________________
--Ax without exception, there is no rule ... heavyhaulage.ie Targeted Advertising Cookie Optout (TACO) extension for Firefox The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones ![]() 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ... |
|
#5
|
|||
|
|||
|
Quote:
????? What?? lol No, that code had nothing to specify the date did it??? Also, is deltree still alive and kicking in winxp? I thought it died with win9x. I could be horribly wrong though. |
|
#6
|
||||
|
||||
|
Code:
C:\Documents and Settings\Ax>deltree /? 'deltree' is not recognized as an internal or external command, operable program or batch file. XP SP2 |
|
#7
|
||||
|
||||
|
'deltree' only went as far as Windows ME - if you need to use a command along the same lines, use 'del' or 'rmdir'.
__________________
The No Ma'am commandments: 1.) It is O.K. to call hooters 'knockers' and sometimes snack trays 2.) It is wrong to be French 3.) It is O.K. to put all bad people in a giant meat grinder 4.) Lawyers, see rule 3 5.) It is O.K. to drive a gas guzzler if it helps you get babes 6.) Everyone should car pool but me 7.) Bring back the word 'stewardesses' 8.) Synchronized swimming is not a sport 9.) Mud wrestling is a sport |
|
#8
|
|||
|
|||
|
Thanks, I'll just keep on reading and searching. I noticed a lot of the stuff found googleing are old posts. I have no clue to batch programming, sorry. My forte is in web design so I thought people might be able to help me.
I certainly don't expect people to write code for me just because I'm female and I believe this site is devshed not match.com. |
|
#9
|
|||
|
|||
|
Quote:
This helps a lot thanks. However, I don't think you can execute rmdir with files inside the directory with batch. I tried using del and that seems to work with a prompt. Need to figure how to bypass the prompt. |
|
#10
|
|||
|
|||
|
Quote:
Try with the /q and /s switches and i think it should work. So it would look like...... rmdir /s /q e:\temp |
|
#11
|
|||
|
|||
|
Quote:
Another question, xcopy seems to copy the file, how do I move the files? I know in Unix command you can do a mv command, is it the same for DOS? Is it safe to use mv command in batch? |
|
#12
|
||||
|
||||
|
it's a rename or ren command, but doesn't work across drives, can you install perl on the machine in question?
|
|
#13
|
|||
|
|||
|
Problem. I'm a dumb @$$. Xcopy will only copy the files, not move them, so you'll still have them. I found this little batch on the web for you, but I haven't had time to disect it to see if it works yet...
Also, it seems you are unfamiliar with scripts, so this might be a little complicated for you, but you'll learn something at least! Plus DOS scripts don't really get more complicated than this, so if you figure this out, dos batch files will be a breeze of you. Code:
:: --------DELOLD.BAT----------
@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%
set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 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
if %mm%==12 goto SET31
goto ERROR
:SET31
set /A dd=31 + %dd%
goto DONE
:SET30
set /A dd=30 + %dd%
goto DONE
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (*.*) do (
set FileName=%%i
call :PROCESSFILE %%~ti
)
set mm=
set yyyy=
set dd=
set thedate=
goto EXIT
:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD X
ECHO Where X is the number of days previous to Today.
ECHO.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.
GOTO EXIT
:PROCESSFILE
set temp=%1
set fyyyy=20%temp:~6%
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
:: +*************************************+
:: | This is where the files are deleted |
:: | Change the ECHO command to DEL to |
:: | delete. ECHO is used for test. |
:: +*************************************+
if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
ECHO %FileName%
)
set temp=
set fyyyy=
set fmm=
set fdd=
:EXIT
:: ----------END-DELOLD.BAT-------------
View Solution
|
|
#14
|
|||
|
|||
|
Quote:
Yes, it is installed and I know it's probably easier to use Perl than batch. Since I'm not familiar with Batch and DOS at all I tought it would be worth learning. Thanks. |
|
#15
|
|||
|
|||
|
Thanks! I'll google for this exact batch and see if I can figure it out. Thanks for helping!
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > Windows Help > Deleting files older than 14 days using Batch file script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|