Jump to content

read specific content from text file


 Share

Recommended Posts

Hi

i try to read Wlan Profiles from local Computer. First Step Command Prompt: netsh wlan show profiles >> C:\temp\test\wlan.txt.

Second Step (here's a sample script to find a solution for my problem. Script read each line of wlan.txt and Display Name of Wireless Connection which i have set up in the past.  If i edit wlan.txt to wlan - sample.txt script works. wlan.txt containsline without : so i get error message. How can i read only lines beginning with All User Profile so i can use original file wlan.txt which i crate in the command prompt with: netsh wlan show profiles >> C:\temp\test\wlan.txt ?

Thanks for any suggestions.

Kneze

$Form1 = GUICreate("Form1", 374, 268, 892, 512)
$Button1 = GUICtrlCreateButton("button", 145, 35, 75, 25)

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

 Case $Button1

$file = "C:\temp\test\wlan.txt"
FileOpen($file, 0)

For $i = 1 to _FileCountLines($file)
    $line = FileReadLine($file, $i)
    $after = StringSplit($line, ":")[2]

MsgBox(262144, "Result", $after, 0)


Next
FileClose($file)


    EndSwitch
WEnd

 

 

wlan - sample.txt

wlan.txt

Edited by kneze
Link to comment
Share on other sites

Could you post up an example of the .txt file?

I imagine you can find a different way to parse the line, such as the line break instead of the colon.

Also you can get the results directly in code without writing to a file first unless you need/want the physical file for some reason.

Link to comment
Share on other sites

Good catch, I was looking for it posted in the message body.

This is just one way of doing it:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Include <Array.au3>

$Form1 = GUICreate("Form1", 374, 268, 892, 512)
$Button1 = GUICtrlCreateButton("button", 145, 35, 75, 25)

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _ReadFile()
    EndSwitch
WEnd

Func _ReadFile()
$sFile = FileRead(@ScriptDir & "\wlan.txt")
$aResults = StringRegExp($sFile, "(?i)all user profile\s+:\s*(.*)", 3)
_ArrayDisplay($aResults)
EndFunc

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

16 hours ago, mikell said:

If each profile includes "@" then this other way could work  :)

#Include <Array.au3>
$aResults = StringRegExp(FileRead(@ScriptDir & "\wlan.txt"), '\S+@\S+', 3)
_ArrayDisplay($aResults)

 

It would capture the wireless profile but not meet the criteria must match "All User Profile" it could be user specific profiles it matches also.

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