Sign in to follow this  
Followers 0
deltron

"variable" fixed length strings

19 posts in this topic

I'm wondering if there's any way to create a "variable" fixed length string in autoit. What I'm wanting to do is for logging purposes to have a consistent looking log file.

here's an example of the current log output we have:

ENDPOINT    OPERATING SYSTEM    SERVICE PACK      PUSH NAME         BULLETIN      KB NUMBER     RESULT          RC      TIME/DATE
BJC11116698 Windows XP        Service Pack 2    Acrobat705            AcrRdr-705    NA          Already Installed   0       2005:12:07:18:17:10

(it might look a little strange since it's a long output due to word-wrap)

I'm wanting to create fixed-lenght fields, with each field being different length, say 10 characters, then 20 characters, then 3, etc. I'm not sure if there's any UDF created for this, I've tried to look for one, and there doesn't seem to be anything in the help file. In Winbatch, there's a function called strfix which allows you to set the length of strings, and also pads/truncates to fill the requested value. I hope that I stated it clearly enough, any questions let me know.

Share this post


Link to post
Share on other sites



Are you looking for something like this

Func _FixedVar($nLen, $nvar)

    $nvar = StringTrimRight($nvar, StringLen($nvar) - $nLen)
    Return $nvar
    
EndFunc

When used like this it will return the first four charaters of $var01.

$var01 = "This is a really long var"
$out = _FixedVar(4, $var01)
MsgBox(0, "Testing...", $out)

Func _FixedVar($nLen, $nvar)

    $nvar = StringTrimRight($nvar, StringLen($nvar) - $nLen)
    Return $nvar
    
EndFunc

[quote] Gilbertson's Law: Nothing is foolproof to a sufficiently talented fool.Sandro Alvares: Flaxcrack is please not noob! i can report you is stop stupid. The Post[/quote]I made this: FWD & MD5PWD()

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Not Exactly sure what you mean, but i'll take a stab.

Closest vanilla options in autoit are the STRINGLEFT() STRINGMID() ETC commands, Look in the help file.

append some dummy whitespace to your string value to pad out before slicing.

HardCopy

Edited by HardCopy

Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad

Share this post


Link to post
Share on other sites

I'm going to have go with HardCopy on this one... I'm a bit confused with the example and the explination on what it is you are looking for.


[center]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.[/center]

Share this post


Link to post
Share on other sites

I think what he is looking for is GUICtrlCreateListView.or something similar.



Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites
:lmao: Oh my...I'm all confused. Ryan Grelck, please explain.

[quote] Gilbertson's Law: Nothing is foolproof to a sufficiently talented fool.Sandro Alvares: Flaxcrack is please not noob! i can report you is stop stupid. The Post[/quote]I made this: FWD & MD5PWD()

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

try:

$ENDPOINT = "BJC11116698"
MsgBox(0, "AutoIt", ">>>" & $ENDPOINT & "<<<")
$ENDPOINT = PadIt($ENDPOINT, 16)
MsgBox(0, "AutoIt", ">>>" & $ENDPOINT & "<<<")

Func PadIt($var, $DesiredLength)
    If StringLen($var) >= $DesiredLength Then
        MsgBox(0, "AutoIt", "Are you crazy?")
        Exit
    EndIf
    Do
        $var = $var & " "
    Until StringLen($var) = $DesiredLength
    Return $var
EndFunc;==>PadIt

Edit1:

@others,

I think that he is generating a report (log file).

ENDPOINT is a column header

OPERATING SYSTEM is a column header

and so on...

when he gets his data for the report like:

BJC11116698 and Windows XP

he wants to be able to write that data

to a text file and have it line up under

the applicable column header

...so, he will have to pad each

variable to the desired length

before writing it to the report file.

Edit2:

@Ryan Grelck,

Let me know if you need more of an

explanation as to how the "UDF" works...

or just ignore the post if I did not understand your request.

Edit3: cannot spell

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Ok i think i now know what you mean.

I knocked this up allows left and right justification

I called it _StrFix to keep familiarity 8)

;;example
$MyStringA = "I Am A String"
$MyStringB = "123456"

    $Var1 = _StrFix($MyStringA,"L",20)
    $Var2 = _StrFix($MyStringb,"R",15)
    
    
    ConsoleWrite( "Left  Justified : " & $Var1 & @LF)
    ConsoleWrite( "Right Justified : " & $Var2 & @LF)



;; $Align is "L"eft & "R"ight / $Chrs is number of characters.
Func _StrFix($sString,$vAlign,$vChrs)
    Local $sBlank = "                                                                          "
    
    Select
    Case $vAlign = "L"
        $sString = StringLeft($sString & $sBlank,$vChrs)
        
    Case $vAlign = "R"
        $sString = StringRight($sBlank & $sString,$vChrs)
    Case Else
        SetError(1)
        Return ""   
    EndSelect
    SetError(0)
    Return $sString
EndFunc

Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

edit: @herewasplato - you got it right, trying to create a custom formatted log file in seperate columns.

Sorry, I was just in a meeting. There won't be a gui to this, as it's a silent install/uninstall script. Basically, let me show you my code in winbatch.

each of these variables were inited with a set number, such as ComputerNameLen = strlen( ComputerName), PushNameLen = 20, etc.

edit: this just sets the top columns up in WinBatch

