Ok, the first script searches for files that contain the specified strings anywhere in the file name. Could there be more than one file in a folder that matches one of the search patterns? Say the following:
C:\123.txt
C:\Jerrys123file.txt
C:\today.123
If that is possible, would you want to delete all three of these, or just the exact match 123.txt? The script below assumes there will only be one file that matches a particular pattern in a folder. If there are more than one, it will pick the last one as the one to remove, last usually being last in alphabetical order. As the first script doesn't specify a sort order, the "last" file might not be what you expect, so in that case you'd want to specify a sort order in the first script.
2nd, found a slight bug in the first script. c:\kresults.txt will contain a list of files that match the search strings (in the example abc,123, and tree). If you have a folder that contains one of those strings, the folder will also appear in the file and there won't be an easy way to tell if the line is a folder name or a file name without an extension.
Several solutions depending on what you need. Easiest is to modify the first script so c:\kresults.txt will only contain file names. If you need to check for folder names and perhaps do some processing on folder names, best to create a separate file for just folder names.
So modify the first one to this:
Code:
@echo off
if exist C:\kresults.txt del /q C:\kresults.txt
Pushd C:\
For /F "tokens=*" %%a in ('dir *cheese* /s /b /ad') Do (
FOR %%G IN (abc 123 tree) DO (
echo Searching for %%G in %%a
echo Searching for %%G in %%a>>C:\kresults.txt
dir "%%a\*%%G*" [COLOR=Red][B]/a-d[/B][/COLOR] /L /B /S>> C:\kresults.txt
))
If not exist C:\kresults.txt echo "Files not found" >> C:\kresults.txt
popd
start notepad C:\kresults.txt
exit
This will process the first file and create two more, remove.txt with the file name you want to remove, and keepers.txt which is kresults.txt with that one file name removed.
Code:
@echo off
Set _UnwantedFilePatteren=123
Set _UnwantedInFolder=c:\
Set _SF=C:\kresults.txt
@For /F "Tokens=*" %%I In ('findstr /I /C:%_UnwantedFilePatteren% %_SF%') Do (
If /I "%%~dpI"=="%_UnwantedInFolder%" Set _UnwantedFile=%%I
)
Set _OF1=C:\removed.txt
Set _OF2=C:\keeper.txt
If EXIST %_OF1% del %_OF1%
If EXIST %_OF2% del %_OF2%
findstr /i /C:"%_UnwantedFile%" "%_SF%">>%_OF1%
findstr /i /v /C:"%_UnwantedFile%" "%_SF%">>%_OF2%
:: Now delete the file that is listed in c:\removed.txt
For /F "usebackq tokens=*" %%I in ("%_OF1%") Do Del /F "%%I"
For /F "tokens=1 delims==" %%I In ('Set _') do Set %%I=
Jerry