Batch Scripting - Delete all Directories within a Directory

Terribly sorry for bumping such and old thread, but I'm at my wits end here.

Fitz, you may be my saviour in this.

I need *almost* the exact same thing, except the files in the root must be intact.

Code:
So far I've adapted your code thusly;
@echo off
dir /b /ad %1 > dirlist.txt
for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
pause
I removed the cleanup feature to find out why this was not working, as well as the pause to actually see it.
Apparently, the files cannot be found. I'll try and translate it as best as I can;
The error message I get is: "The file cannot be found."
Is there any way you can see what I'm doing wrong, and help me fix it? I'd greatly appreciate it.

I am also trying to make a related script, and I won't bother everyone by making a separate thread for it - as it's related:
It's like this;
I've been scratching my head over this for some time now;

Code:
@echo off
set /p name="Skriv elev navn(e): "
echo 1
md %name%
echo 2
echo %name%>>folderlog.txt
echo 3
echo %name%>%name%\%name%.txt
echo 4
pause
This is the code I've got so far, obviously the echo's are for debugging.

This is the aim of this script;

To create as many folders as required, in this case student names - e.g.: John, Jake, Peter etc.
I need it to create the folder, a .txt file within the newly created folder containing the number the folder was during creation - and the name of the folder.
As such: MAIN DIR\John\John.txt - containing string: <number>, <John>.
I hope this is clear enough.
I have yet to figure out the numbering issue, and it's a real head-scratcher. However, I have successfully managed to get it to create the .txt file with "John" in it.
However, when I try to make more than one folder - the script breaks only creating the folders, and no .txt files. This happens at the .txt file creation string.
That's basically where I'm stuck. I've worked on it for hours today, with no luck.

Please your wizard of command prompt! SAVE ME! :D
 
I need *almost* the exact same thing, except the files in the root must be intact.

Code:
So far I've adapted your code thusly;
@echo off
dir /b /ad %1 > dirlist.txt
for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
pause
That batch should work fine with what you are trying to do.

2 questions:
1) How are you calling the batch file
2) what are the contents of dirlist.txt after it runs.


I am also trying to make a related script, and I won't bother everyone by making a separate thread for it - as it's related:

--------

This is the aim of this script;

To create as many folders as required, in this case student names - e.g.: John, Jake, Peter etc.
I need it to create the folder, a .txt file within the newly created folder containing the number the folder was during creation - and the name of the folder.
As such: MAIN DIR\John\John.txt - containing string: <number>, <John>.
I hope this is clear enough.
I have yet to figure out the numbering issue, and it's a real head-scratcher. However, I have successfully managed to get it to create the .txt file with "John" in it.
However, when I try to make more than one folder - the script breaks only creating the folders, and no .txt files. This happens at the .txt file creation string.
That's basically where I'm stuck. I've worked on it for hours today, with no luck.
Do you know the names of the people ahead of time? would be much easier to read it from a text file instead of prompting the user to enter it each time.

Otherwise, you can use loops and counting and setting an environment variable to update each iteration through to do the count of the number of iterations through.

Really though, this would probably be much easier to do in vbscript. If you're open to doing it in VBScript, let me know and I can whip that up for you too. Been a while since I did loops and counting in straight batch.
 
Code:
@echo off
for /f "tokens=* delims=" %%i in ('dir /b /a:d') do (
    rd "%%i" /s /q
    )
This nifty piece of code deletes all subdirectories - with any name - in any folder - including their contents.

It's what I ended up with.

As for the folder creation, that's solved as well - don't have the code on me - I'll post it later.
 
that's really doing the same thing as my script but instead of piping the the data from the file, it's directly executing the dir command. I like having the text file created first as I often times like to see what it's doing before it does it.

edit: it also runs the dir command only once instead of each time the for loop executes
 
Greetings from the future!

I found this forum while searching for a y to delete all files and folders in temp folders.
After fiddling around and using /? a bit, I decided upon this solution:

Code:
CHDIR %temp% 
DEL *.* /s /q 
FOR /d %%i in (*) DO rd /s /q "%%i"

