Jump to content

Help with selecting a string from a non standard text file.


Azazash
 Share

Recommended Posts

Hi,

I'm new here but have been lurking for a while now. I've been writing a program that help me do various admin tasks at work, and am stuck at a particular point.

What i am trying to achieve is the following:

I have a file on their local machine, say in for instance @SystemDir that is an RTF file but saved with a non standard file extension of PLN (for Plain :) ). Within this file are a series of lines that contain some information that i need to extract. The information in question will always start with three spaces, followed by a random series of characters, then finished with an ampersand.

(^^^ Denotes 3 spaces for ease of use)

eg ^^^computer1& ^^^computer2& fdgjn4 4fnjn 4 4 e d df ^^^computer3&

As you can see there are other bits of information that i do not want to extract, just the strings beginning with 3 spaces and ending in the &

The Problem.

Is it possible to extract every instance of the string, that is between the 3 spaces and the ampersand without extracting anything else? there by having an array that i can output into something simple like a label that shows only the following info, with the spaces and &'s stripped:

computer1

computer2

computer3

Its probably not explained very well....

Ive not really got any code together yet as im still experimenting with the FileOpen and FileRead commands but im not getting anywhere

Link to comment
Share on other sites

Hi,

I'm new here but have been lurking for a while now. I've been writing a program that help me do various admin tasks at work, and am stuck at a particular point.

What i am trying to achieve is the following:

I have a file on their local machine, say in for instance @SystemDir that is an RTF file but saved with a non standard file extension of PLN (for Plain :) ). Within this file are a series of lines that contain some information that i need to extract. The information in question will always start with three spaces, followed by a random series of characters, then finished with an ampersand.

(^^^ Denotes 3 spaces for ease of use)

eg ^^^computer1& ^^^computer2& fdgjn4 4fnjn 4 4 e d df ^^^computer3&

As you can see there are other bits of information that i do not want to extract, just the strings beginning with 3 spaces and ending in the &

The Problem.

Is it possible to extract every instance of the string, that is between the 3 spaces and the ampersand without extracting anything else? there by having an array that i can output into something simple like a label that shows only the following info, with the spaces and &'s stripped:

computer1

computer2

computer3

Its probably not explained very well....

Ive not really got any code together yet as im still experimenting with the FileOpen and FileRead commands but im not getting anywhere

Look at the StringSplit() function in the help file.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

Look at the StringSplit() function in the help file.

Ok so i looked at the help example and it might equate like this:

$file = FileOpen("file.pln")

$pcname = StringSplit(""&$file, "&")

This should in theory, return anything in that file with the & used as a delimiter, the problem with that is the extra information between the delimiters that i do not want.

For example (^^^ to denote 3 spaces)

serial number ^^^1235467A& Postcode Date serial number ^^^1234567B& Postcode Date serial number ^^^1234567C&

Im only interested in getting the 1234567A or B or C numbers not the rest.

StringSplit would give me:

serial number ^^^1235467A for result 1

Postcode Date serial number ^^^1234567B for result 2

Postcode Date serial number ^^^1234567C for result 3

Link to comment
Share on other sites

  • Moderators

#include <array.au3>
Global $sText = "   computer1&    computer2& fdgjn4 4fnjn 4 4 e d df    computer3&"
Global $aSRE = _CustSRE($sText, "\s{3,}", "&")
_ArrayDisplay($aSRE)

Func _CustSRE($sIn, $vDelim1, $vDelim2 = "", $vCase = 0)
    If $vCase <> 0 Then $vCase = ""
    If $vCase = 0 Then $vCase = "(?i)"
    Return StringRegExp($sIn, "(?s)" & $vCase & $vDelim1 & "(.*?)" & $vDelim2, 3)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <array.au3>
Global $sText = "   computer1&    computer2& fdgjn4 4fnjn 4 4 e d df    computer3&"
Global $aSRE = _CustSRE($sText, "\s{3,}", "&")
_ArrayDisplay($aSRE)

Func _CustSRE($sIn, $vDelim1, $vDelim2 = "", $vCase = 0)
    If $vCase <> 0 Then $vCase = ""
    If $vCase = 0 Then $vCase = "(?i)"
    Return StringRegExp($sIn, "(?s)" & $vCase & $vDelim1 & "(.*?)" & $vDelim2, 3)
EndFunc
Thanks for this, i've twaeked it a tiny bit to allow me to open a file for reading and pass the correct data into the array, but forgive me for being a bit slow, how do i output the array contents into a form?

would like them to be presented on the screen as:

xxxx

xxxx

xxxx

etc not as the _ArrayDisplay pop up.

Im not very good at this programming malarky :)

Link to comment
Share on other sites

...forgive me for being a bit slow, how do i output the array contents into a form?... Im not very good at this programming malarky...

Mr. Withthe Malarkey - def: "empty rhetoric or insincere or exaggerated talk"

