Command line del c:mydir*.* – delete subfolders also

Posted on

A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about Command line del c:mydir*.* – delete subfolders also was one problem in server stack that need for a solution. Below are some tips in manage your windows server when you find problem about windows, command-line-interface, , , .

I have a script that runs the command:

del c:mydir*.*

Is there a command line switch I can use that will also delete all subfolders in that directory? Thanks!

If there are files in the C:mydir directory then you’ll need to do both lines. Otherwise, the first line will do what you want:

FOR /D %i IN (C:mydir*) DO RD /S /Q "%i"
DEL /Q C:mydir*.* 

That preserves the C:mydir directory.

Edit: David1235 is quite right. If you want to do this in a batch file, you’ll need to double-up the “%” in the “FOR …” line.

It’s a little unclear to me why David1235’s script needs the “pushd” and “popd” when you can specify the path right in the “FOR …” and “DEL …” lines, though.

If you want to run your script from anywhere, try

@echo off
pushd "C:mydir"
for /d %%d in (*.*) do rmdir /s /q "%%d"
del /q *.*

In a batch script, you need the double percent signs. From “help for”:

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

Is there a reason that:

DEL /Q /S C:mydir*

won’t work?

Instead of complex FOR…LOOPS etc. I would just use:

del /q *.* /s 

… but an even faster method is:

rd /q /s c:mydir

as RD is “remove directory” just as the old DELTREE

Leave a Reply

Your email address will not be published. Required fields are marked *