DEL wipes out any files in the current folder and all subfolders.
The /d switch in FOR means it only finds directories.
(Quicker and cleaner than running "dir /b /ad" every time.


Based on previous posts, there are a few shortcomings to this method:
1. It doesn't output a text file, which could be dangerous if you're in a rather important folder.
2. It deletes every file in every folder first instead of deleting all folders and then cleaning up just the files in the main folder. (This was for styling purposes. I like having the longest line last and the speed difference is marginal.

Maybe somebody else will find this useful in 5 years and the cycle can begin anew.
 
fitz: Just now noticed, your script would fail if you were not allowed to write a new file to the current directory the batch file is executed in...
 
wow.. frankly, I don't even remember this thread.. lol

X: that's true - it would also fail if you didn't have permissions on any of the files/folders you were trying to delete.. I could wrap a script to take ownership and change permissions using other utilities and/or VBScript.. but that's too much work for an old thread :)

Besides, your earlier suggestion of installing SFU and/or Cygwin and just doing a rm-rf is still the easiest (still would have permissions issues though if you didn't have the rights..)
 
wow.. frankly, I don't even remember this thread.. lol

X: that's true - it would also fail if you didn't have permissions on any of the files/folders you were trying to delete.. I could wrap a script to take ownership and change permissions using other utilities and/or VBScript.. but that's too much work for an old thread :)

Besides, your earlier suggestion of installing SFU and/or Cygwin and just doing a rm-rf is still the easiest (still would have permissions issues though if you didn't have the rights..)

rm -rf would attempt to remove everything, if it did not have permissions on a file it would just skip over the file and move onto the next one ... and it won't require a file to be written to disk first.
 
Hi i found this tread trying to find something to clear my desktop from files and folders.

I have been trying different solutions but none of them has worked as i wanted.

@echo off
:foldercheck1
if not exist C:\Death\ goto make1
:cleanup
xcopy "%USERPROFILE%"\Desktop\*.* /f/e/y c:\Death
rmdir "%USERPROFILE%"\Desktop\ /s /q
pause
mkdir "%USERPROFILE%"\Desktop
goto foldercheck2
:make1
mkdir C:\Death
goto foldercheck1

goto cleanup

:foldercheck2
if not exist %USERPROFILE%"\Desktop\ goto make2

:make2
mkdir "%USERPROFILE%"\Desktop
goto done

:done
exit


This does what i wan´t but i removes "Desktop"
and then create "Desktop", but sometimes it doesen´t.
By adding "pause"
it somehow increases the posibility for the script to be able to create it.

Basicly i want to copy all files and folders from "Desktop" to
c:\Death
then delete all files and folders from "Desktop"

Then manually go through the "Death" folder and delete files that i dont want.

The reason for this is that my computer is used by others then me, using my account.
People.. including me.. don´t listen to my words "Do not save stuff on the desktop!"

I tryed this
@echo off
dir /b /ad %1 > dirlist.txt
for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
del %1\*.*
del dirlist.txt

At first i got "Access denied" and "File not found" etc...
Tryed to change permission etc but couldnt get it to function on desktop.
And by a little mistake i cleaned some stuff in "c:\" ...
So i obviously i need some help..

Im using Windows 7
 
Replace:

rmdir "%USERPROFILE%"\Desktop\ /s /q
pause
mkdir "%USERPROFILE%"\Desktop

With:

del "%USERPROFILE%"\Desktop\*.*

Saves you some hassle :)
 
why can´t this script remove folders that has a "space" in it like this
c:\test\New folder
?
But can remove this
c:\test\Newfolder
?
If i can get this running then all my problems would be solved.


dir /b /ad c:\test > dirlist.txt
for /f %%i in (dirlist.txt) do rd /s /q c:\test\%%i
del c:\test\*.*
pause

Please help!
 
you could try del newfold~1

How do i tell the script to put that in "dirlist.txt"

I don´t now what folders are in "c:\test" , it could be anything..

Why isn´t there a batch command to just empty a folder?
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back