Now when need to play a completely different game: Read the help file; especially the parts describing the building of GUI and the function GUICtrlCreateLabel($Array[1],10,10,80,31).

Browse the Example Scripts part of the AutoIt forums and try some of the simpler scripts there to learn some techniques.

And be a man, def: "only rarely ever refered directly to verse 6, 7, or even verse 8 of the Official Canon of Valik".

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

  • Moderators

Thanks for this, i've twaeked it a tiny bit to allow me to open a file for reading and pass the correct data into the array, but forgive me for being a bit slow, how do i output the array contents into a form?

would like them to be presented on the screen as:

xxxx

xxxx

xxxx

etc not as the _ArrayDisplay pop up.

Im not very good at this programming malarky :)

Global $sText = "   computer1&    computer2& fdgjn4 4fnjn 4 4 e d df    computer3&"
MsgBox(64, "Malarky", _CustSRE($sText, "\s{3,}", "&"))

Func _CustSRE($sIn, $vDelim1, $vDelim2 = "", $vCase = 0, $vDelim3 = @CRLF)
    If $vCase <> 0 Then $vCase = ""
    If $vCase = 0 Then $vCase = "(?i)"
    Local $aSRE = StringRegExp($sIn, "(?s)" & $vCase & $vDelim1 & "(.*?)" & $vDelim2, 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    Local $sHold
    For $iCC = 0 To UBound($aSRE) - 1
        $sHold &= $aSRE[$iCC] & $vDelim3
    Next
    Return StringTrimRight($sHold, StringLen($vDelim3))
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Global $sText = "   computer1&    computer2& fdgjn4 4fnjn 4 4 e d df    computer3&"
MsgBox(64, "Malarky", _CustSRE($sText, "\s{3,}", "&"))

Func _CustSRE($sIn, $vDelim1, $vDelim2 = "", $vCase = 0, $vDelim3 = @CRLF)
    If $vCase <> 0 Then $vCase = ""
    If $vCase = 0 Then $vCase = "(?i)"
    Local $aSRE = StringRegExp($sIn, "(?s)" & $vCase & $vDelim1 & "(.*?)" & $vDelim2, 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    Local $sHold
    For $iCC = 0 To UBound($aSRE) - 1
        $sHold &= $aSRE[$iCC] & $vDelim3
    Next
    Return StringTrimRight($sHold, StringLen($vDelim3))
EndFunc
Well this did exactly what you said it would, thanks your a genious, but unfortunately it doesn't read the files i need it to, only text files.

the file i need looks like this..

Seen on Dictated at DateTime3

,Routine,Today,Urgent,Immediate 9E-35F9-D09A JWhS9x2 Reference Recipient Seen by Hã@ APROBRIUM_NY& {8B77867E-640B-49C8-84BA-1B8BBE2875BC} À8ã@

APROBRIUM3& {686D1D12-8004-49ED-893D-BC539B28BA31} Fã@

APROBRIUM2& {2034B711-0BEE-4EED-811D-E457590CF8B7} =ã@

APROBRIUM6& {69380FC2-DE5E-4B5E-87A2-739FAE850884} 9ã@

APROBRIUM5& {AA1317C3-1290-4336-BF1F-CA47AB0F0532} Hã@ APROBRIUMXPDEV2& {F5F8489A-12CF-40A6-91AC-02CE171F77E9} ÀGã@ APROBRIUMXPDEV1& {492B0F7B-F1DA-465F-B378-36880CB371BC} ÀFã@ VM_XP& {78FE6E74-6802-4472-B8B2-637CF34B28E5}

Then imagine all this is presented on one line :)

is it possible to read non standard text files?

Link to comment
Share on other sites

Have you even tried? If it doesn't work, then you can ask more questions.

Of course i've tried, which is why im asking, and i have checked in the AutoIt help file which doesn't explain about reading files other than text based.

The example provided by SmOke_N worked perfectly for displaying an array containing standard text, but fails to output anything when used with the file i need.

I think the problem is that its not a standard text file, hence the original thread subject name. If its the file thats causing the problem, then its the FileRead command that ive used to get the data, which obviously doesn't support the file witht he PLN extension im using.

The file does contain the symbols and ascii text im looking for however so the function SmOke_N provided should work.

Sorry if i didn't make my questions very clear, and that i apparently used Malarky wrong :), i do really appreciate the help though.

Edited by Azazash
Link to comment
Share on other sites

This is an image of the file that i need. I guess the fact that what i thought were spaces were actually null values means that it wont work.

bah forget it its a waste of my time.

Posted Image

Read it in binary...
Link to comment
Share on other sites

if you know all the escape characters you can replace them, for example:

$sText = FileRead(@ScriptDir & "\file.pln")
$sText = StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($sText, Chr(11), ""), Chr(27), "|"), Chr(7), ""), Chr(15), ""), Chr(5), ""), Chr(12), @CRLF), Chr(0) & Chr(0) & Chr(0), @CRLF)

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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...