Jonas
Jonas

Reputation: 1195

Deleting files with "certain" words in them

so I have a folder full of about 300 .txt files, All named like below

ERROR WITH Position Post_20162602_052055.txt
ERROR WITH Position Post_20162602_052053.txt
ERROR WITH Position Post_20162602_052102.txt
INCOMPLETE MESSAGE_20162602_052102.txt
EXCEPTION ERROR_20162602_052102.txt

I would like to create a batch file to delete only the INCOMPLETE MESSAGE_20162602_052102.txt and the EXCEPTION ERROR_20162602_052102.txt errors in the folder. All the files are unique, The only common factor between them are Position Post, INCOMPLETE MESSAGE and EXCEPTION ERROR

I have tried using this code below however it seems to have an issue with the spaces in the file names and I'm pretty sure its looking in the file instead of at the file name.

@echo off
FOR /F"delims=;" %%a in ('findstr /m /i "EXCEPTION ERROR INCOMPLETE MESSAGE" C:\test\*.txt') do (
@echo %%a
del %%a
)

I took a look at the FIND function for bash but I could not find a way to set the a "search variable" for specific words in a file name.

Upvotes: 2

Views: 75

Answers (2)

Hackoo
Hackoo

Reputation: 18827

Try like this :

@echo off
setlocal EnableDelayedExpansion
Set Folder=c:\Dataexports
set Log=FileList.txt
If Exist %Log% Del %Log%
Set Exp="EXCEPTION ERROR" "INCOMPLETE MESSAGE"
(
    For %%a in (%Exp%) Do (
        For /f "delims=" %%b in ('Dir /s /b "%Folder%\*.txt" ^| find %%a /i') do ( set File=%%b
            echo "!File!" & Del "!File!"
        )
    )   
)>>%Log% 2>&1
Start "" %Log%

Upvotes: 2

user5721973
user5721973

Reputation:

del "INCOMPLETE MESSAGE*.txt" "EXCEPTION ERROR*.txt" is how to.

Upvotes: 1

Related Questions