Lizardking141

Pulling only Certain lines from Notepad

15 posts in this topic

#1 ·  Posted

 

Hello, I am doing a project for work to make my life easier in the long run. One of my morning routines is to run a manual update of the companies AV definitions on the computers that are out of date. I pull the info from the reports in the AV and put it into a Notepad file so I can format it into pretty much a CSV with only the computer names so that the manager client can recognize it when in the parts that allow you to send out mass updates. When I first get the info from the report it looks like this:

Notepad Censored.png

When I get done formatting it it should look like this:

notepad formated.png

I am hoping to get a script that can do this formatting for me. I cannibalized some code from some examples by MHz in this old post and tried to integrate the two examples into one: 

and came up with this:

; filename to read
$file_to_read = "file_to_read.txt"
; open file to read and store the handle
$handle_read = FileOpen($file_to_read, 0)
; check the handle is valid
If $handle_read = -1 Then
    ; show warning and exit with code 1
    MsgBox(0, @ScriptName, 'failed to open handle to read the file')
    Exit 1
EndIf

; filename to write
$file_to_write = "file_to_write.txt"
; open file to read and store the handle
$handle_write = FileOpen($file_to_write, 2); erase mode
; check the handle is valid
If $handle_write = -1 Then
    ; close read handle here as write handle is invalid
    FileClose($handle_read)
    ; show warning and exit with code 2
    MsgBox(0, @ScriptName, 'failed to open handle to read the file')
    Exit 2
EndIf

; loop through each line of the file
While 1
    ; read each line from a file
    $line_read = FileReadLine($handle_read)
    ; exit the loop if end of file
    If @error Then ExitLoop
    ; show the line read (just for testing)
    MsgBox(0, 'Line read', $line_read)
    ; get the fist part of the line up to the whitespace
    $line_array = StringRegExp($line_read, '(.+?)\s+', 3)
    ; Check to see if it is a line we want to move over
    If $line_array = "SI00*********" Then
        ; write each line to a file
        FileWriteLine($handle_write, $line_array)
    EndIf
WEnd

; close the file handle for read
FileClose($handle_read)
; close the file handle for write
FileClose($handle_write)

Just looking to see if I am going in the right direction or if there is an easier way to do something like this that I am overlooking. 

Thanks for anything you can think of.

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

Since its a Tab delimited file you could just read the file directly into an array.

#include <Array.au3>
#include <File.au3>
Global $sFileRead = 'File_To_Read.txt'
Global $sFileWrite = 'File_To_Write.txt'
Global $hFileWrite, $aFileRead, $aComputer

_FileReadToArray($sFileRead, $aFileRead, 1, @TAB)
    If @error <> 0 Then Exit

$hFileWrite = FileOpen($sFileWrite, 2)
$aComputer = _ArrayUnique($aFileRead, 0, 1)
For $x = $aComputer[0] To 1 Step - 1
    If StringLeft($aComputer[$x], 4) = 'SI00' Then FileWrite($sFileWrite, $aComputer[$x] & @CRLF)
Next
FileClose($sFileWrite

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks, I figured there would most likely be a simpler way to do it, I just have only been using the language for like 2 weeks so I was taking examples and learning from them and trying to piece things together to do what I needed from there. This gives me more to look into and try. 

Thanks Again. 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@Lizardking141

It s now a big time i am trying to improve my autoit skills

and if you can format your text file into

[Stats]
Use=47

[Optional]
Folder1=

[Message]
Message1=hello

[Pause]
Time=3000

[User]
User1=

With settings knowledge

Autoit helpfile:
 

$SettingsFile = @SCRIPTDIR & '\Settings.ini'

$S1 = IniRead

 IniWrite

May it s not realy related but it can give you an other way for working about that.

I dit a looooot of nice thing with

IniRead

IniWrite

Edited by caramen

Share this post


Link to post
Share on other sites

#5 ·  Posted

The OP states the output is in a comma delimited text file. I doubt he wants to convert the format to an INI just to satisfy a script, especially when the suggestion by Subz will do what he needs.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#6 ·  Posted

54 minutes ago, Lizardking141 said:

I just have only been using the language for like 2 weeks so I was taking examples and learning

@JLogan3o13 Becose may exactly like me he whould like to learn some way to work with autoit.

Share this post


Link to post
Share on other sites

#7 ·  Posted

I'm all for learning the language; that is what we are here to support. But you seem to be posting in a number of threads lately; the posts either are inflammatory due to your lack of understanding of what is being asked (comments directed at Champak), or not at all related to what the OP is asking (this thread). Posting just to see your name appear in a thread, or to bump your post count, is not going to earn you any friends on the forum.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#8 ·  Posted

You might post the txt file, It would be much easier to make tests
However assuming that the names begin with SI and end with BLU you could try this - obviously untested, lazily I didn't copy the content of your image to a txt file :)

#Include <Array.au3>

$txt = FileRead("test.txt")
$aNames = StringRegExp($txt, '(?m)^SI\d+BLU', 3)
_ArrayDisplay($aNames)

$sNames = _ArrayToString($aNames, ", ")
MsgBox(0, "$sNames", $sNames)

 

1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted

27 minutes ago, mikell said:

assuming that the names begin with SI and end with BLU

The majority of the time this is correct, sometimes there are extra letters at the end of the BLU if we reimage a computer and rename it while being too lazy to remove the original computer name from the domain so it gets an extra character. I will make a new text file and do some trimming to cut out some info I would rather not go outside the company and get it uploaded.

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

1 minute ago, Lizardking141 said:

The majority of the time this is correct, sometimes there are extra letters...

Well, it's precisely the reason why a pretty correct/complete sample txt file is needed  :)

Share this post


Link to post
Share on other sites

#11 ·  Posted

3 minutes ago, mikell said:

pretty correct/complete

The only things I pulled out were details inside the lines, those being user names and IP addresses. Just me being paranoid. :D

Test Input.txt

Share this post


Link to post
Share on other sites

#13 ·  Posted

Nice @mikell, Thanks works beautifully. Just have to remember to clear the output file after I transfer it back to the management console, but that is a lot easier and quicker than doing that formatting by hand every day. :D

Share this post


Link to post
Share on other sites

#14 ·  Posted

4 minutes ago, Lizardking141 said:

Just have to remember to clear the output file

A little sweat left for you :)

Glad I could help

Share this post


Link to post
Share on other sites

#15 ·  Posted

@Lizardking141 -

Quote

Just have to remember to clear the output file after I transfer it back to the management console

Just change the last line with the following:

$hFileOpen = FileOpen(@scriptdir & "\Output.txt", 2)
FileWrite($hFileOpen, $sNames)
FileClose($hFileOpen)

 

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