4

It is easy to delete files with certain extension in windows command line:

del *.jpg

I need a command that i could delete all files that DO NOT START with, somthing like this:

del !foo*.jpg

3 Answers 3

8

The Delete command doesn't have this flexibility, but you can use Remove-Item in Powershell

C:\PS>remove-item * -include *.dll -exclude *cal*

Description:

This command deletes from the current directory all files with a .dll file name extension and a name that does not include "cal". It uses the wildcard character (*) to specify the contents of the current directory. It uses the Include and Exclude parameters to specify the files to delete.

1
  • 2
    del is actually a alias for remove-item in PowerShell. so you could use del instead of remove-item.
    – SimonS
    Nov 8, 2016 at 10:22
1

This powershell script will recursively delete the small backup files created by macOS, starting with ._

$curDir = Split-Path -Parent $MyInvocation.MyCommand.Path
foreach ($file in Get-ChildItem -force $curDir -Recurse)
{
if (($file.Extension -match '.DS_Store') -or ($file -like '._*'))
    {
    Remove-Item $file.FullName -Force | Out-Null
    }
}
0

Turns out that with matth's you can use regex, like this:

$curDir = Split-Path -Parent $MyInvocation.MyCommand.Path
foreach ($file in Get-ChildItem -force $curDir -Recurse) {
  if ($file -match '^(?!foo).*?\.jpg') {
    Remove-Item $file.FullName -Force | Out-Null
  }
}

The operator -match works regexp patterns. Check the docs for more matching operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.