Jump to content

Recommended Posts

Posted

Looks like I wasn't completely awake last time. This time I believe it's going to work for you. :graduated:

#include <Array.au3>
Global $hFile, $sText, $aDate, $sDate

$hFile = FileOpen(@ScriptDir & "\report.txt", 0)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$sText = FileRead($hFile) ;remove the second param to read the entire file, insead of just the first character.

$aDate = StringRegExp($sText, "Date\h*(\H*)",1) ;set the regexp flag to one, to make it return an array of matches.
If Not IsArray($aDate) Then ;check if the array was created to avoid the hard crash
    MsgBox(0, "Error", "No match found.")
    Exit
EndIf

$sDate = $aDate[0] ;get the date from the array. (not really needed)
MsgBox(0, "date", $sDate)

FileClose($hFile)
Posted (edited)

Here is working solution,

note Func StringBetween() is wrapper function which returns string not array

#include <string.au3>

$txt = _
'Operating System                               Microsoft Windows 7 Home Premium 6.1.7600 (Win7 RTM)' & @CRLF & _
'   Date                                            2010-11-18' & @CRLF & _
'   Time                                            13:45' & @CRLF & _
'   Computer Type                                   ACPI x86-based PC'

$check = StringBetween($txt, 'Date', '\r\n')
MsgBox(0, "Date", $check)

Func StringBetween($input, $start, $end)
;~  $result = StringRegExp($input, $start & '(.*)' & $end, 3)
    $result = _StringBetween($input, $start, $end, -1, 1)
    If @error Then Return ''
    Return StringStripWS($result[0],3) ; strip leading/trailing spaces
EndFunc

EDIT:

it works fine also with this

$check = StringBetween($txt, 'Date', 'Time')
Edited by Zedna
Posted (edited)

I didn't know "\r\n" worked either. When were character escape codes added?

_StringBetween() has two modes

1) uses StringInStr

2) uses StringRegExp

when you use second mode then you must use RegExp escapes in start/end parametres

EDIT: so \r\n is related only to StringRegExp, it's not global in Autoit

Edited by Zedna
Posted

_StringBetween() has two modes

1) uses StringInStr

2) uses StringRegExp

when you use second mode then you must use RegExp escapes in start/end parametres

EDIT: so \r\n is related only to StringRegExp, it's not global in Autoit

Was that the 5th parameter that was mentioned in the help file?

2009/05/03 Script breaking change, removed 5th parameter

If so, would that "\r\n" still work?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

Was that the 5th parameter that was mentioned in the help file?

If so, would that "\r\n" still work?

I still use old 3.2.12.1 version because I have got too many scripts in syntax for this version

and because it can prodduce Win9x compatible EXE files.

I have got installed latest beta for running/testing scripts from forum only.

Yes I remember there was change in some later version in _StringBetween()

