5

I need to find and delete all files in subdirectories that are not .SQL files. I can't figure out how to search for <>.SQL or !.SQL in Windows Search.

Most of the solutions that I've seen here are to find specific extensions and do something with them. I'm looking for the opposite.

4
  • yeah it's very easy to search for a specific file, but how do you search for everything that's not a specific file.
    – Nico
    Jul 5, 2011 at 14:53
  • i can't search for <>*.sql or !*.sql in windows search ?
    – Nico
    Jul 5, 2011 at 14:53
  • I do not believe this is a function of the Windows default search. You would most likely need to write a script or install a tool like grep.
    – Mike Soule
    Jul 6, 2011 at 0:24
  • 1
    I know this is way old, but this question superuser.com/questions/328223/… has a brilliant answer. In Explorer search for "NOT *.sql" (capitalization of NOT matters) . It was new to me but I tested it and it works. You can even do "NOT *.mp3 NOT *.aiff NOT *.m4p". Even further you can go "NOT folder" to exclude folders from the results.
    – Jeff
    Jul 17, 2015 at 20:06

3 Answers 3

10

I don't know of a way to do it from Windows Search, but from the command line it is:

dir /s /b /a-d | findstr /v /r ".*\.sql"
  • dir /s - recursive directory listing
  • /b - bare listing, only file names (no directory size info etc. in the output)
  • /a-d - filter by attributes, not directory (remove sub directories from the listing)
  • | (pipe) - send the output of the directory listing to
  • findstr - text search utility
  • /v - only return lines that don't match
  • /r - use regular expressions
  • ".*.sql" - match anything any number of times followed by a dot followed by sql
3
  • You don't need regular expressions for this. After the pipe, findstr /v "*.sql" should work just fine. Thanks, though! May 4, 2012 at 23:29
  • If you want to do something with the output of this command (e.g. delete the files), you can use this inside a batch file: FOR /f "tokens=* USEBACKQ" %%a in (`dir /s /b /a-d ^| findstr /v /r ".*\.sql"`) DO del /f /q "%%a"
    – WackGet
    Aug 17, 2013 at 3:31
  • This is good, and what I came up with as well. It'd be nice to find a way to do this without using dir \b (so I could see date, size, etc) and without using a for /f loop.
    – dgo
    Mar 13, 2016 at 22:57
4

If you insist on using Windows search which doesn't have a native ability or support for such a feature, you could simply run it this way:

*.a OR *.b OR *.c OR *.d OR *.e OR *.f OR *.g OR *.h OR *.i OR *.j OR *.k OR *.l OR *.m OR *.n OR *.o OR *.p OR *.q OR *.r OR *.t OR *.u OR *.v OR *.w OR *.x OR *.y OR *.z

Following search will exclude all files with .s* extensions, while listing all others.

Perhaps there is a better way to do it, but this should work as well.

1
  • 2
    File extensions usually have 3 characters not 1. 26^3 = 17576 different combinations..........
    – Pacerier
    Aug 27, 2014 at 13:24
4

In Windows 10 (I'm on version 1803) I use the Search box in File Explorer and type

*.* kind:-jpg

to find all files which are not jpg in my pictures folders (so, videos etc). Perhaps you should try

*.* kind:-sql
2
  • 1
    This will also match folders. Your response and Jeff's comment on the original question led me to NOT *.sql NOT kind:folder which seems to work perfectly.
    – Amorphous
    Mar 7, 2023 at 0:54
  • Curious, I tried my search again today and it did indeed include folders. So I modified my search to *.* kind:-jpg kind:-folder and folders were then excluded. Thanks for the update though @Amorphous
    – Lawrence
    Mar 10, 2023 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .