Link to home
Start Free TrialLog in
Avatar of foxdesignz
foxdesignz

asked on

Extracting Information

I want a batch file to be able to extract information from a text file, that the batch file earlier appended to.
Avatar of LRHGuy
LRHGuy

How about an example of what you might be trying to do?

There's several ways to get info out of a file:

type log.txt

for one.

Find "name" < log.txt

is another..
Avatar of foxdesignz

ASKER

I want to find data saved to that file and resave it as a variable, or use it directly to the batch file using the  
echo command.
Ex. echo "information i saved to the text file"
or
Ex set name="information I saved to the text file"
You might try something like this...works in Win XP:


for /f "tokens=1-2 delims= " %%a in ('type log.txt') do call :PROCESS %%~a %%~b
goto done

:process
echo From file: %~1 %~2

:done
The :process line could do most anything:

:process
set fname=%~1

for another example...
Not sure what your telling me. Maybe this might help you. This is what my batch file is looking like.

set /p fname=Type you name:
echo %fname% -- %time% >> log.txt
cls
find %fname% < log.txt ("how do i set a true function after this?, for example: if it can find it goto true
AND set the information found from the text file into a variable.")
Try something like:

This will find the name in the log, and output the line to a single file:

find "%fname%" < log.txt > tmp.txt

This will use tmp.txt as input to set FOUND to the name, or blank if not found:

set FOUND=
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :PROCESS %%~a
goto done

:process
set FOUND=%~1
echo %~1

:done

I hope that works for you...you only gave me a B on the other question..I'm curious as to why you think it didn't deserve an A.
Ok, i tried this and when i run the batch file it clears and screen and i can't do anything, its just blank, command prompt doesn't even come up. I sort of understand what this is doing, and figured that if i give you the entire batch file it might make things easyier. Check is out. ( I set it up so that the name will be there)

@echo off
cls
:begin
set /p fname=Type your name:
echo %fname% --- %time% --- %date% >> log.txt
cls
find "%fname%" < log.txt > tmp.txt
set FOUND=
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :PROCESS %%~a
goto done

:process
set FOUND=%~1
echo %~1

:done
set fname=
cls
Don't take me giving you a B grade personally, I'm new at this site, and just bearly know what I'm doing. If you can figure this out, i'll repay you with some points and a A grade. :)
400 points
A couple of changes should fix it: see the ***** below

It seems to work here, of course the screen goes blank as soon as I hit enter because of the CLS in the :DONE section. The FOUND variable is listed in the SET list though...

@echo off
cls
:begin
set /p fname=Type your name:
echo %fname% --- %time% --- %date% >> log.txt
cls
find "%fname%" < log.txt > tmp.txt
set FOUND=
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :PROCESS %%~a
goto done

:process
set FOUND=%~1
echo %~1
goto EOF                 ******

:done
set fname=
cls

:EOF       ******
This is still not what i'm looking for. I deleted the cls under the done label, and added the changes you submitted. If the find command can find the data in the text file i want it to submit a message telling me that %fname% was found. I created this in the mean time, maybe it could be brushed up.

@echo off
cls
set /p fname=Type your name:
echo %fname% > log.txt
cls
if exist %fname% < log.txt  goto found
echo Sorry %fname% was not found in the log file
pause
goto exit
:found
echo Welcome %fname%
pause
goto exit
:exit
set fname=
cls
ASKER CERTIFIED SOLUTION
Avatar of LRHGuy
LRHGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, i'm getting tired of this. Heres your points and your A, but if you would send me an email explaining what each step of this is:
find "%fname%" < log.txt > tmp.txt
set INLOG=
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :processfind %%~a
if %INLOG%a==a goto NOTFND

I have no clue what delims, or %%a is, or what it is doing. Thanks for the help though, regardless.
My email is : webmaster@foxdesignz.com
or should i rather say, i dont understand ANY of it. My newbie presence probably disgusts you :) sorry.
Is anything working for you or are you getting errors? This batch commands being used, some only work in WIN XP.


@ECHO OFF
rem Prompts for a name, then looks to see if it's listed in a log file
rem if not, adds it, otherwise, says "Hello!"

rem Prompt user
set /P fname=Your Name:

rem Check for entry in log file
rem FIND looks for a string, but uses the redirected input, i.e. reads from "< filename"
rem the output is redirected to another file "> tmp.txt" instead of to the console
find "%fname%" < log.txt > tmp.txt
rem Make sure INLOG variable is blank
set INLOG=
rem FOR /F looks at each line in the input, with tokens=X being the number of "words" it sees on each line.
rem delims specifies how to determine the end of word (i.e. space separates words)
rem since tmp.txt only has %fname% in it, if found above, we simple calls processfind once with the firt token on the line (i.e. the name)
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :processfind %%~a
rem if INLOG is blank, goto NOTFND section
if %INLOG%a==a goto NOTFND
echo Welcome %fname%!
pause
goto exit

rem Set INLOG variable to the parameter from the file, specified in the FOR line above
:processfind
set INLOG=%~1
rem Must go to end of batch file to "return" from the call
goto EOF

:NOTFND
echo Sorry, %fname% not in the log
rem Add it to log like so:
echo %fName% --- %time% --- %date% >> log.txt

:exit
set fname=
rem cls

:EOF


I hope that helps!
i copyied and paste the code above and ran it for results this is what happenend. It prompted me for the name "Your Name" i enter a name pressed enter and nothing came up, it just returned to the command prompt. My command screen looks exactly like this: (ps-i called the batch file "try.bat")

G:\>try
Your Name:foxdesignz

G:\>

Thats what happened. I have XP Pro.
Here it is again, I made one slight change to detect that there is not a log.txt file.

I would suggest deleting any log.txt and tmp.txt files, then try it. Mine works great, pausing and everything!
You might comment out or delete the ECHO OFF line so you can watch what is doing for debugging...

@ECHO OFF
rem Prompts for a name, then looks to see if it's listed in a log file
rem if not, adds it, otherwise, says "Hello!"

rem Prompt user
set /P fname=Your Name:

rem If no log file, we know it's not there...
if not exist log.txt goto NOTFND

rem Check for entry in log file
rem FIND looks for a string, but uses the redirected input, i.e. reads from "< filename"
rem the output is redirected to another file "> tmp.txt" instead of to the console
find "%fname%" < log.txt > tmp.txt
rem Make sure INLOG variable is blank
set INLOG=
rem FOR /F looks at each line in the input, with tokens=X being the number of "words" it sees on each line.
rem delims specifies how to determine the end of word (i.e. space separates words)
rem since tmp.txt only has %fname% in it, if found above, we simple calls processfind once with the firt token on the line (i.e. the name)
for /f "tokens=1 delims= " %%a in ('type tmp.txt') do call :processfind %%~a
rem if INLOG is blank, goto NOTFND section
if %INLOG%a==a goto NOTFND

echo Welcome %fname%!
pause
goto exit

rem Set INLOG variable to the parameter from the file, specified in the FOR line above
:processfind
set INLOG=%~1
rem Must go to end of batch file to "return" from the call
goto EOF

:NOTFND
echo Sorry, %fname% not in the log
rem Add it to log like so:
echo %fName% --- %time% --- %date% >> log.txt

:exit
set fname=
rem cls

:EOF

Finally got it to work. This is what I changed.
changed this:
find "%fname%" < log.txt > tmp.txt
to:
findstr "%fname%" log.txt > tmp.txt
Now it works fine, every single time. For some reason the batch file would hang whenever the FIND command was in use. So i changed it to FINDSTR. No problems yet. I would rather use FINDSTR too, just because of the wider range of options with it. Thanks for the help man.