I am trying to run a command that will search a bunc h of folders and DELETE ALL files EXCEPT a specific file.
I am using this right now.
for %i in (*) do if not %i == .msg del %i
Questions:
1. This does not work on subfolders. How do I modify it to do so?
2. How do I specify more than one type of file to KEEP (right now only the .msg are kept)
PLEASE HELP!!!!! Cheers
batch delete specific file type in subfolders
Moderator: DosItHelp
Re: batch delete specific file type in subfolders
This is for the cmd prompt:
You have to navigate to the folder you want to run the command beforehand.
You could also define the path where to start:
Delete the ECHO command if it outputs the right files.
You have to double the percent signs if you want to run the FOR loop from within a batch script.
Steffen
Code: Select all
for /r %i in (*.msg) do ECHO del "%~fi"
You have to navigate to the folder you want to run the command beforehand.
You could also define the path where to start:
Code: Select all
for /r "C:\whatever path" %i in (*.msg) do ECHO del "%~fi"
Delete the ECHO command if it outputs the right files.
You have to double the percent signs if you want to run the FOR loop from within a batch script.
Steffen
Re: batch delete specific file type in subfolders
It seems that the OP wants to exclude filetypes from the delete command.
This can be very slow if there are a bazillion number of files, otherwise it works.
This can be very slow if there are a bazillion number of files, otherwise it works.
Code: Select all
@echo off
set exclude=msg txt gif mp3 mkv
for /f "delims=" %%a in ('dir /b /s /a-d ^|findstr /ev "%exclude%" ') do del "%%a"
Re: batch delete specific file type in subfolders
jaykappy wrote:I am trying to run a command that will search a bunc h of folders and DELETE ALL files EXCEPT a specific file.
I am using this right now.
for %i in (*) do if not %i == .msg del %i
Highly doubtful this works as you are not using the command modifier to only compare the extension.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: batch delete specific file type in subfolders
Squashman wrote:jaykappy wrote:I am trying to run a command that will search a bunc h of folders and DELETE ALL files EXCEPT a specific file.
I am using this right now.
for %i in (*) do if not %i == .msg del %i
Highly doubtful this works as you are not using the command modifier to only compare the extension.
The file could literally be called .msg