Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch File to Delete Files/Subfolders 1

Status
Not open for further replies.

Shell02

Technical User
Joined
Nov 6, 2002
Messages
53
Location
US
I am looking to create a batch file that will delete all the files and subfolders from a directory, without deleting the root directory.

For example, I have created f:\test. I'd like to delete everything from beneath f:\test. Could someone please assist? Thanks in advance.
 
Something like this should work for you. batch file content is in red:

f:
cd test

you want f:\test to be your working dir.

dir /AD /B > %temp%\test.txt

this will dump all directories, minus date/timestamp, etc., to a temporary text file.

ping -n 1 127.0.0.1

this just gives your batch file a second to catch up.

for /F %%1 in (%temp%\test.txt) do rmdir %%1 /S /Q
del f:\test\*.*

The for command takes the output of the dir and rmdirs each entry. The del command removes all the files without removing the f:\test\ directory.
 
After the batch file executes 'for /F %%1 in (%temp%\test.txt) do rmdir %%1 /S /Q' it attempts to run 'rm New /S /Q' and errors with 'the system cannot find the file specified'. Any clue why this may be happening?
 
How about:

alldel.bat said:
rd /s /q nameofdir
mkdir nameofdir

This will actually delete the folder yes, but will recreate it once it is done.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita - that's what I always use!
 
Thank you! Is there any way to remove the contents of the directory without removing the root directory? I ask because the root directory will eventually be a share and have special security. I'd prefer not to alter it.
 
'the system cannot find the file specified'

You have a space in one of your directory names, sorry, should have thought of that. I'll see if I can get around that.
 
Yes, you are correct!
 
Ok, I got it. The problem is the FOR command sees a space as a delimeter and you need to replace that ("delims=,"). The new batch file would be:

cd \scripts\test
dir /AD /B /X > %temp%\test.txt
ping -n 1 127.0.0.1
for /F "delims=," %%1 in (%temp%\test.txt) do rmdir "%%1" /S /Q
del c:\scripts\test\*.* /Q

I also added a /Q on the last line to supress confirmation of the del command.
 
Ah, forgot to change my paths back to what you had from what I was testnig on my system. It will actually be:

f:
cd test
dir /AD /B > %temp%\test.txt
ping -n 1 127.0.0.1
for /F "delims=," %%1 in (%temp%\test.txt) do rmdir "%%1" /S /Q
del f:\test\*.* /Q
 
Works great! Thanks!

One more question... Rather than using ping, I'd like to use another way to pause the batch file for a few seconds. What is the best way to do that?
 
I always use ping. If you're concerned with creating traffic, 127.0.0.1 just pings the loopback address, which is the local interface.
 
You can prefix your command with
start /wait
which will wait for the task to finish before continuing.

eg

start /wait \\server\share\setup.exe
echo Setup has now finished.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top