Jump to content
am632

Read from text file and set variable

Recommended Posts

am632

Hi,

I have an example .txt file which contains the following string of characters "There are 5 bottles of milk" (without quotes) in my script dir

so I have the following code to open/read the file

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$File = FileOpen(@ScriptDir & "\testfile.txt")
$FileRead = FileRead($File)
$hFileRead = $FileRead

I'd like to set the number 5 as a variable to be displayed in a MsgBox - My question is how would I do this, knowing that the number '5' in the .txt file could change outside of autoit?

I was looking at the _FindInFile UDF found on this forum here autoitscript.com/forum/topic/132159-findinfile-search-for-a-string-within-files-located-in-a-specific-directory/ as I thought it may be a step in the right direction but I couldn't see how I'd do what I need to do.

 

Please can anyone help?

Thanks

am632

Share this post


Link to post
Share on other sites
JLogan3o13

@am632 There are a couple of ways to go about this (I am operating under the assumption that you simply want to know how many bottles of milk you have). A couple of questions first:

  • Is the information you seek always going to be on the same line?
  • Is the line you're after unique - are there no other lines that say "there are x bottles of milk"?

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Earthshine

5 bottles of milk on the wall, 5 bottles of milk. Take one down and pass it around, 4 more bottles of milk on the wall.

that would be my test file. lol sorry.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
am632
1 hour ago, JLogan3o13 said:

@am632 There are a couple of ways to go about this (I am operating under the assumption that you simply want to know how many bottles of milk you have). A couple of questions first:

  • Is the information you seek always going to be on the same line?
  • Is the line you're after unique - are there no other lines that say "there are x bottles of milk"?

Hi,

Thank you for your reply.

You are correct in saying I want to find how many bottles of milk there are.

To answer your questions the information is likely to always be on the same line but in less common cases it may not but the line is unique.

I am ultimately planning on working with log files which are stored as .txt files. This is why I say there may be a possibility the line may change but the line will definite always be unique. By using the example I provided, I'll be able to adapt the solution to fit in with my script.

Many thanks for your help

am632

Share this post


Link to post
Share on other sites
Subz

Hopefully I read this right, couple of examples to get the number 5 from the file.

#include <Array.au3>
#include <File.au3>
#include <String.au3>

Global $sFileName = @ScriptDir & "\testfile.txt"

Example1()
Example2()

Func Example1()
    Local $sFileRead = FileRead($sFileName)
    Local $aFileRead = _StringBetween($sFileRead, "There are ", " bottles of milk")
        If @error Then Exit MsgBox(16, "Error", "String not found")
    $iVar = Number($aFileRead[0])
    MsgBox(0, "Bottles Found", "Example1: " & $iVar)
EndFunc

Func Example2()
    Local $aFileRead, $aFileLine
    _FileReadToArray($sFileName, $aFileRead)
    If @error Then Exit MsgBox(16, "Error", "Unable to read: " & $sFileName)
    For $i = 0 To $aFileRead[0]
        $aFileLine = _StringBetween($aFileRead[$i], "There are ", " bottles of milk")
        If Not @error Then ExitLoop
    Next
    $iVar = Number($aFileLine[0])
    MsgBox(0, "Bottles Found", "Example2: " & $iVar)
EndFunc

 

Share this post


Link to post
Share on other sites
am632

Hi Subz,

Thanks for your help with this, I think example 1 should do the trick perfectly for my script.

Just 1 quick question though, I see in your example you don't use FileOpen() - I thought this was required before you could use FileRead() but obviously I was wrong. What is the advantage of using $var = FileOpen() -> FileRead($var) over not using FileOpen() at all? or does the FileOpen Serve another purpose all together?

Thanks

am632

Share this post


Link to post
Share on other sites
kaz

A lots of informations are in help :

..If a filename is given rather than a file handle - the file will be opened and closed during the function call ..

  • Like 1

Share this post


Link to post
Share on other sites
am632

Hi Kaz,

Thanks for the information

am632

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×