user3390915
user3390915

Reputation: 43

What am I doing wrong with my .bat that deletes a folder?

I have a folder named "Red" and in it is a jpeg. I want to delete this folder, and the picture inside, but I don't want it to ask me if i want to delete it, I just want the program to delete it and move on.

It's located on my desktop.

Here is the code I have to delete the folder and picture

del /Q "C:\Users\Chris\Desktop\Red"

When I run the .bat it doesn't do anything, it just opens the CMD and closes. Is there a problem with the syntax? Thanks!

Upvotes: 0

Views: 50

Answers (1)

paxdiablo
paxdiablo

Reputation: 881093

The del command is not the right tool for deleting a folder and its contents. Rather you should use rmdir (or rd) instead, with the option to delete subfolders/files (and without prompting):

rmdir /s /q whatever

The del command will work quietly without prompting and it will descend through directory structures but it will only delete files in those structures. If you set up the following hierarchy:

xyzzy
|
+----- xyzzy.txt
|
+----- plugh
       |
       +----- plugh.txt
       |
       +----- twisty
              |
              +----- twisty.txt

and then run del /s /q xyzzy, all you will see is:

Deleted file - C:\Users\Pax\Documents\xyzzy\xyzzy.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\plugh.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\twisty\twisty.txt

and you'll be left with the tree untouched (but all the files gone):

xyzzy
|
+----- plugh
       |
       +----- twisty

If instead you use rmdir /s /q xyzzy, the whole tree (including the top level) will be removed.

Upvotes: 1

Related Questions