Jump to content

read a some words from a file


ahmed9100
 Share

Go to solution Solved by DW1,

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

  • Solution

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...