How can I delete only subfolder/s and files which it conatins using a
batch file.
thanks
adil
>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"
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
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.
@echo off
pushd "C:\Folder"
rd /s /q "." 2>nul
popd
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
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
>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"
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.
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 ------
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)
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 ------