[SOLVED] + Delete Multiple File Type Question? +

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

[SOLVED] + Delete Multiple File Type Question? +

#1 Post by Dos_Probie » 24 Oct 2012 07:54

Would like a script to automate a bulk delete of multiple file types from my C: root drive ex. *.bmp, *.cab, *.dll, *.exe, *.ini, *.log, *.msi, *.txt Does anyone have a batch in a for loop that could do this?
have been using the below code to delete single file types but now have a
need to add the eight file types above to it..
Thanks for the help.. Dos_Probie :D

Code: Select all

@echo off

::VARIABLES::
set sd=%systemdrive%
set del=del /q/f
set cmd=cmd /c

:: CLEANUP SYSTEM DRIVE CRAP
for /f "delims=" %%a in ('dir/b "%sd%\*.bmp"') do (
%cmd% %del% "%sd%\%%a">nul 2>&1
)
Last edited by Dos_Probie on 24 Oct 2012 14:14, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: + Delete Multiple File Type Question? +

#2 Post by abc0502 » 24 Oct 2012 08:55

while reading your question i had an idea, why not using something like lists in python instead of using a file that contain all your extension that you want to delete.

Try this:

Code: Select all

@echo off

::VARIABLES::
set sd=%systemdrive%
set del=del /q/f
set cmd=cmd /c
set "list=bmp,cab,dll,exe,ini,log,msi,txt"
set "list_len=8"

setlocal enabledelayedexpansion
For /L %%@ in (1,1,%list_len%) Do (
   For /F "tokens=1-%list_len% delims=," %%a in ("%list%") Do (
      Call :del_type "%%a"
      Call :del_type "%%b"
      Call :del_type "%%c"
      Call :del_type "%%d"
      Call :del_type "%%e"
      Call :del_type "%%f"
      Call :del_type "%%g"
      Call :del_type "%%h"
   )
)

Pause
Exit /B

:del_type
For /F "delims=" %%a in ('dir/b "%sd%\*.%~1"') do (
   %cmd% %del% "%sd%\%%a">nul 2>&1
)
goto :eof


Edited:
This is the new version of the code above

Code: Select all

@echo off
::VARIABLES::
set sd=%systemdrive%
set del=del /q/f
set cmd=cmd /c

:: Max list elements & lenght is 9
set "list=txt,py,jpg,exe"
set "list_len=4"

setlocal enabledelayedexpansion
For /F "tokens=1-%list_len% delims=," %%a in ("%list%") Do (
   Call :command "%%a" "%%b" "%%c" "%%d"
    )
)

Pause
Exit /B

:command
Pushd %sd%
For /F "delims=" %%a in ('Dir /B /A:-D "*.%~1" "*.%~2" "*.%~3" "*.%~4" "*.%~5" "*.%~6" "*.%~7" "*.%~8" "*.%~9"') Do (
   %cmd% %del% "%%a">nul 2>&1
)
Popd
goto :eof

The command function can be changed depending on the commands that is required.
Last edited by abc0502 on 24 Oct 2012 09:36, edited 1 time in total.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: + Delete Multiple File Type Question? +

#3 Post by Dos_Probie » 24 Oct 2012 08:59

Thanks for the quick reply and code ABC!... :D

I also came up with the below code...its pretty simple but it works

Code: Select all

set del=del /q/f
cd\
cd %SystemDrive%

%del% *.bmp, *.cab, *.dll, *.exe, *.ini, *.log, *.msi, *.txt


abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: + Delete Multiple File Type Question? +

#4 Post by abc0502 » 24 Oct 2012 09:02

that is so typical :lol:
but thanks, your question gave me something to do now :)

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: + Delete Multiple File Type Question? +

#5 Post by Dos_Probie » 24 Oct 2012 10:02

abc0502 wrote:while reading your question i had an idea,..instead of using a file that contain all your extension that you want to delete.


Thanks again ABC, your answer to Not List Extensions gave me the code to add after my silent install of C++ :D

Code: Select all

@echo off

set cd=%~dp0
set del=del /q/f

:: SILENT INSTALL C++
%cd%\vcredist2008_x64.exe /q:a /c:"VCREDI~1.EXE /q:a /c:""msiexec /i vcredist.msi /qn"" "
cls
:: CLEAN-UP INSTALL FILES
cd\
cd %systemdrive%
%del% *.*
cd\
cls

exit

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: + Delete Multiple File Type Question? +

#6 Post by Dos_Probie » 24 Oct 2012 10:18

This also works for all files from the For loop .. :mrgreen:

Code: Select all

cd\ 
cd %systemdrive%
for /f "delims=" %%a in ('dir/b "*.*"') do (
%del% "%%a">nul 2>&1
)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: + Delete Multiple File Type Question? +

#7 Post by abc0502 » 24 Oct 2012 11:02

Dos_Probie wrote:This also works for all files from the For loop .. :mrgreen:

Code: Select all

cd\ 
cd %systemdrive%
for /f "delims=" %%a in ('dir/b "*.*"') do (
%del% "%%a">nul 2>&1
)

but this will delete every thing

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: + Delete Multiple File Type Question? +

#8 Post by dbenham » 24 Oct 2012 12:39

I don't understand why the original script is so complicated. I don't see any need for CMD, nor a FOR loop.

I believe the following will do what you want:

Code: Select all

@echo off
setlocal
set sd=%systemdrive%
del /f %sd%\*.bmp %sd%\*.cab %sd%\*.dll %sd%\*.exe %sd%\*.ini %sd%\*.log %sd%\*.msi %sd%\*.txt


I suppose the following form with a FOR loop might be marginally easier to maintain:

Code: Select all

@echo off
for %%X in (bmp cab dll exe ini log msi txt) do del /f %systemdrive%\*.%%X


Dave Benham

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: + Delete Multiple File Type Question? +

#9 Post by Dos_Probie » 24 Oct 2012 14:12

ABC, Yes the *.* will delete all files but really doesn't matter since I will only be using this when doing
a clean install of Windows and the only files in my systemdrive at that time will be install orphan files.
Also, thanks Dave for your input, I am going with your oneliner with the for loop code as its short and consise,
plus I don't need to do the change directory..Thanks again everyone.. :P

Post Reply