Hi guys,
I need to run a batch file that:
Looks inside a file for the existence of specified txt... and then set a variable to the value of the characters next to that txt.
God if I struggle to explain it in English no wonder I can't do it in DOS!
So I managed to hack this together (by trial and error really) to put the filename in to a variable (the file will always start with EJ but could have anything to follow that)
FOR /F "tokens=1 delims=" %%A in ('dir EJ* /b') do (
SET ejfilename=%%A
echo %ejfilename%
)
This works and everyone in the village was amazed.
But checking inside that file is killing me...
The thing I am searching for is "MC". Inside this file is a machine ID of the form MC00014 - so I need to look for "MC" and add to a variable the 5 numbers following it.
When I cut and paste the data from the EJ file in to new file "findme.txt" I can run this on the command line: findstr "MC00" findme.txt and it returns the line that contains the MC00015.
But when I run that on the EJ file it fails to find anything. Perhaps this is the unicode limitation of findstr?
So I thought of running: FIND "MC00" ejdata
But that doesn't find anything either.
Why didn't I pay more attention in school? Why am I attempting this with a massive hangover?
Anyway... so if anyone could point me in the right direction I would be very grateful.
tia
GP.
Find text within file
Moderator: DosItHelp
Re: Find text within file
Can you provide the text file or a sample of it, maybe there is another simple way to do that.
Re: Find text within file
Try this and see what it spits out.
Code: Select all
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir ej* /a:-d /o:n /b') do (
for /f "delims=" %%b in ('findstr /r /i "mc[0-9][0-9][0-9][0-9][0-9]" "%%a" ') do (
set var=%%b
echo %%a - "!var!"
)
)
pause
-
- Posts: 3
- Joined: 13 Jul 2012 04:38
Re: Find text within file
You'll be pleased to know that the hangover is gone and the world is a much more cheery place. And you're attempt to fix this foxi has really cheered me up 
I've continued to play on the command line with find and findstr and I think the problem has something to do with the format the data file is in. I've noticed in notepad++ that the file is reported in Macintosh format.
For initial testing I cut and pasted the first 50 or so lines in to a testfile.txt and when I do:
FIND "MC00"" testfile.txt
it works
and when i do
FINDSTR "MC00" testfile.txt it also works.
But when I try and find in the original file (both methods) it doesn't work.
Your suggestion just stopped at the pause with no output. (I've add some echo's to your code):
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir ej* /a:-d /o:n /b') do (
echo %%a
for /f "delims=" %%b in ('findstr /r /i "MC[0-9][0-9][0-9][0-9][0-9]" "%%a" ') do (
echo %%b
set var=%%b
echo %%a - "!var!"
)
)
pause
So the first echo successfully echo's the filename. Hooray.
But the 2nd echo doesn't echo anything. Boo.
...
Ok so a dump of the actual text file will be very difficult. But I will dump an example of the line I am looking for within the file. I'll also dump one of the lines that has a weird 'esc' character in it... and a combination of these two should be enough for you to understand why I can't dump the entire file
Then I shall stop using the word dump so much.
Thanks for your efforts thus far.
GP
Line I'm looking for (that has the MC code):
01/07/12 03:29 MC00016
Example of other line:
[020t 03:29:10 PIN ENTERED
...hmm I can't paste the offending line in to this box.. the weird ESC character won't paste. It has a black background with a white font.
Am I missing the fundamental point here?

I've continued to play on the command line with find and findstr and I think the problem has something to do with the format the data file is in. I've noticed in notepad++ that the file is reported in Macintosh format.
For initial testing I cut and pasted the first 50 or so lines in to a testfile.txt and when I do:
FIND "MC00"" testfile.txt
it works
and when i do
FINDSTR "MC00" testfile.txt it also works.
But when I try and find in the original file (both methods) it doesn't work.
Your suggestion just stopped at the pause with no output. (I've add some echo's to your code):
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir ej* /a:-d /o:n /b') do (
echo %%a
for /f "delims=" %%b in ('findstr /r /i "MC[0-9][0-9][0-9][0-9][0-9]" "%%a" ') do (
echo %%b
set var=%%b
echo %%a - "!var!"
)
)
pause
So the first echo successfully echo's the filename. Hooray.
But the 2nd echo doesn't echo anything. Boo.
...
Ok so a dump of the actual text file will be very difficult. But I will dump an example of the line I am looking for within the file. I'll also dump one of the lines that has a weird 'esc' character in it... and a combination of these two should be enough for you to understand why I can't dump the entire file

Thanks for your efforts thus far.
GP
Line I'm looking for (that has the MC code):
01/07/12 03:29 MC00016
Example of other line:
[020t 03:29:10 PIN ENTERED
...hmm I can't paste the offending line in to this box.. the weird ESC character won't paste. It has a black background with a white font.
Am I missing the fundamental point here?
Re: Find text within file
Maybe the file is a binary file or unicode.
Try this to see if it changes:
Try this to see if it changes:
Code: Select all
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir ej* /a:-d /o:n /b') do (
for /f "delims=" %%b in ('type "%%a"^|findstr /r /i "mc[0-9][0-9][0-9][0-9][0-9]" ') do (
set var=%%b
echo %%a - "!var!"
)
)
pause
-
- Posts: 3
- Joined: 13 Jul 2012 04:38
Re: Find text within file
I get a page full of:
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
I'll look this one up when I get home.
Thanks so much for your thoughts.
GP
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
FINDSTR: Line 1 is too long.
I'll look this one up when I get home.
Thanks so much for your thoughts.
GP
Re: Find text within file
Ok, try this.
The nature of the file is still unknown, but find.exe can process longer lines than findstr
The nature of the file is still unknown, but find.exe can process longer lines than findstr
Code: Select all
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir ej* /a:-d /o:n /b') do (
for /f "delims=" %%b in ('type "%%a"^|find " MC" ') do (
set var=%%b
echo %%a - "!var!"
)
)
pause