where one of these two modes was removed. I expect (but I'm not sure) StringRegExp mode was kept so this my example could work with simple removal of last parameter in call of _StringBetween()

EDIT:

here is source code for _StringBetween() from Autoit 3.2.12.1

; #FUNCTION# ====================================================================================================================
; Name...........: _StringBetween
; Description ...: Returns the string between the start search string and the end search string.
; Syntax.........: _StringBetween($sString, $sStart, $sEnd[, $vCase = -1[, $iSRE = -1]])
; Parameters ....: $sString - The string to search.
;               $sStart  - The beginning of the string to find
;               $sEnd   - The end of the string to find
;               $vCase   - Optional: Case sensitive search. Default or -1 is not Case sensitive else Case sensitive.
;               $iSRE   - Optional: Toggle for regular string manipulation or StringRegExp search. Default is regular string manipulation.
; Return values .: Success - A 0 based $array[0] contains the first found string.
;               Failure - 0
;               |@Error  - 1 = No inbetween string found.
; Author ........: SmOke_N (Thanks to Valik for helping with the new StringRegExp (?s)(?i) isssue)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _StringBetween($sString, $sStart, $sEnd, $vCase = -1, $iSRE = -1)
    If $iSRE = -1 Or $iSRE = Default Then
        If $vCase = -1 Or $vCase = Default Then
            $vCase = 0
        Else
            $vCase = 1
        EndIf
        Local $sHold = '', $sSnSStart = '', $sSnSEnd = ''
        While StringLen($sString) > 0
            $sSnSStart = StringInStr($sString, $sStart, $vCase)
            If Not $sSnSStart Then ExitLoop
            $sString = StringTrimLeft($sString, ($sSnSStart + StringLen($sStart)) - 1)
            $sSnSEnd = StringInStr($sString, $sEnd, $vCase)
            If Not $sSnSEnd Then ExitLoop
            $sHold &= StringLeft($sString, $sSnSEnd - 1) & Chr(1)
            $sString = StringTrimLeft($sString, $sSnSEnd)
        WEnd
        If Not $sHold Then Return SetError(1, 0, 0)
        $sHold = StringSplit(StringTrimRight($sHold, 1), Chr(1))
        Local $avArray[UBound($sHold) - 1]
        For $iCC = 1 To UBound($sHold) - 1
            $avArray[$iCC - 1] = $sHold[$iCC]
        Next
        Return $avArray
    Else
        If $vCase = Default Or $vCase = -1 Then
            $vCase = '(?i)'
        Else
            $vCase = ''
        EndIf
        Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
        If IsArray($aArray) Then Return $aArray
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_StringBetween
Edited by Zedna
Posted

Ok been playing again and this makes no sense

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


$file = FileOpen(@ScriptDir & "\report.txt", 0)
$chars = FileRead($file, 1)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;-----------------------------------------------
;$chars2 = StringInStr($chars, "Date")
;MsgBox(0, "Search result:", $chars2)

;$date2 = StringRegExp($chars, "Date\h*(\H*)")
    ;MsgBox(0, "date", $date2)

;Local $check = _StringBetween($chars, ", ", ", ")
    ;MsgBox(0, "date", $check)

;Local $check = _StringBetween($chars, "Date", "Time")
    ;MsgBox(0, "date", $check)

;$date2 = StringRegExp($chars, "Date")
;$date3 = StringMid($date2,1)
    ;MsgBox(0, "date", $date3)

;$date2 = StringRegExp($chars, "Date\h*(\H*)")
;$date3 = $date2;[0] <<< this throws an error if i take the comment out before the [0]
    ;MsgBox(0, "date", $date3)
;-----------------------------------------------

FileClose($file)

ive tried all of these between the lines and every single one of them returns a 0, what am i missing plz?

all im trying to do is get it to return the 2010-11-18 on its own

Operating System                                  Microsoft Windows 7 Home Premium 6.1.7600 (Win7 RTM)
    Date                                              2010-11-18
    Time                                              13:45
    Computer Type                                     ACPI x86-based PC
    Operating System                                  Microsoft Windows 7 Home Premium
    OS Service Pack                                   -
    Internet Explorer                                 8.0.7600.16385
    DirectX                                           DirectX 11.0

I keep reading the bits for this in the help file but it dosent seem to work like it says

@Zedna thx for the info , unfortunatly the eg deals with arrays which i havent used

Hi Chimera,

This line will be a problem:

$chars = FileRead($file, 1)

The second argument "1" is telling it to only read one character from the file. If you omit the second argument, it'll default to reading the entire file which I assume you want to do.

If you want to do this without arrays and you are the one who wrote the program that actually generates this file, have a look at iniwrite() and iniread(). They are very easy to use and will let you insert and pull stuff out of an ini (just a text file).

It just involves a little rewrite of the file generating program if you're up for that.

Hope that helps.

Posted

Hi Chimera

I use this currently (see below). If you look at the StringTrimLeft(FileReadLine($file , 16), 21) line. The first number 16 counts 16 lines down then the 21 counts 21 characters accross and it returns whatever is after. i use this on windows 7 and it returns the directx version which for me is directx 11. Hope this is what you were after.

Dim $DXfile = @HomeDrive & "\dx.txt", $dxv
    SplashTextOn("Getting DirectX Info", " Please Wait...", 220, 35, -1, -1, 2, "", 8)
    Runwait('dxdiag.exe /whql:off /t'&@HomeDrive &'\dx.txt', '', @SW_HIDE)
   If FileExists ( $DXfile ) then
      FileOpen ( $DXfile, 0 )
   If $DXfile = -1 Then Exit
      $dxver = StringTrimLeft(FileReadLine($file , 16), 21) ;line to read the DirectX info
      FileClose( $DXfile )

      MsgBox(4096,"DirectX Version", $dxver)
      SplashOff()
    else
      MsgBox(4096,"error", $DXfile & " File not found.")
   EndIf

Drunken Frat-Boy Monkey Garbage

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
×
×
  • Create New...