Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
4  
    
Does Windows support regexes? On Linux you could've done *.txt$ –  Gx1sptDTDa Jul 18 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 at 14:33
1  
@Gx1sptDTDa: You can turn it off system-wide, but it is still the default. –  MSalters Jul 18 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 at 19:03

5 Answers 5

up vote 4 down vote accepted

Maybe you could use the forfiles command :

forfiles /p "c:\test" /m "*.txt" /c "cmd /c del @file"
share|improve this answer
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 at 16:47
    
Yes, this solution works perfectly. Thank you :) –  Artur Iwan Jul 23 at 12:03

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.

share|improve this answer
    
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 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 at 12:14

Fast solution, but not directly answer your question:

rename *.txt_* *.nodelete
del *.txt
rename *.nodelete *.txt_bakcup
share|improve this answer
    
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 at 8:40

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

share|improve this answer
    
If I could use PowerShell, I would't ask that question :) –  Artur Iwan Jul 18 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 at 12:41

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:

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

share|improve this answer
    
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 at 12:57
    
the answer updated –  TBI Infotech Jul 18 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 at 13:39
    
he specify the suffix after extension i think @HopelessN00b as far as i can understand –  TBI Infotech Jul 18 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 at 13:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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