delete folders/files with only numbers in name?

Homerboy

Lifer
Mar 1, 2000
30,856
4,974
126
I need to come up with a Windows script to delete only folders and it's contents where the name contains just number (0-9) They would be a maximum of 4 characters.
Anyone have any ideas?
 

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,101
126
Code:
Remove-Item -Path P:\Test\ -Force -Recurse -Include *[0-9][0-9][0-9][0-9]*

This PowerShell will delete folders or files under P:\Test\ with exactly 4 digits in the name.

if you want to delete folders or files with 1, 2 or 3 digits in the name just add

Code:
Remove-Item -Path P:\Test\ -Force -Recurse -Include *[0-9]*
Remove-Item -Path P:\Test\ -Force -Recurse -Include *[0-9][0-9]*
Remove-Item -Path P:\Test\ -Force -Recurse -Include *[0-9][0-9][0-9]*

to the above script

or just any number of digits anywhere in the name
Code:
Remove-Item -Path P:\Test\ -Force -Recurse -Include *[0-9]*
is all you need.

https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
 
Last edited:
  • Like
Reactions: Homerboy

Homerboy

Lifer
Mar 1, 2000
30,856
4,974
126
After some digging, I realized all the folders begin with "6". So I was able to do it this way:

FOR /D %%X IN (C:\folder\6*) DO RD /S /Q "%%X"
 
  • Like
Reactions: mxnerd

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,101
126
Yeah, it can be done by installing Linux subsystem. But it's kind of overkill.

==

You also can get things done with VBscript instead of using batch file or PowerShell.
 
Last edited: