1

I am trying to delete all files in a folder EXCEPT those whose filename contains the string ntdll.

Here's what I have tried:

for %i in (dlls/*) do if not %%i == %%i:ntdll del %%i
for %i in (*) do if not %i == %i:ntdll del %i

I tried with findstr but I had little success.

Compo
  • 36,585
  • 5
  • 27
  • 39
Ginzo Milani
  • 418
  • 1
  • 5
  • 18
  • You tagged this question as a batch-file but your code clearly shows that you are only running it from the command prompt. – Squashman Dec 19 '18 at 20:42

1 Answers1

0

Your syntax is wrong. Here is a possible solution (you will need delayed expansion):

for %A IN (*) do @set file=%A && if !file!==!file:ntdll=! (@del /F /A !file!)

Enable delayed expansion in cmd with cmd /v:on. You are forced to use it as you are inside a code block.

This simple command searches for all files in the current working directory, assign each in a variable and checks if they have the string ntdll inside them. If not, they delete them.

For better understanding commands mentioned above, open a new cmd and type:

  • for /?
  • set /?
  • del /?

Some interesting references for further reading

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • It's good of you to change your answer following my comment, however I'd have preferred it had you responded to let me know, so that I could have deleted the comment. But whilst I'm here, I must ask, have you actually read the output from `Del /?` as you've advised the OP to do? I ask because, you are now using the `/Q` option, despite deleting individual files one by one. The `/Q` option as far as I'm aware is used when specifying a 'wildcard', you haven't used a wildcard and therefore don't need that option at all. Can I also ask why you you decided against using the `/A` option I advised? – Compo Dec 19 '18 at 20:29
  • It works in the same manner as the `Dir` command! If you use `/A` without specifying attributes, `Del` displays the names of all files, (i.e. it assumes all attributes, `/A:RHISAL`, _where the colon, **`:`**, is optional_). – Compo Dec 20 '18 at 06:31