Solved Batch file: Preg match possible?

September 14, 2011 at 05:12:58
Specs: Windows 7
Hi there,

I am looking at this for hours now but i can't figure it out. The following:
I have text files in a directorie named:
234342422_prob_23424242.txt
2fdkjsafjsproben23424242.txt
p2342242prob2fdsfkjsljfa.txt

I try to figure out how to recognise the txt files with 'prob' in the name.
I have the following what doesnt work:

SETLOCAL ENABLEDELAYEDEXPANSION
set findthis=prob
for %%f in (*.txt) do (
echo %%f
if /i %%f=="*%findthis%*" echo good
)

I tried also the following but no results:
for %%f in (*.txt) do (
set write=%%f
SET _prefix=%write:~0,4%
IF %_prefix%=="prob" goto this
)

Any help is appreciated.


See More: Batch file: Preg match possible?

Report •


✔ Best Answer
September 15, 2011 at 12:11:51
findstr has a /v switch that will print only lines that DONT contain a match. So you could do a second findstr on filenames.txt, and have that output. This isn't tested but something like this might do it.

Dir /b *pro*.txt > filenames.txt
findstr /v "probendaten.txt" filenames.txt > filenames.txt
findstr /i /c:"pro" filenames.txt >2
if %errorlevel% EQU 0 (goto SENDEMAIL) else (goto DONOTHING)



#1
September 14, 2011 at 06:35:22
Hi mauricio1980,

You'll need to echo the contents of that directory into a text file and then use findstr to find the prob string in that file. Then you can use that for loop to find that string, and then echo each matching line into another text file. Something like this:

@echo off

dir /b c:\directory_name > c:\filenames.txt

for /F "tokens=*" %%A in (c:\filenames.txt) do (
findstr "prob" c:\filenames.txt
echo %%A >> matching_filenames.txt
)


Report •

#2
September 14, 2011 at 07:34:55
Thanks for your help jowah, but in your example the file matching_filenames.txt is not filled with the 'prob' text files. It's filled with all the files in that directory.

As follow-up, is it possible to check if a text file is empty or not. Because on the end i want to receive an email (already configured) when a txt file with 'prob' in it.


Report •

#3
September 14, 2011 at 09:45:09
Oops your right maruicio1980.

@echo off

dir /b c:\directory_name > c:\filenames.txt

for /F "tokens=*" %%A in (c:\filenames.txt) do (
findstr "prob" c:\filenames.txt >> matching_filenames.txt
)


Report •

Related Solutions

#4
September 14, 2011 at 09:48:08
You could use filecompare (FC) to check for empty file. You could create an empty file and then use FC to compare, then delete the empty file you created.

Report •

#5
September 14, 2011 at 11:45:31
Dir *prob*.txt

does not work?


Report •

#6
September 14, 2011 at 13:29:05
Wow ace you just hit me with the stupid stick. dir *prob* does work, perfectly. lol, sometimes I over-complicate things.

Report •

#7
September 15, 2011 at 00:58:11
Thanks for the help guys. I have almost figured it out with the code you guys posted.
I have the following what almost works:

Dir /b *pro*.txt > filenames.txt
findstr /i /c:"pro" filenames.txt >2
if %errorlevel% EQU 0 (goto SENDEMAIL) else (goto DONOTHING)

This works! But, when the txt file is called probendaten.txt then this is a good one and i don't have to be emailed. So if probendaten then don't put it in filesnames.txt, is this possible? Or an other way?
So is it possible to exclude probendaten in above code?

Thanks again.


Report •

#8
September 15, 2011 at 12:11:51
✔ Best Answer
findstr has a /v switch that will print only lines that DONT contain a match. So you could do a second findstr on filenames.txt, and have that output. This isn't tested but something like this might do it.

Dir /b *pro*.txt > filenames.txt
findstr /v "probendaten.txt" filenames.txt > filenames.txt
findstr /i /c:"pro" filenames.txt >2
if %errorlevel% EQU 0 (goto SENDEMAIL) else (goto DONOTHING)


Report •

#9
September 16, 2011 at 02:54:40
Great Jowah, that /v switch does do the trick.
It works fine now.

Thanks alot for your help.


Report •


Ask Question