Jump to content

Pulling only Certain lines from Notepad


Recommended Posts

 

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.

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

@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

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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.

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

  • Moderators

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.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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.

 

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