Jump to content

Noob here, need help with lines and columns


Recommended Posts

I am very new to Autoit and pretty new to scripting altogether.I have a PowerShell script that pulls owner info from folders on our file share server. There are 3 owners for every folder, Domain Admin, User setup for network scanning and the user. I am trying to copy the username only from this txt file. I have gotten as far as to being able to read the lines in the txt file but can't seem to read the columns. This is what i have so far.

#include <File.au3>

$file = "c:\test01.txt"
FileOpen($file, 0)

For $i = 5 to _FileCountLines($file)
    $line = FileReadLine($file, $i)


 msgbox(0,'','the line ' & $i & ' is ' & $line & 'and the column is')
Next
FileClose($file)

 

This is the outcome.....
2016-12-27 16_58_44-C__Users_dpg_Desktop_New Text Document.au3 - SciTE.png

This is the txt file

2016-12-27 17_06_11-test01.txt - Notepad.png

I am trying to copy the ABC part without the MyDomain\. I know it's line 5 and columns 11-14 of the txt file but i can't figure out how to read it. I hope i'm explaining this correctly. 

Link to comment
Share on other sites

You're close, but they're not columns in AutoIt. A string is a string in AutoIt and cannot be treated as an array, unless you split it.

A couple of things to note. Don't use a variable used in a for loop outside the for loop. I.e., if you use For $i = n and $i was not declared prior to the for loop, it's bad practice. If you're going to read multiple lines from a file you're better off reading it once and store the contents in an array.

Hopefully this helps

#include <Array.au3>
#include <File.au3>

Global $aFileContents = Null
Global $aUsers[0]

CreateTest01Txt()

; Read the contents of the test01.txt file into an array
; Using _FileReadToArray because I don't like the count in the [0] element from FileReadToArray
_FileReadToArray(@ScriptDir & "\test01.txt", $aFileContents, $FRTA_NOCOUNT)

_ArrayDisplay($aFileContents, "File Contents")

; Go through every line of the file (every row of the array)
For $i = 0 to UBound($aFileContents) - 1
    Local $sUser = Null
    
    ; If the MyDomain\ is in this line
    If (StringInStr($aFileContents[$i], "MyDomain\")) Then
        ; Extract the username fromo the line 
        $sUser = StringRight($aFileContents[$i], StringLen($aFileContents[$i]) - StringInStr($aFileContents[$i], "\"))
        ; and add it to the $aUsers array
        _ArrayAdd($aUsers, $sUser)
    EndIf
Next

_ArrayDisplay($aUsers, "Users")

FileDelete(@ScriptDir & "\test01.txt")

Func CreateTest01Txt()
    Local $hFile = FileOpen(@ScriptDir & "\test01.txt", $FO_OVERWRITE)

    If ($hFile) Then
        FileWrite($hFile, "#TYPE System.Security.Principal.SecurityIdentifier" & @CRLF & _
                        "BinaryLength,AccountDomainSid,Value" & @CRLF & _
                        "Some Fill In Text" & @CRLF & _
                        ",,MyDomain\Domain Admins" & @CRLF & _
                        ",, MyDomain\ABC" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & _
                        ";ABC=Username")
        FileClose($hFile)
    EndIf
EndFunc

 

Link to comment
Share on other sites

@6401integramandj

Welcome to the forums.

Here are a few methods manipulating strings.

(Don't worry about the regular expressions (RegExp) examples if you don't understand them. You are not alone. )

#include <FileConstants.au3>

CreateTest01Txt()

$sFile = FileRead("test01.txt")

; ================= StringMid with StringInStr on @CR of 4th line ========
$iStartFifthLine = StringInStr($sFile, @CR, 0, 4) + 2 ; 2 is for the 2 characters, @CR and @LF, at end of line #4
$iEndFifthLine = StringInStr($sFile, @CR, 0, 5)
ConsoleWrite(StringMid($sFile, $iStartFifthLine + 11, $iEndFifthLine - ($iStartFifthLine + 11)) & @CRLF) ; 11 is for first 11 characters on line #5.
; ========================================================================

;Or

; ================== StringMid with  StringInStr on 2nd occurrence of "\" in file =================
$iStartOfRequiredCharacters = StringInStr($sFile, "\", 0, 2) + 1 ; Find second occurring "\" character.
;                                                                  And, 1 is for the first character after the found "\" character.
ConsoleWrite(StringMid($sFile, $iStartOfRequiredCharacters, $iEndFifthLine - $iStartOfRequiredCharacters) & @CRLF)
; =================================================================================================

;Or

; ============ RegExp 4 lines then match last occurrence of "\" on that line. =======
ConsoleWrite(StringRegExpReplace($sFile, "(?:\V*\v+){4}.+\\(.+)(?s).*", "$1") & @CRLF)
; "(?:\V*\v+){4}.+\\" - Match the first 4 lines, "(?:\V*\v+){4}", (non-capture group, no backreference), and all the characters,
;                       except linefeeds, on the next (5th) line, up to and including the last "\" character on that line., ".+\\".
; "(.+)" - Capture all characters that are not linefeeds. This is the first capture group and is referenced to by
;          backreference 1, "\1" or "$1" or "${1}".
; "(?s)" - From here on when matching characters the dot, ".", will also match linefeed characters.
; ".*" - Match all characters (including the linefeed at the end of the fifth line) to the end of the file.
; Parameter "replace"  = "$1" - As the entire file is matched, the text of the entire file is replaced with backreference 1.
; ===================================================================================

;Or

; ================= RegExp match to last occurrence of "\" in file =================
ConsoleWrite(StringRegExpReplace($sFile, "(?s).+\\(\V+).*", "\1") & @CRLF)
; "\V" - Matches any character that is not a vertical whitespace character (a linefeed).
; ==================================================================================

;Or

; =============== RegExp match to last occurrence of "MyDomain\\" in file ==========
ConsoleWrite(StringRegExpReplace($sFile, "(?s).+MyDomain\\(\V+).*", "${1}") & @CRLF)
; =================================================================================

FileDelete(@ScriptDir & "\test01.txt")


Func CreateTest01Txt()
    Local $hFile = FileOpen(@ScriptDir & "\test01.txt", $FO_OVERWRITE)

    If ($hFile) Then
        FileWrite($hFile, "#TYPE System.Security.Principal.SecurityIdentifier" & @CRLF & _
                "BinaryLength,AccountDomainSid,Value" & @CRLF & _
                "Some Fill In Text" & @CRLF & _
                ",,MyDomain\Domain Admins" & @CRLF & _
                ",,MyDomain\ABC" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & _
                ";ABC=Username")
        FileClose($hFile)
    EndIf
EndFunc   ;==>CreateTest01Txt

#cs ; Returns:-
ABC
ABC
ABC
ABC
ABC
#ce

 

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

×
×
  • Create New...