This article introduces a simple way to list and delete all files with a certain name pattern in a folder and all its sub-folders. A common scenario is deleting the hidden thumbs.db files, which Windows XP (and in some cases, Windows 7) stores in each folder that contains pictures or video files.
Using Command Prompt
- Open the Command Prompt. You’ll find it in the Start menu, the Accessories folder.
- Change the current folder to the one containing the files you’d like to delete. To so, use the cd /d command. For example:
cd /d "C:\Documents and Settings\test\My Documents"
- (Optional) Get a list of the files you’d like to delete. This will give you a chance to verify what is deleted before actually deleting them. To do so, use the dir /a /b /s command. For example:
- To get a list of all
thumbs.db
files in all subfolders:dir thumbs.db /a /b /s
- To get a list of all files with the generic name pattern of
a*.txt
(where * is placeholder for any number of characters) in all subfolders:dir a*.txt /a /b /s
- To get a list of all
- Delete those files. To do so, use the del /a /s command. For example:
- To delete all
thumbs.db
files in all subfolders:del thumbs.db /a /s
- To delete all files with the generic name pattern of
a*.txt
(where * is placeholder for any number of characters) in all subfolders:del a*.txt /a /s
- To delete all
You cannot delete a group of files across more than one volume with only one command. You must repeat this command for each drive.
Using PowerShell
Imitating Command Prompt’s syntax
PowerShell is incredibly powerful. There are myriads of ways in which you can accomplish this task in PowerShell. So, let’s start with imitating what we did above in the Command Prompt. Then, we’ll see a better way of doing it.
- Start PowerShell. You’ll find it in Start Menu.
- Change the current folder to the one containing the files you’d like to delete. To so, use the Set-Location command. For example:
Set-Location 'C:\Users\test\Documents'
- (Optional) Get a list of the files you’d like to delete. This will give you a chance to verify what is deleted before actually deleting them. To do so, use the Get-ChildItem command. For example:
- To get a list of all
thumbs.db
files in all subfolders:Get-ChildItem thumbs.db -Recurse -Force
- To get a list of all files with the generic name pattern of
a*.txt
(where*
is placeholder for any number of characters) in all subfolders:Get-ChildItem a*.txt -Recurse -Force
- To get a list of all
- Delete those files. To do so, you can use the
Remove-Item
command. For example:- To delete all
thumbs.db
files in all subfolders:Remove-Item * -Include thumbs.db -Recurse -Force
- To delete all files with the generic name pattern of
a*.txt
(where*
is placeholder for any number of characters) in all subfolders:Remove-Item * -Include a*.txt -Recurse -Force
- To delete all
To summarize:
Set-Location 'C:\Users\test\Documents'
Get-ChildItem thumbs.db -Recurse -Force
Get-ChildItem a*.txt -Recurse -Force
Remove-Item * -Include thumbs.db -Recurse -Force
Remove-Item * -Include a*.txt -Recurse -Force
A better way
PowerShell has done away with many idiosyncrasies of the Command Prompt. You can do everything in one command instead of five:
Remove-Item 'C:\Users\test\Documents\*' -Include 'thumbs.db','a*.txt' -Recurse -Force -Confirm
But perhaps it’s better to break this line down to its components. Here is the expanded version:
Remove-Item `
'C:\Users\test\Documents\*' `
-Include 'thumbs.db','a*.txt' `
-Recurse `
-Force `
-Confirm
And here is what each piece does:
- The first parameters defines the path to the items to delete, in this case,
'C:\Users\test\Documents\*'
. This item means: Anything whose path begins with ‘C:\Users\test\Documents\’. - The
-Include
parameter defines whitelist filter, i.e., only delete items whose names match one of these patterns: ‘thumbs.db
‘ and ‘a*.txt
‘ - The
-Recurse
parameter specifies that the asterisk in the first parameters includes items in subfolders too. - The
-Force
parameter is for deleting hidden files like thumbs.db. Ordinarily, you mustn’t use it. - The
-Confirm
parameter makes the command ask for confirmation for each item before deleting it. Alternatively, you can use the-WhatIf
parameter to see exactly what you are deleting, without deleting them.
Nice! I’m been using Win 7 Ultimate and have been having problems with Thums.db for quite some time. Thanks!
LikeLike