1.

Solve : Deltree a folder by creation date in a .bat file?

Answer»

So I had to create a batch file that RAN a build script and created a unique directory then copied files to that directory during each run. What I need to do now is add to the script so that it looks for all the directories in that are more that 4 days old and delete them. How would I go about doing this?

The batch is run on a local admin account on a win 2003 server.

date /t command gives: Fri 05/07/2010
format (mm/dd/yyyy)

The directory that it will search in is: Z:\IT\AD&M\CLASSMate\ESD\

So to sum it up, I need a command that I can add to my current batch file that will check the creation date of all the directories within the "ESD" folder and delete any folder (and it's contents) that are more than 4 days old.

Any help would be greatly appreciated, Thanks!

Oh, and not sure if it matters, but all the folder names in that ESD directory will also be in the format of "yyyy-mm-dd hh.mm". Every time the build script is run, it creates a directory with the date and time it was run as the folder name and places the files in there. So it's the old builds that I need to get rid of after a little time.http://www.raymond.cc/blog/archives/2007/09/24/deltree-command-replacement-in-windows-2000-or-windows-xp/


Deltree Command Replacement in Windows 2000?

"Just a short tip for today. I was required to create a batch to automatically
remove a directory as well as all of its subdirectories and contained files.
I remembered many years ago when I was using Windows 98, I could use the “deltree”
command to delete a folder and everything in it.

I launched command prompt(DOS), and typed “deltree /?”
to display all the commands for deltree because I couldn’t remember
the deltree parameters. I was QUITE embarrassed to see the error
message “deltree is not recognized as an internal or external command,
operable program or batch file.” It seems that the deltree command is no
longer used."

Reference:

http://stackoverflow.com/questions/324267/batch-file-to-delete-files-older-than-a-specified-date/1180746#1180746


I have not tested the following code:


Code: [Select]@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX

echo >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /L /I /EXCLUDE:~~~FILES_TO_KEEP.TXT~ null') do if exist "%%~nxa" del "%%~nxa"
del ~~~FILES_TO_KEEP.TXT~

GOTO END

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD mm-dd-yyyy
ECHO Where mm-dd-yyyy is the date prior to which you want to delete files.
ECHO.
ECHO EX: "DELOLD 10-17-2008" Deletes files older than October 17, 2008.
ECHO.
ECHO This should work on any language version of Windows, but has only been
ECHO tested in English-US versions.
GOTO END

:END Quote from: marvinengland on May 07, 2010, 11:33:16 AM

http://www.raymond.cc/blog/archives/2007/09/24/deltree-command-replacement-in-windows-2000-or-windows-xp/


Deltree Command Replacement in Windows 2000?

"Just a short tip for today. I was required to create a batch to automatically
remove a directory as well as all of its subdirectories and contained files.
I remembered many years ago when I was using Windows 98, I could use the “deltree”
command to delete a folder and everything in it.

I launched command prompt(DOS), and typed “deltree /?”
to display all the commands for deltree because I couldn’t remember
the deltree parameters. I was quite embarrassed to see the error
message “deltree is not recognized as an internal or external command,
operable program or batch file.” It seems that the deltree command is no
longer used."



You can see how long it's been since i last played with DOS commands then hmm, that code wouldn't work for me. The batch will be an automated batch file so it needs to calculate the date on it's own to determine if a folder needs to be deleted or not. So regardless of what date the batch runs, it will always delete any folder that is more than 4 days old. The script above asks the user for a date value to determine what files it will delete.Hmm.... Can the forfiles command be used? I was thinking something like this, but I'm not sure if i have the rd part right or not:


forfiles /m *.* /d -4 /c "cmd /c echo @file is at least 4 days old."
forfiles /m *.* /d -4 /c "cmd /c rd @file"

is the forfiles command an external command that I can bring over to my xp box so i can test? Or can it only be run on a win server? I created this little bit of code;

evaluate.vbs contains the following code...
Code: [Select]Wscript.echo eval(WScript.Arguments(0))
main batch file contains...
Code: [Select]@echo off
del output.txt
del output2.txt
del output3.txt
del output4.txt
rem ** set to the root folder, code will check folders inside this one
set FOLDERCHK=c:\test2

rem ** get the date from 4 days ago
for /f "tokens=*" %%i in (' cscript //nologo c:\test\evaluate.vbs "date -4" ' ) do set DELDIR=%%i

rem ** export DIR result in folder
for /f "tokens=*" %%i in ('dir /A:D %FOLDERCHK%') do echo %%i >>output.txt

rem ** List folder list that contain the required date
findstr %DELDIR% < output.txt >>output2.txt

rem ** Output only the name of the folders
for /f "tokens=4 delims= " %%i in (output2.txt) do echo %FOLDERCHK%\%%i >>output3.txt

rem ** Filter out the "." & ".." lines
findstr /V /C:"." < output3.txt >>output4.txt
echo Folders to be deleted...
type output4.txt
set /P CONFIRM=Type 'y' to confirm...
if '%CONFIRM%=='y goto delfolders
goto end
:delfolders

rem ** Confirming the commands to be executed...remove "ECHO" to actually delete the folders...
for /f "tokens=*" %%i in (output4.txt) do echo rd /S /Q %FOLDERCHK%\%%i
:end

It's a little messy, but it works...
I've never heard of the forfiles command before, but it's available on my Win7 PC and MAKES my code redundant lol...Quote from: ShadyDave on May 07, 2010, 04:42:25 PM
I created this little bit of code;

evaluate.vbs contains the following code...
Code: [Select]Wscript.echo eval(WScript.Arguments(0))

Actually, I created evaluate.vbs. Just saying.
Apologies, I added that VBS code details after I posted it, and should put the "I created..." line after the VBS code and acknowledged you for the VBS code...

After all your help with me creating my DR check script I don't want to take any credit away from you.Got it, this is what I used:

Code: [Select]forfiles /m * /d -4 /c "cmd /c if @isdir==TRUE rd /s /q @file"
So this will look at the current directory I am in when the command is called and it will check all directories (and only directories) for the folder date and determine if it's more than 4 days old, if so, it will delete that folder.

Quote from: Panthers_Den on May 10, 2010, 01:18:50 PM
Got it, this is what I used:

Code: [Select]forfiles /m * /d -4 /c "cmd /c if @isdir==TRUE rd /s /q @file"
So this will look at the current directory I am in when the command is called and it will check all directories (and only directories) for the folder date and determine if it's more than 4 days old, if so, it will delete that folder.



Great "forfiles" is versatile and powerful.

Here is another example passing the date as a command line argument.


C:\test>type den.bat
Code: [Select]@echo off
forfiles /m *.txt /D %1 /c "cmd /c dir @file" | findstr ".txt"
Output:

C:\test>den.bat 05/08/2010
05/10/2010 05:04 PM 74 5810.txt
05/09/2010 12:56 PM 9 BATCH116-20100508-1701.txt
05/09/2010 12:56 PM 9 DATA256.txt
05/08/2010 05:01 PM 7 seq.txt

C:\test>den.bat 05/06/2010
05/10/2010 05:04 PM 74 5810.txt
05/09/2010 12:56 PM 9 BATCH116-20100508-1701.txt
05/09/2010 12:56 PM 9 DATA256.txt
05/08/2010 05:01 PM 7 seq.txt
05/07/2010 02:12 PM 9 test.txt

C:\test>den.bat 05/01/2010
05/10/2010 05:04 PM 74 5810.txt
05/09/2010 12:56 PM 9 BATCH116-20100508-1701.txt
05/09/2010 12:56 PM 9 DATA256.txt
05/08/2010 05:01 PM 7 seq.txt
05/07/2010 02:12 PM 9 test.txt
05/01/2010 09:11 AM 17 today.txt

C:\test>

p.s. It is a good idea to get a list before you deleteyea, i actually had an "echo" before the rd, but removed it when I posted the cmd on here.


Discussion

No Comment Found