batch file that will delete folders

jds123 January 27, 2004 at 11:49:49
Specs: w 2000, p4 256
Hi there

I'm having trouble accomplishing the below.

I would like a batch file that will delete sub directories and the files contained in them, but it should keep the latest 10 sub directories.
example- c:\test\ contained in this directory are sub directories named jan012004,

In the event of a new dir being added to c:test\ ie - jan112004, jan012004 should be deleted by the batch program. However if no new sub directory is added ie jan112004 the oldest sub directory should not be deleted by the batch program ie jan012004.

Thanks very much....

See More: batch file that will delete folders

Report •

January 27, 2004 at 12:09:28
Hi friend, this is DOS forum. You have asked your question in a *wrong* forum. Please note that Windows 2000 does not include "DOS". The "Command Prompt" in Win2K is just the WinNT/2K/XP system console, which is NOT DOS. Below is the *quotation* from the warning messages of this forum:

"DO NOT post questions related to NT Batch scripting on this forum. Use the Programming forum.

Use the Windows NT, 2000 or XP forum for questions regarding the Windows NT, 2000 or XP "DOS" command prompt."

For more information about the "Command Prompt" (Win32 Console) in WinNT/2K/XP, you may read this page:

Report •

January 27, 2004 at 22:09:49
Excuse me but what makes you so sure he is in the *wrong* forum. I don't see any mention of what operating sytem he he using. For all you can tell from his post he could be running msdos2.0 on a 64k comodore. I think you are out of line here Wengier. And by the way I have batch files that I run by clicking on the file in windows explorer (Win 95c) and I amm fairly sure that I can get my xp pro computer to run them too.

Hi JDS.... Try seting up your batch file using the attrib command to set the archive attribute on all files after a certain date, remove the "A" attribute on all files before that date and then xcopy all files in the root folder that have the "A" attribute turned on to a temporary folder, delete all the subdirectoies/files in"c:\test using the delete or the deltree (use with extreeme caution) commands. If you get error messages that say bad command and you know that you spelled everything right you may have to boot from a floppy disk with an OS that suppors the commands you want. I personally recomend dos 6.2 partly because it has the most comprehensive help file of all microsofts efforts. good luck I may be back aftyer I do some research on this.

Report •

January 28, 2004 at 03:12:21
" I don't see any mention of what operating sytem he he using."

In the header of his message it states w2000.

Report •

Related Solutions

January 28, 2004 at 06:15:13

So, please read the poster's "OS" column carefully before trying to make a reply, thanks.

Report •

January 28, 2004 at 15:45:02
OK so he is running win 2000. If win2k,xp etc. are not in fact DISK OPERATING SYSTEMS (DOS), then how do they operate disks, for example floppys and hard drives? Should the forum be reffered to as "command prompt" rather than as "DOS"? I have old versions of MICROSOFT DISK OPERATING SYSTEMS, that do in fact incorperate graphical user interfaces, although they are very basic, and not designed for a mouse. Don't mean to be picky here but a D.O.S. is a disk operating system wether it is called MSDOS or UNIX or Windows.

Report •

January 28, 2004 at 16:38:00

You mean all OSes are DOS (Disk Operating System)?! Quite funny, isn't it?

Report •

Jemuzu January 28, 2004 at 17:32:58
what abiut those old tape ones? they didn't use floppy discs... so... does that mean that... i'm using random knowledge at a random time?

Report •

January 30, 2004 at 07:10:40
You guys are strange. His question applies to batch files deleting directories. Personally I find nothing wrong with the post. Can you do this with DOS? YES. Will the same commands work under W2Ks command prompt? YES.

jds123, Homegrown is on the right track.

Report •

January 30, 2004 at 07:47:07
First, the OS column can be autofilled. So it might not be the OS that he's wanting to do this with.

Second. Run the MS-DOS editor, and type in the following.
ATTRIB %1\*.* -R -A -S -H

Now save it as whatever you want to name it, as long as it's followed by a .BAT extension, and is located in the PATH.
Say you name it DELDIR.BAT
and you want to delete the folder C:\DIR
you would type in DELDIR.BAT C:\DIR

It would then execute the commands in the DELDIR.BAT file as follows.
ATTRIB C:\DIR\*.* -R -A -S -H

The %1 is a variable, that will be replaced by why whatever you type in after the batch file command (in this case C:\DIR
Hoe that helped.

Report •

January 31, 2004 at 11:45:46
Personally, I've never understood why so many people on this site get their feathers ruffled because someone posts a batch file question in the DOS section. While I do agree that there are better sections for it, I don't see or understand the problem in posting it here. Unfortunetly, Justin (the one and only moderator of this forum) has been convinced by these people that batch file questions MUST be posted in the Programming forum.

I think Maineh missed the fact that you want to automate the deletion of the directories while at the same time save the newest 10 directories (based on their names). Homegrown's method should work just fine, although he/she didn't provide any code to show you how to accomplish it.

If you want to “think outside of the box” and use a more powerful scripting language, Perl can easily accomplish this and would be more readable and very possibly fewer lines of code than the batch file. If you want to go with Perl, I'd be happy to write the script for you.

Report •

January 6, 2006 at 13:27:08
I have an assembly language program I am using with both win me and win2k pro in the dos prompt. deldir.exe. It works well with 8 character file names but will not delete files such as "my file" but will work with files names such as "my_file_name" which is longer than eight characters. This program will delete all the files in all subdirectories in the directory you are in unconditional wheather a r s h attributes are set. I will send this program to you in an attachment via e-mail if you request it.

I no longer have the source code of this program. Sadley, I lost it.

Jessie Taft

Report •

Ask Question