:SetLogLine
    Now = TimeYmdHms ( )
    LogLine = StrCat (StrFix (ComputerName, ' ', ComputerNameLen), @TAB, StrFix (WinOS, ' ', WinOSLen), @TAB, StrFix (csdver, ' ', csdverLen), @TAB)
    LogLine = StrCat (LogLine, StrFix (PushName, ' ', PushNameLen), @TAB, StrFix (Bulletin, ' ', BulletinLen), @TAB, StrFix (KBNumber, ' ', KBLen), @TAB)
    LogLine = StrCat (LogLine, StrFix (Result, ' ', ResultLen), @TAB, StrFix (ReturnCode,' ',RCLen), @TAB, Now)
    FileWrite (MyFile, LogLine)
Return

here's some pseudocode to show kinda what I want to do. I really want to avoid having many if-then statements and having to call StringMid, StringRight, etc in the code and replace it with a function.

$log = FileOpen("c:\temp\logfile.log", 1)
$app = "Mozilla Firefox"
$endpoint = @COMPUTERNAME
$endpointlen = stringlen($endpoint)
$os = @OSVERSION
$logheader = fixedstring("ENDPOINT", " ", 20) & @TAB & fixedstring("OS", " ", 10) & @TAB & fixedstring($app, " ", 20) etc..
$logstring = fixedstring($endpoint, " ", 20) & @TAB & fixedstring($os, " ", 10) & @TAB & fixedstring($app, " ", 20)  etc..

If $log = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWriteLine($log, $logheader)
FileWriteLine($log, $logstring)
FileClose($log)
Edited by Ryan Grelck

Share this post


Link to post
Share on other sites

Would you mind uploading a .log (even a dummy one if you have it for testing would be great.)


[center]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.[/center]

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

$log = FileOpen("c:\temp\logfile.log", 1)
If $log = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($log, PadIt("ENDPOINT", 20))
FileWrite($log, PadIt("OS", 10))
FileWrite($log, @CRLF)

FileWrite($log, PadIt(@ComputerName, 20))
FileWrite($log, PadIt(@OSVersion, 10))
FileWrite($log, @CRLF)

FileClose($log)

Func PadIt($var, $DesiredLength)
    If StringLen($var) >= $DesiredLength Then
        MsgBox(0, "AutoIt", "Are you crazy?")
        Exit
    EndIf
    Do
        $var = $var & " "
    Until StringLen($var) = $DesiredLength
    Return $var
EndFunc ;==>PadIt
It just depends on how you want to layout your code....

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Nice Plato... with that post I think I actually understand what he was trying to do :lmao:


[center]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.[/center]

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

$log = FileOpen("c:\temp\logfile.log", 1)
If $log = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($log, PadIt("ENDPOINT", 20))
FileWrite($log, PadIt("OS", 10))
FileWrite($log, @CRLF)

FileWrite($log, PadIt(@ComputerName, 20))
FileWrite($log, PadIt(@OSVersion, 10))
FileWrite($log, @CRLF)

FileClose($log)

Func PadIt($var, $DesiredLength)
    If StringLen($var) >= $DesiredLength Then
        MsgBox(0, "AutoIt", "Are you crazy?")
        Exit
    EndIf
    Do
        $var = $var & " "
    Until StringLen($var) = $DesiredLength
    Return $var
EndFunc;==>PadIt
It just depends on how you want to layout you code....
ah, this is exactly what I needed, you sir, rock. I think that putting the writes on different lines makes it look a lot cleaner too. thanks again for your help! o:)

another quick question, what's the best way to concatenate strings together? I don't see anything in the help files/forum search that quickly allows you to do so.

edit: $string = $string & "otherstuff" ?

I think I need a coffee break ;):lmao:

Edited by Ryan Grelck

Share this post


Link to post
Share on other sites

ah, this is exactly what I needed, you sir, rock. I think that putting the writes on different lines makes it look a lot cleaner too. thanks again for your help! o:)

another quick question, what's the best way to concatenate strings together? I don't see anything in the help files/forum search that quickly allows you to do so.

edit: $string = $string & "otherstuff" ?

I think I need a coffee break ;):lmao:

Why not convert the log to an html or csv format?

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Share this post


Link to post
Share on other sites

ah, this is exactly what I needed, you sir, rock. I think that putting the writes on different lines makes it look a lot cleaner too. thanks again for your help! :lmao:

You are welcome...

You can replace this line in the UDF with something a bit more useful:

MsgBox(0, "AutoIt", "Are you crazy?")


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

You are welcome...

You can replace this line in the UDF with something a bit more useful:

MsgBox(0, "AutoIt", "Are you crazy?")

lol, yeah, I figured as much :lmao:

Share this post


Link to post
Share on other sites

Why not convert the log to an html or csv format?

I like the idea of a CVS file. Then you can sort your file and that would give you some nice reports you could pull. Good call! :lmao:


[quote] Gilbertson's Law: Nothing is foolproof to a sufficiently talented fool.Sandro Alvares: Flaxcrack is please not noob! i can report you is stop stupid. The Post[/quote]I made this: FWD & MD5PWD()

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

I like the idea of a CVS file. Then you can sort your file and that would give you some nice reports you could pull. Good call! :lmao:

Thanks.

I like working on projects that include logs, they are essential to my network admin tasks.

I'm also a big Tufte fan and his view(s) on the presentation and organization of data. To me the log is one of the most important forms of data in my line of work, so presenting it into a effective manner is very important to me (saves so much time).

i'm trying to get into outputing my logs into an xml format. but i'm such a noob and so slow at learning.

oh well, got to get back to keeping on.

lates.

-Blademonkey

edit: i make so many @#!$ typos.

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

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  
Followers 0