EnvMatt
EnvMatt

Reputation: 33

Delete all files except files with no extension in command line

I have an application that dumps logs to a folder. It writes the files the most recent file without an extension and each old version gets a .1, .2, .3, etc. appended to the end.

Example:

Filename   
Filename.1  
Filename.2  
Filename.3

I would like to write a basic script that erases "filename.?", but whenever i write something like

del /s "Filename.?"

or

del /s "filename.*" 

It erases everything with that filename.

Any help would be greatly appreciated.

Upvotes: 3

Views: 1478

Answers (3)

user1795784
user1795784

Reputation:

Wrote a little python script for fun.

import os

for f in os.listdir('.'):
    if os.path.isfile(f):
        if '.' in f:
            os.remove(f)

Just pop open a python shell and run this, works like a charm.

Upvotes: 2

Bart
Bart

Reputation: 2060

Assuming Windows, put this in a file named something like "exclude_delete.bat":

@echo off & setlocal

if "%1"=="" goto :syntax

set wildcard=%1

for %%f in (%wildcard%) do (
  if not "%%~xf"=="" (
    echo Deleting: %%f
    del "%%f"
  ) else (
    echo Keeping: %%f
  )
)

goto :eof

:syntax
echo. Syntax: %0 ^<wildcard^>
echo.
echo. Example: to delete every file "Filename.*" except "Filename":
echo.   %0 Filename.*

Upvotes: 2

Blorgbeard
Blorgbeard

Reputation: 103565

You could do it this way, if you don't mind temporarily renaming the file:

ren filename filename_keep
del filename.*
ren filename_keep filename

Upvotes: 2

Related Questions