3

Imagine that we have directory c:\test with following files:

first.txt
second.doc
third.txt
fourth.txt_bak

I want to remove only txt files, but leave doc and txtwhatever untouched.

When I run command del c:\test\*.txt, it removes everything except the 2nd file.

Any ideas how to fix that to remove only txt files?

Edit: I have no idea how customer will name their files. Suffix can be anything, not only txt_something. It could be also txtsomething, txt-something, and so on...

I also cannot install any binaries on the server, I can only use the default ones.

Artur Iwan
  • 133
  • 5
  • 4
    [How to prevent command line wildcard processor from evaluating short file names](http://superuser.com/questions/179479/how-to-prevent-command-line-wildcard-processor-from-evaluating-short-file-names) on Superuser might be worth reading. – Damien_The_Unbeliever Jul 18 '14 at 13:56
  • Does Windows support regexes? On Linux you could've done `*.txt$` – Gx1sptDTDa Jul 18 '14 at 14:22
  • 1
    @Gx1sptDTDa: That's not the problem, the 4th file is **also** named `fourth.txt` under 8.3 naming. And that absolutely matches `*.txt`. – MSalters Jul 18 '14 at 14:33
  • @MSalters I thought 8.3 naming is optional, and no longer the default? – Gx1sptDTDa Jul 18 '14 at 14:40
  • 1
    @Gx1sptDTDa: You can turn it off system-wide, but it is still the default. – MSalters Jul 18 '14 at 14:41
  • 1
    The more I think about this, the worse it seems - if you cannot trust how users are naming files within this directory, how do you not know that they've decided to actually use the `.txt` extension for some files that they want to keep? – Damien_The_Unbeliever Jul 18 '14 at 19:03

5 Answers5

4

Maybe you could use the forfiles command :

forfiles /p "c:\test" /m "*.txt" /c "cmd /c del @file"
krisFR
  • 13,280
  • 4
  • 36
  • 42
  • 1
    I like this one best. It is what I'd do on linux with the find command and exec. Very clever and clean way to operate. :D – Alex Rouge Jul 18 '14 at 16:47
1

Based on fquinto's idea, but more robust:

mkdir FilesToSave.AEEC869E-A480-4459-84AE-B20D47A967BC
move *.txt?* FilesToSave.AEEC869E-A480-4459-84AE-B20D47A967BC
del *.txt
move FilesToSave.AEEC869E-A480-4459-84AE-B20D47A967BC\*.* .
rmdir FilesToSave.AEEC869E-A480-4459-84AE-B20D47A967BC

The *.txt?* wildcard moves all files with an extension of more than 3 characters starting with *.txt to a temporary directory. These are the files whose 8.3 names end in .txt. So, in line 3 you only delete files whose true extension is .txt, and in line 4 you restore the files which would have been accidentily deleted.

The GUID is there to make the temp dir unique.

MSalters
  • 700
  • 5
  • 6
  • Actually this does not work either :(¨ Maybe it's some Windows bug, but filter `*.txt?*` matches also files ending with pure `.txt` :( – Artur Iwan Jul 23 '14 at 12:07
  • Ow, turns out the MSDN doc is incorrect when it states that ? matches a single character. It also matches zero characters. – MSalters Jul 23 '14 at 12:14
0

Fast solution, but not directly answer your question:

rename *.txt_* *.nodelete
del *.txt
rename *.nodelete *.txt_bakcup
fquinto
  • 101
  • 2
  • This is nice solution and I thought about something similar. But unfortunately I have no idea how customer will name his files. It could use any suffix... `file.txt_something`, `file.txtsomething`, `file.txt-something`. So your solution won't help :-( – Artur Iwan Jul 18 '14 at 08:40
0

I know I'm not answering the question in the way you want but... ... if you can use PowerShell, the cmdlet Remove-Item does what you need.

Remove-Item *.txt

Greetings

Alex Rouge

Alex Rouge
  • 104
  • 4
  • If I could use PowerShell, I would't ask that question :) – Artur Iwan Jul 18 '14 at 10:21
  • That's what I was afraid of. That's also why I mention I'm not really answering your question. However, my answer remains an answer for other facing similar problems than yours. What is the context or the historical reason that prevents you from using PowerShell? – Alex Rouge Jul 18 '14 at 12:41
-1

If i am getting your problem right than these might be helpful:

@echo off
setlocal EnableDelayedExpansion
set exclude=.log.sdb.sdk.bat.
for %%f in (*.*) do (
   if /I "%exclude%" == "!exclude:%%~Xf.=!" del "%%f"
)

OR

@echo off
forfiles /c "cmd /c if @isdir equ FALSE if /i not @ext==\"sdb\" if /i not @ext==\"sbk\" if /i not @ext==\"log\" if /i not @ext==\"bat\" del @file"

For reference use:

https://stackoverflow.com/questions/15401813/how-to-delete-all-files-but-excluding-specific-files-batch-file

TBI Infotech
  • 1,584
  • 10
  • 17
  • And what makes you think the recursive/subdirectory switch (`/S Delete specified files from all subdirectories.`) is going to change the behavior? – HopelessN00b Jul 18 '14 at 12:57
  • the answer updated – TBI Infotech Jul 18 '14 at 13:23
  • You're still not reading the question correctly. Using `del *.txt` deletes `fourth.txt_bak`. The problem he's trying to solve is not deleting a file with arbitrary text after the `.txt` extension, and none of your answers so far have solved that problem. – HopelessN00b Jul 18 '14 at 13:39
  • he specify the suffix after extension i think @HopelessN00b as far as i can understand – TBI Infotech Jul 18 '14 at 13:42
  • Well, you understand wrong, and he was quite clear about the problem... which I've also explained to you. – HopelessN00b Jul 18 '14 at 13:58