Jump to content
Sign in to follow this  
6401integramandj

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. 

Share this post


Link to post
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

 

Share this post


Link to post
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

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Kiko745
      Hello!
      I am new to autoit so please if someone could help me. I am trying to combine multiple .txt files but I can't get it right. In every file are the exact amout of lines ( for example here are 2 ) ,            I don't want to make it for just 2 lines for each txt files but for all the lines the txt files contains. The thing I want to do is :
      Text File 1
      Hello my name
      I am a big
      ...
      Text File 2
      is Fred,
      Potato head for
      ...
      Text File 3
      and I like to eat pizza.
      Not getting this right.
      ...
      ------------------- Combine them all into one txt file like here ------------------------------
      Final Text file
      Hello my name is Fred, and I like to eat pizza.
      I am a big Potato head for Not getting this right.
      ...
       
      Thanks for any advice!
      P.S. Sorry for my English, not my first language.
       
    • By ThePoro
      Hello,
      I have a problem with getting first 10 characters with a big txt file, about 50k lines. I only want 10 characters of each line and save it into another file. I tried to use Stringleft every line and save it into another file but it was really slow to do it. I tried to save it into a variation like $var=$var&stringleft($file,10)&@CRLF. It's faster but still really slow.
      Can anybody help me with this
      Thank you so much
    • By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • By kneze
      hi
      i use from Command Line net user /Domain UserID >> C:\temp\sample.txt to request informations about specific Domain User. Now i wil get Password last set, Password expires, password changeable and all Groups from Global Group Membership of this User to display in a MsgBox. With FUNC _readGroups i can check which position is group date of last password set but i don't know how to get needed informations as i wrote at the beginning.
      Thanks in advance for any hint how can i solve it.
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.3 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <File.au3> $FilePath=@ScriptDir &"\sample.txt" $StringToSearch="Password last set" $CaseSense=0 $Lines=_FileCountLines($FilePath) $hFile=FileOpen($FilePath,0) $LinesCount=_FileCountLines($FilePath) For $i=0 To Number($Lines) $Test=FileReadLine($hFile,$i) If StringInStr($Test,$StringToSearch,$CaseSense) Then $foundLine = $i _readGroups($foundLine,$LinesCount,$i) ;MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $i) ;MsgBox(0,"","Lines found: " & $LinesCount) ExitLoop EndIf Next FileClose($hFile) FUNC _readGroups($foundLine,$LinesCount,$i) ;For $j=$foundLine To Number($LinesCount) $lLine=FileReadLine($hFile,$i) ;$split = StringSplit($lLine," ") $split = StringSplit(StringStripWS($lLine,1)," ") For $x = 1 To $split[0] MsgBox(0,$x,$split[$x]) Next ;MsgBox(0,"",$split[15]& " " & $split[16]) ;Next EndFunc  
      sample.txt
×
×
  • Create New...