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.

Nico
  • 153
  • 1
  • 5
  • 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 05 '11 at 14:53
  • i can't search for <>*.sql or !*.sql in windows search ? – Nico Jul 05 '11 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 06 '11 at 00:24
  • 1
    I know this is way old, but this question http://superuser.com/questions/328223/find-all-files-that-are-not-of-a-specific-type-extension-in-folder 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 '15 at 20:06

3 Answers3

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
shf301
  • 7,910
  • 1
  • 28
  • 25
  • You don't need regular expressions for this. After the pipe, `findstr /v "*.sql"` should work just fine. Thanks, though! – Iain Samuel McLean Elder May 04 '12 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 '13 at 03: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 '16 at 22:57
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
Lawrence
  • 41
  • 3
  • 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 07 '23 at 00: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 '23 at 16:19
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.

jjj
  • 180
  • 1
  • 4
  • 2
    File extensions usually have 3 characters not 1. 26^3 = **17576** different combinations.......... – Pacerier Aug 27 '14 at 13:24