how to delete

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

how to delete

#1 Post by Mohammad_Dos » 07 Jan 2011 02:50

Hi,
I want delete all files and directories there is in c:\a
but I dont want delete this folder and just delete files and folders in it.
how?
(by one line. if it is possible)
thank u

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to delete

#2 Post by aGerman » 07 Jan 2011 14:14

DEL deletes files, RD (or RMDIR) removes folders.

As one-liner.

Code: Select all

pushd "C:\a"&&(del /f /q /a *&(for /f "delims=" %%a in ('dir /ad /b') do rd /s /q "%%~a")&popd)


But normally I would write a block (for a better readability).

Code: Select all

pushd "C:\a" &&(
  del /f /q /a *
  for /f "delims=" %%a in ('dir /ad /b') do rd /s /q "%%~a"
  popd
)


Regards
aGerman

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: how to delete

#3 Post by Mohammad_Dos » 12 Jan 2011 04:33

do u can explain that?
please teach "for" command in a new topic for us.
thank u

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: how to delete

#4 Post by ChickenSoup » 12 Jan 2011 07:26

For is a loop.
See: FOR /?

"For /f" will parse each line of a file and will output the line as a single variable or variable set depending on the delimiters used.

So, the For loop will process each line of 'dir /ad /b'. If this was run against the C: drive, you would get an output something like this as the dir command:

Code: Select all

Documents and Settings
Program Files
RECYCLER
System Volume Information
WINDOWS

Your for loop would then process each line of the above output into whatever is after your 'do' statedment. In this case it would try to do the following.

Code: Select all

rd /s /q "Documents and Settings"
rd /s /q "Program Files"
rd /s /q "RECYCLER"
rd /s /q "System Volume Information"
rd /s /q "WINDOWS"

Post Reply