Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to delete subfolders using batch script

1,326 views
Skip to first unread message

adilsiv

unread,
Nov 27, 2009, 8:53:49 AM11/27/09
to
Hi guys,

How can I delete only subfolder/s and files which it conatins using a
batch file.

thanks

adil

foxidrive

unread,
Nov 27, 2009, 9:23:03 AM11/27/09
to
On Fri, 27 Nov 2009 05:53:49 -0800 (PST), adilsiv <joh...@googlemail.com>
wrote:

>How can I delete only subfolder/s and files which it conatins using a
>batch file.

And leave the parent folder?

@echo off
rd "c:\folder" /s /q
md "c:\folder"

adilsiv

unread,
Nov 27, 2009, 9:35:16 AM11/27/09
to

Hi there,

the parent folder is shared in the datacenter server,so I need to
make sure that I won't delete the parent folder and it's contents only
the required sub folder/s.

eg:- //server01/etc/management/ under this I have got 5 sub folders
and files.
I only need to delete the sub folder and it's contents under
the //.../../management/ called "temp"

thanks in advance

adil

Pegasus

unread,
Nov 27, 2009, 9:30:25 AM11/27/09
to

"adilsiv" <joh...@googlemail.com> wrote in message
news:79f14374-d8ca-4e26...@k4g2000yqb.googlegroups.com...

You wrote "How can I delete only subfolder/s and files which it
conatins (contains)". Does "it" refer to the parent folder or does
it refer to the subfolder(s)?

If "it" refers to the parent folder then the simplest method is to
delete the parent folder, then to recreate it:
@echo off
set Folder=D:\My Folder\My Files
rd /s /q "%Folder%"
md "%Folder%

If "it" refers to the files contained in the subfolders then this will do:
@echo off
set Folder=D:\My Folder\My Files
for /d %%a in ("%Folder%\*.*") do echo rd /s /q "%%a"

Remove the word "echo" in the last line to activate the batch file.


01MDM

unread,
Nov 27, 2009, 10:01:54 AM11/27/09
to

@echo off
pushd "C:\Folder"
rd /s /q "." 2>nul
popd

01MDM

unread,
Nov 27, 2009, 10:04:24 AM11/27/09
to

Not use above. It can delete current folder if not exist C:\Folder
Use this:

@echo off
pushd "C:\Folder" && rd /s /q "." 2>nul
popd

adilsiv

unread,
Nov 27, 2009, 10:17:49 AM11/27/09
to

I tried this script on .bat file nothing happened.

set Folder=C:\Temp\New Folder
for /d %%a in ("%Folder%\*.*") do rd /s /q "%%a"

adil

foxidrive

unread,
Nov 27, 2009, 10:33:21 AM11/27/09
to
On Fri, 27 Nov 2009 06:35:16 -0800 (PST), adilsiv <joh...@googlemail.com>
wrote:

>Hi there,


>
>the parent folder is shared in the datacenter server,so I need to
>make sure that I won't delete the parent folder and it's contents only
>the required sub folder/s.
>
>eg:- //server01/etc/management/ under this I have got 5 sub folders
>and files.
>I only need to delete the sub folder and it's contents under
>the //.../../management/ called "temp"

This is untested:

@echo off
rd /s /q "//server01/etc/management/temp"

Pegasus

unread,
Nov 27, 2009, 10:31:38 AM11/27/09
to

"adilsiv" <joh...@googlemail.com> wrote in message
news:1ed266d4-f967-430d...@j24g2000yqa.googlegroups.com...

Make it like so:

set Folder=C:\Temp\New Folder
for /d %%a in ("%Folder%\*.*") do rd /s /q "%%a"

(i.e. with @echo off at the start), then run the batch file from a Command
Prompt so that you can see what's going on.


>

unread,
Nov 27, 2009, 4:48:52 PM11/27/09
to
adilsiv said :

> I tried this script on .bat file nothing happened.
>
> set Folder=C:\Temp\New Folder
> for /d %%a in ("%Folder%\*.*") do rd /s /q "%%a"
>
> adil

This following is uses the line I've always successfully used and I see
only one reason why you believe it not to work for you.

::----- START KILLKIDS.CMD -----
@ECHO OFF & SETLOCAL
SET "Folder=C:\Temp\New Folder"
FOR /D %%# IN ("%Folder%\*") DO RD/S/Q "%%#"
::------ END KILLKIDS.CMD ------

That reason is that it will not remove hidden|system directories.

This will, subject to permissions. (PLEASE BE CAREFUL)!
::----- START KILLKIDS.CMD -----
@ECHO OFF & SETLOCAL
SET "Folder=C:\Temp\New Folder"
FOR /F %%# IN ('DIR/B/AD "%Folder%") DO RD/S/Q "%%~f#"
::------ END KILLKIDS.CMD ------


Todd Vargo

unread,
Nov 27, 2009, 11:14:13 PM11/27/09
to

adil, during your learning curve, I recommend you avoid using variables when
deleting files or folders using wildcards. The following is much safer
method while learning.

for /d %%a in ("C:\Temp\New Folder\*.*") do rd /s /q "%%a"

The reason, a very typical error many people make with learning to use
variables in batch files is that they tend to insert spaces around the "="
(as is commonly done in most programming languages). If this error were to
occur with this batch code, then the variable %Folder% would expand to
nothing leaving "\*.*" to delete all folders beginning at the root folder.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

>

unread,
Nov 28, 2009, 7:58:50 AM11/28/09
to
ten.n...@virgin.net said :

> That reason is that it will not remove hidden|system directories.
>
> This will, subject to permissions. (PLEASE BE CAREFUL)!
>>> ----- START KILLKIDS.CMD -----
> @ECHO OFF & SETLOCAL
> SET "Folder=C:\Temp\New Folder"
> FOR /F %%# IN ('DIR/B/AD "%Folder%") DO RD/S/Q "%%~f#"
>>> ------ END KILLKIDS.CMD ------

Correction, this will!


::----- START KILLKIDS.CMD -----
@ECHO OFF & SETLOCAL
SET "Folder=C:\Temp\New Folder"

FOR /F "TOKENS=*" %%# IN ('DIR/B/AD "%Folder%") DO RD/S/Q "%%~f#"
::------ END KILLKIDS.CMD ------


Message has been deleted
0 new messages