Reputation: 433
I want to delete all files which contain a number in the file extension - in my case the files look like:
FileName.1
FileName.2
FileName.3
...
I (naively) tried del *.*[0-9]*
but (of course) no luck. I am using a Windows 7 system where I do not have administrator rights.
Just as an info - the question is related to a question of me in a Latex forum: https://tex.stackexchange.com/questions/310238
Thanks all for the answers. It's hard for me to say which I should accept.
Upvotes: 0
Views: 2180
Reputation: 34899
I would do that as follows:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FILEMASK=%~1"
for /F "eol=| delims=" %%F in ('
dir /B /A:-D "%FILEMASK%" ^| findstr /R "\.[0-9][0-9]*$"
') do (
del /P /F "%%~F"
)
endlocal
exit /B
To use this batch program, provide the file mask as an argument. If you do not give such, the current directory will be enumerated.
The /P
switch at the del
command is added for testing purposes, so if you do not want any prompts to appear, just remove it. The /F
switch forces deletion of read-only files; if you do not want that, remove it too.
The file mask I am talking about above is for the dir
command only, which supports the global wildcards *
and ?
only, standing for a sequence of characters or a single character, respectively. See also this Microsoft article: Using wildcard characters.
There is no direct way to build a mask that matches numeric digits only, according also to this: Command-prompt wildcards for digits?
Therefore I am using the findstr
command, which supports a tiny set of regular expressions, to filter any files that have got a purely numeric file extension. See the Microsoft article Findstr and also site FINDSTR for details about this useful command.
Upvotes: 2
Reputation: 67216
This solution delete all files with a numeric extension greater than 0
:
@echo off
setlocal EnableDelayedExpansion
for %%a in (*.*) do (
set "ext=%%~Xa"
set /A "ext=!ext:~1!"
if !ext! neq 0 del "%%a"
)
To delete files in multiple folders, just add /R
switch to for
command.
Upvotes: 2
Reputation: 28963
Having said that PowerShell is nicer, easier, and more powerful, if I had to do a batch file approach, it might be:
@Echo Off
rem List all filenames into a temp file
dir /b > tmpFileList.txt
rem Isolate filenames which end in numbers into another temp file
findstr /R "\.[0-9][0-9]*$" tmpFileList.txt > tmpFileList2.txt
rem Run through the second temp file, and delete those filenames
for /f "delims=" %%L in (tmpFileList2.txt) do (
del /P "%%L"
)
rem cleanup
rm tmpFileList.txt
rm tmpFileList2.txt
Remove the /P
to delete without prompting.
You could probably do this without one or more of the temporary files, but this is straightforward enough. However this will only work if all the files are in one folder, and you are in the same folder. (The PowerShell version is easier to make it work with sub-folders).
[Edit: adjusted the 'for' loop so it can handle files with spaces in the name]
Upvotes: 1
Reputation: 28963
If you're willing to use PowerShell (which comes with Windows 7), you can make use of regex patterns. One long form approach is:
Get-ChildItem "d:\folder\" | where { $_.Extension -match '[0-9]' } | Remove-Item -Whatif
(That pattern will match extensions which contain a number, or use '^\.\d+$'
to match extensions which are entirely numbers and nothing else. The -WhatIf
flag makes it non-destructive so it tells you what it would remove, remove that flag to make it run for real).
There's a handful of ways to write this in short form, e.g.:
gci "d:\folder\" |? { $_.Extension -match '\d' } | rm
Upvotes: 1