112

I need to delete all .jpg and .txt files (for example) in dir1 and dir2.

What I tried was:

@echo off
FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t

In some directories it worked; in others it didn't.

For example, this didn't do anything:

@echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t

What I'm I missing in the second snippet? Why didn't it work?

ruffin
  • 16,507
  • 9
  • 88
  • 138
vexe
  • 5,433
  • 12
  • 52
  • 81
  • It would help if you provided more information about how *exactly* it "didn't work". How does the actual result differ from the expected result? Do you get an error message? Which? – Ansgar Wiechers Oct 31 '12 at 03:04

7 Answers7

280

You can use wildcards with the del command, and /S to do it recursively.

del /S *.jpg

Addendum

@BmyGuest asked why a downvoted answer (del /s c:\*.blaawbg) was any different than my answer.

There's a huge difference between running del /S *.jpg and del /S C:\*.jpg. The first command is executed from the current location, whereas the second is executed on the whole drive.

In the scenario where you delete jpg files using the second command, some applications might stop working, and you'll end up losing all your family pictures. This is utterly annoying, but your computer will still be able to run.

However, if you are working on some project, and want to delete all your dll files in myProject\dll, and run the following batch file:

@echo off

REM This short script will only remove dlls from my project... or will it?

cd \myProject\dll
del /S /Q C:\*.dll

Then you end up removing all dll files form your C:\ drive. All of your applications stop working, your computer becomes useless, and at the next reboot you are teleported in the fourth dimension where you will be stuck for eternity.

The lesson here is not to run such command directly at the root of a drive (or in any other location that might be dangerous, such as %windir%) if you can avoid it. Always run them as locally as possible.

Addendum 2

The wildcard method will try to match all file names, in their 8.3 format, and their "long name" format. For example, *.dll will match project.dll and project.dllold, which can be surprising. See this answer on SU for more detailed information.

Laf
  • 7,965
  • 4
  • 37
  • 52
  • 3
    appending a line `pause` in the end will allow user to see complete log information. – KNU Feb 04 '15 at 06:27
  • 2
    Thanks for the addendum, Laf. I think it will help others spotting this indeed important difference. – BmyGuest Oct 19 '16 at 20:14
  • If anybody want to delete from a specific dir, you can combine `cd` and `del` commands with `&' see https://stackoverflow.com/q/8055371/7429464 – FindOutIslamNow Apr 29 '18 at 13:52
  • @FindOutIslamNow or you can specify the full path in the `del` command, without having to change the current directory. – Laf May 01 '18 at 12:26
  • Just a small caveat to this - it will cause an error if no files to delete are found. – Jamie Marshall Jan 04 '19 at 17:16
  • 2
    I noticed that when I do this, it deletes any file extension that _starts_ with the one specified. For example, if I have `.jsx` files and `.jsxbin` files in a folder and use `del /S *.jsx`, it also deletes the `.jsxbin` files. Putting quotes around `*.jsx` didn't change anything. Is there a way to force it to delete files with exactly the extension specified? – Marty Dec 16 '20 at 19:41
  • @Marty I think [this answer](https://stackoverflow.com/a/60837669/994125) will help you understand why my answer does not work in your case. Had to search for a few minutes before I found this out, never knew about that. – Laf Dec 17 '20 at 14:07
10

You can use this to delete ALL Files Inside a Folder and Subfolders:

DEL "C:\Folder\*.*" /S /Q

Or use this to Delete Certain File Types Only:

DEL "C:\Folder\*.mp4" /S /Q 
DEL "C:\Folder\*.dat" /S /Q 
StackBuck
  • 789
  • 1
  • 12
  • 34
5

I wrote a batch script a while ago that allows you to pick a file extension to delete. The script will look in the folder it is in and all subfolders for any file with that extension and delete it.

@ECHO OFF
CLS

SET found=0
ECHO Enter the file extension you want to delete...
SET /p ext="> "

IF EXIST *.%ext% (           rem Check if there are any in the current folder :)
  DEL *.%ext%
  SET found=1
)
FOR /D /R %%G IN ("*") DO (  rem Iterate through all subfolders
  IF EXIST %%G CD %%G
  IF EXIST *.%ext% (
    DEL *.%ext%
    SET found=1
  )
)

IF %found%==1 (
  ECHO.
  ECHO Deleted all .%ext% files.
  ECHO.
) ELSE (
  ECHO.
  ECHO There were no .%ext% files.
  ECHO Nothing has been deleted.
  ECHO.
)

PAUSE
EXIT

Hope this comes in useful to anyone who wants it :)

Pyroglyph
  • 164
  • 5
  • 14
  • Alternatively, if you have Bash installed (Cygwin, WSL, etc), then a simple `find . -name "*.ext" -type f -delete` (where ext is your chosen extension) will do. – Pyroglyph Mar 11 '19 at 16:53
1

I don't have enough reputation to add comment, so I posted this as an answer. But for original issue with this command:

@echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t

The first For is lacking recursive syntax, it should be:

@echo off
FOR /R %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t

You can just do:

FOR %%p IN (C:\Users\0300092544\Downloads\Ces_Sce_600) DO @ECHO %%p

to show the actual output.

0

this is it:

@echo off

:: del_ext
call :del_ext "*.txt"
call :del_ext "*.png"
call :del_ext "*.jpg"

:: funcion del_ext
@echo off
pause
goto:eof
:del_ext
 set del_ext=%1
 del /f /q "folder_path\%del_ext%"
goto:eof

pd: replace folder_path with your folder

acgbox
  • 312
  • 2
  • 13
0

Step 1: Navigate to the folder in question using the cd command

For example: cd C:\Users\tremanleo\Desktop\HoldLEOCMS

Step 2 Delete the the file type.

For Example: DEL *.bak

-11

If you are trying to delete certain .extensions in the C: drive use this cmd:

del /s c:\*.blaawbg

I had a customer that got a encryption virus and i needed to find all junk files and delete them.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 14
    This seems terrifyingly unsafe. – dudewad Apr 27 '15 at 16:20
  • 6
    What is the main difference between this answer (-2 votes) and the accepted one (+42 votes) ? – BmyGuest Jul 08 '16 at 15:51
  • 7
    @BmyGuest The main difference is if you execute my command somewhere in a sub-folder, it will only affect the files located under this subfolder. In this answer, you delete _everything_ that matches the file name in the entire drive. So if you wanted to delete some dll files in your project, and you mistakenly type `del /s c:\*.dll`, then you're up for a very bad surprise. **Never** run such commands on the whole drive, unless _absolutely_ necessary. – Laf Oct 19 '16 at 13:01
  • 3
    I don't think this answer deserve that many downvotes. The answer explains that this command is for the whole drive (and why). `c:` should be replaced by the desired folder if the person does not want the whole drive. – user1527152 Apr 28 '18 at 23:51
  • 3
    I think it needs as many down-votes if not more as it serves as a disclaimer and warning of what not to do. You could really screw things up by copying, pasting, and executing code like this. There are many Easter eggs built into bash to teach this exact lesson in a "safe" way.. this example could ruin your day. Best! – jacktrader Apr 17 '19 at 16:52