Solved

Extracting Information

Posted on 2004-10-07
18
311 Views
Last Modified: 2010-04-17
I want a batch file to be able to extract information from a text file, that the batch file earlier appended to.
0
Comment
Question by:foxdesignz
  • 10
  • 8
18 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12254422
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..
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254442
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"
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12254490
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:LRHGuy
ID: 12254495
The :process line could do most anything:

:process
set fname=%~1

for another example...
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254532
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.")
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12254595
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.
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254735
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
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254748
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. :)
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254757
400 points
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12254796
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       ******
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12254910
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
0
 
LVL 7

Accepted Solution

by:
LRHGuy earned 500 total points
ID: 12255026
Here's mine:


@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
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
echo Welcome %fname%!
pause
goto exit

:processfind
set INLOG=%~1
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
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12255128
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
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12255141
or should i rather say, i dont understand ANY of it. My newbie presence probably disgusts you :) sorry.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12255195
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!
0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12255274
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.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12255463
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

0
 
LVL 1

Author Comment

by:foxdesignz
ID: 12255926
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question