| 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? http://www.raymond.cc/blog/archives/2007/09/24/deltree-command-replacement-in-windows-2000-or-windows-xp/ 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; 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: 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. |
|