Jump to content
Sign in to follow this  
ahmed9100

read a some words from a file

Recommended Posts

ahmed9100

my unfinished code is 

#include <File.au3>
Local $aRecords
                If Not _FileReadToArray(@ScriptDir&"\profile.rsc", $aRecords) Then
                    MsgBox(4096, "Error", "Error file not found" & @error)
                else
                $line = "None"
                For $x = 1 To $aRecords[0]

                    $result = StringInStr($aRecords[$x], "name")
                    if $result = "0" Then
                    Else
                        MsgBox("","",$aRecords[$x])
                        $aArray1 = _StringBetween($aRecords[$x], 'name=', ' ')
                        $line = $line &"|"& $aArray1[0]
                    EndIf
                Next
            EndIf
;~ next save $line to setting file to put it in combo

in the file i need to read :

set [ find default=yes ] address-pool=dhcp_pool1 idle-timeout=10m name=
    default rate-limit=80K/800K shared-users=2 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m keepalive-timeout=none name=30
    rate-limit=64K/240K shared-users=1 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=50 rate-limit=64K/400K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=100 rate-limit=64K/800K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
------------------
word default and 30 and 50 and 100 only those words

profile.rar

Edited by ahmed9100

Share this post


Link to post
Share on other sites
DW1

I'm no regex pro, but I would do it something like this:

#include <array.au3>
$sFileContents = FileRead(@ScriptDir & '\profile.rsc')

Local $aArray, $aInt, $sString
$aArray = StringRegExp($sFileContents, '\\[[:space:]]+(\S+.*?) rate', 3)
If IsArray($aArray) Then $sString = $aArray[0]
$aInt = StringRegExp($sFileContents, 'name=(.*?) ', 3)
_ArrayDisplay($aInt, $sString)
Edited by danwilli

Share this post


Link to post
Share on other sites
ahmed9100

it work great but what if i need more

like the file may content 2 or 3 or other number of string i need to get ?

Share this post


Link to post
Share on other sites
DW1

it work great but what if i need more

like the file may content 2 or 3 or other number of string i need to get ?

I went with what you gave me.  Provide an example of a file with multiple strings to gather.

Share this post


Link to post
Share on other sites
ahmed9100
example : 1
set [ find default=yes ] address-pool=dhcp_pool1 idle-timeout=10m name=
    default rate-limit=80K/800K shared-users=2 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m keepalive-timeout=none name=normal 
    rate-limit=64K/240K shared-users=1 status-autorefresh=1m
add address-pool=dhcp_pool1 idle-timeout=10m keepalive-timeout=none name=30 
    rate-limit=64K/240K shared-users=1 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=50 rate-limit=64K/400K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=100 rate-limit=64K/800K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
example : 2
set [ find default=yes ] address-pool=dhcp_pool1 idle-timeout=10m name=
    default rate-limit=80K/800K shared-users=2 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m keepalive-timeout=none name=30 
    rate-limit=64K/240K shared-users=1 status-autorefresh=1m
    transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=50 rate-limit=64K/400K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=100 rate-limit=64K/800K
    shared-users=1 status-autorefresh=1m transparent-proxy=no
add address-pool=dhcp_pool1 idle-timeout=10m name=400rate-limit=64K/800K
    shared-users=1 status-autorefresh=1m transparent-proxy=no

 

i import this file from a server but it may have a multiple value or will be one the default i mean not exact number of profile

your 2 StringRegExp solve this problem but i need it in one array and how to know array value count  to read it in a loop to save if like

default|30|50|100 to put it in a combobox

sorry cuz im a beginner 

Share this post


Link to post
Share on other sites
DW1

I'm not 100% sure I know what you are asking for, but perhaps this can get you started.

Again, I'm no regex pro, so there may be way better ways to do this.

#include <array.au3>
$sFileContents = FileRead(@ScriptDir & '\profile.rsc')

Local $aData
$aData = StringRegExp($sFileContents, '(?m)(?:^[[:space:]]+|.*?name=)(\S+) (?:rate|.{0,2}$)', 3)
_ArrayDisplay($aData)

Share this post


Link to post
Share on other sites
DW1

Or perhaps?

#include <array.au3>
$sFileContents = FileRead(@ScriptDir & '\profile.rsc')

Local $aData, $sString
$aData = StringRegExp($sFileContents, '(?m)(?:^[[:space:]]+|.*?name=)(\S+) (?:rate|.{0,2}$)', 3)
$sString = _ArrayToString($aData)
$sString = StringRegExpReplace($sString, '\|([a-z,A-Z])', '?$1')
$aData = StringSplit($sString, '?')
_ArrayDisplay($aData)
Edited by danwilli

Share this post


Link to post
Share on other sites
ahmed9100

 Increasingly balanced you are a genies  thx bro  :thumbsup:

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
Sign in to follow this  

×