View Full Version : Batch Help!
war_fox
09-07-2008, 03:51 PM
Hey everyone,
I'm new here, and I was hoping someone could help fix a little problem I've been having.
Does anyone know a lot about Batch files?
.bat and .cmd
Well, my desktop gets cluttered quickly, so I created a bat that deletes select types of files, such as .bmp, and .txt. Anyway, I can't figure out how to make it delete FOLDERS.
Can anyone show me the how to delete folders on my desktop, using a bat command?
Bani-Banan
09-07-2008, 04:17 PM
let's say you have a folder on your desktop named poontang.
@echo off
cd desktop
rmdir poontang
copy and paste that into notepad, save it as whateveryouwant.bat (make sure that the format isn't set to .txt)
enjoy
war_fox
09-07-2008, 06:30 PM
Thanks for the reply!
So, if I were trying to delete every folder and file on my desktop, I would do something like this:
rmdir /S /Q "C:\Users\<USERNAME>\Desktop"
?
Bani-Banan
09-07-2008, 07:18 PM
No, that would actually remove the desktop.
Why don't you just hold down the ctrl button and klick on the folders, then when they are selected - you could just remove them.
I'll keep searching...
war_fox
09-07-2008, 07:40 PM
Thanks!
Secret Steve Crumbles
09-07-2008, 11:23 PM
I am the king of batch files man.
What exactly are you wanting to do? Just delete every folder on your desktop, no matter the name?
war_fox
09-08-2008, 01:06 AM
Yes, exactly! :)
Secret Steve Crumbles
09-08-2008, 02:49 AM
Yes, exactly! :)OK, here's the code for your batch file:
@ECHO OFF
cd %userprofile%\desktop
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO RD /S /Q "%%G"
If you want me to explain it, let me know. If not, enjoy.
ALSO
If you want to have a "safety net" for your first trial run, REMOVE the /Q and it will prompt you for each directory deletion. Leave it in to have it automatically delete them without asking you.
Secret Steve Crumbles
09-08-2008, 12:57 PM
Great! Thank you!
But please do explain. I know you can also do a lot of damage to a computer with these batch files....OK, here's the break down of the code... First, let me post the code again:
@ECHO OFF
cd %userprofile%\desktop
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO RD /S /Q "%%G"
DIR /B /AD /S *.* - lists all directories no matter what file is in them
/B - makes the output "bare" with nothing but the file name
/AD - only lists directories
/S - recurses sub directories to include their contents if they match the listing criteria
RD /S /Q [path/name] - deletes the directory [path/dir] and all of it's children
FOR /F - processes each item (in this case directories) in the set IN ('[command]') by executing the DO [command]
%%G - is a parameter, which in this example is a directory name
"tokens=*" - says that all the characters output in a line (i.e. by the dir command) are assigned to the parameter %%G
Have you tested it yet to see if it works? Let me know if something doesn't work right and I'll modify the code for you.
war_fox
09-08-2008, 02:25 PM
Your batch works great for folders. Thanks again! And I added:
del C:\Users\myname\Desktop
one line below the batch you created, and combined, they delete absolutely everything on my desktop, just like I need.
Thank you! :D
Secret Steve Crumbles
09-08-2008, 02:30 PM
Your batch works great for folders. Thanks again! And I added:
del C:\Users\myname\Desktop
one line below of the batch you created, and combined, they delete absolutely everything on my desktop, just like I need.
Thank you! :DUm... I thought the purpose was to keep files on your desktop, just not the folders? IF you are removing the actual desktop folder, than that code is was over complicated for what you need.
tokenuser
09-08-2008, 02:44 PM
Um... I thought the purpose was to keep files on your desktop, just not the folders? IF you are removing the actual desktop folder, than that code is was over complicated for what you need.Yep .. a complete wipe could have been done in a single line.
rd %userprofile%\desktop\ /s /q
Whatever happened to the deltree command? It was more advanced, but now its gone :(
Secret Steve Crumbles
09-08-2008, 03:12 PM
Whatever happened to the deltree command? It was more advanced, but now its gone :(What was it able to do that rd can't do?
war_fox
09-08-2008, 03:34 PM
Sorry for the misunderstanding. Folders... all files... It still got the job done. :)
tokenuser
09-08-2008, 05:19 PM
What was it able to do that rd can't do?It allowed more control over the files being deleted. A cross between del and rd.
Secret Steve Crumbles
09-08-2008, 07:59 PM
It allowed more control over the files being deleted. A cross between del and rd.
You sure you're not thinking about something else?
DELTREE
Deletes a directory and all the files and subdirectories that are in it.
Syntax
DELTREE [/Y] [drive:]path [[drive:]path[...]]
---
RD
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
I remember I had this one program that I got off of a BBS back in the day called delbut. Greatest program ever.
delbut *.exe
Made cleaning out directories (before GUI's) really quick.
tokenuser
09-08-2008, 08:08 PM
You sure you're not thinking about something else?I think you might be right. The flag list was much longer - it allowed exclusion of files by extension, date, file attribute, etc. I might be thinking of something from the old Norton Utilities.
Murphy1d
09-09-2008, 01:42 AM
Seriously, where is everyone?
phatlip12
09-09-2008, 02:31 AM
Seriously, where is everyone?
Someone is itching for their Rev3 fix it seems.
;)