Computing.Net > Forums > Programming > Batch File To delete folders

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File To delete folders

Reply to Message Icon

Name: gorpo86
Date: March 17, 2008 at 12:22:12 Pacific
OS: xp
CPU/Ram: p4
Comment:

hello,

I have a folder that I want to periodiclly clean out old folders. for example a folder is created every day that looks like 20080304. I want to Keep the last 30 folders and delete any folder dated before that. I am just getting started using the SET and FOR commands and need a little help getting this one going.

Thank in advance,



Sponsored Link
Ads by Google

Response Number 1
Name: devil_himself
Date: March 17, 2008 at 18:43:47 Pacific
Reply:

Try This .. It Will Skip The Latest 30 Folders And Delete The Rest .. It Is In Debug Mode ... Remove The Echo from line 5 after "do" to actually make it delete the folders

:: --- BATCH SCRIPT START ---
@echo off
setlocal
set source=C:\source
pushd "%source%"
for /f "skip=30 delims=" %%a in ('dir /b /a:d /o:-d 2^>NUL') do echo rmdir /s /q "%%a"
popd
endlocal
:: --- BATCH SCRIPT END ---


0

Response Number 2
Name: Razor2.3
Date: March 17, 2008 at 18:46:20 Pacific
Reply:

FOR /F "skip=30 delims=" %%a IN ('DIR /O-D /B /AD') DO RD /S /Q "%%a"


0

Response Number 3
Name: Mechanix2Go
Date: March 18, 2008 at 03:02:32 Pacific
Reply:

#1, no need to popd or end; it pops & ends local when the bat quits.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: gorpo86
Date: March 18, 2008 at 05:43:01 Pacific
Reply:

thanks for all the responses,

unfoutunately there are other folders contained in the directory. When i run this script it deletes folders by creation date. Is there a way to specify it to read folder names instead of creation date. the folders are in sequential order 20080102,20080103 etc.


0

Response Number 5
Name: Razor2.3
Date: March 18, 2008 at 06:25:48 Pacific
Reply:

FOR /F "skip=30 delims=" %%a IN ('DIR /B /AD 20?????? | SORT /R') DO RD /S /Q "%%a"

EDIT: Thanks, devil_himself, I really should learn to not do these past my bed time.


0

Related Posts

See More



Response Number 6
Name: gorpo86
Date: March 18, 2008 at 07:39:04 Pacific
Reply:

Razor that line looks almost works, for some reason I can use the line 'DIR /B /AD 20?????? | SORT /R'on its own and it does what it is supposed to do. Inside the for command it gives the "| was unexpected at this time." error msg


0

Response Number 7
Name: devil_himself
Date: March 18, 2008 at 09:39:19 Pacific
Reply:

FOR /F "skip=30 delims=" %%a IN ('DIR /B /AD 20?????? ^| SORT /R') DO RD /S /Q "%%a"

Escape Characters, Delimiters and Quotes
http://www.ss64.com/ntsyntax/esc.html


0

Response Number 8
Name: gorpo86
Date: March 18, 2008 at 09:49:31 Pacific
Reply:

Works like a charm!!

You guy rock thanks for all the help. Devil thanks for the link.


0

Response Number 9
Name: Mechanix2Go
Date: March 19, 2008 at 00:08:22 Pacific
Reply:

#7 is correct about escaping the pipe.

But you don't need SORT.

FOR /F "tokens=* skip=30 delims=" %%a IN ('DIR /B/AD/o-n 20??????') DO RD /S /Q "%%a"
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 10
Name: vij_guy
Date: April 9, 2008 at 12:37:11 Pacific
Reply:

use 'dir /b/ad/TC/o-d' for folders by creation time. User rmdir /q /s to remove folders or directories.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File To delete folders

Batch file to delete folder in fol. www.computing.net/answers/programming/batch-file-to-delete-folder-in-fol/16994.html

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html

Batch file to delete files in a dir www.computing.net/answers/programming/batch-file-to-delete-files-in-a-dir/14599.html