Jump to content
Sign in to follow this  
jslegers

Searching in a text file how ?

Recommended Posts

jslegers

Hi,

I want to make a script that can read a text file and uses the search results as variables.

Here is the test file :

ListUsbDrives V1.7.7

Lists attached USB drives and their USB port names for USBDLM

Freeware by Uwe Sieber - www.uwe-sieber.de

Current User = JSL

LogonType = local

Member of groups = Debugger Users, Gebruikers foutopsporing, Administrators

Admin = yes

MountPoint = F:\

Volume Label = PORT_APPS

Volume Size = 1 GB (FAT32)

Volume Name = \\?\Volume{84913105-cda4-11db-8fee-0018debf0475}\

MultiCardReader = no

NoMediaNoLetter = yes

Drive Type = removable drive

Bus Type = USB

Device Types = HotPlug

Drive DevID = USBSTOR\DISK&VEN_KINGSTON&PROD_DT_ELITE_HS_2.0&REV_5.02\07A11C415350E8C0&0

Ctrl DevID = USB\VID_08EC&PID_0015\07A11C415350E8C0

Host Controller = Intel® 82801G (ICH7 Family) USB2 Enhanced Host Controller - 27CC

Volume DosDevName = \Device\Harddisk3\DP(1)0-0+a

Disk DosDevName = \Device\000000cc

Device Number = 3

Removal Policy = surprise removal

Partition Number = 1 of 1

Friendly Name = Kingston DT Elite HS 2.0

USB Version = 2.0 (high speed)

USB Friendl. Name = Kingston DT Elite HS 2.0

USB Serial = 07A11C415350E8C0

USB Port Name = 5-6

MountPoint = P:\

Volume Label = Samsung External

Volume Size = 250 GB (NTFS)

Disk Size = 250 GB

Volume Name = \\?\Volume{4362ce19-9f49-11dc-92bd-005056c00008}\

MultiCardReader = no

NoMediaNoLetter = no

Drive Type = fixed drive

Bus Type = USB

Device Types = HotPlug

Drive DevID = USBSTOR\DISK&VEN_SAMSUNG&PROD_HM250JI&REV_0-08\30464D007365&0

Ctrl DevID = USB\VID_152D&PID_2338\30464D007365

Host Controller = Intel® 82801G (ICH7 Family) USB2 Enhanced Host Controller - 27CC

Volume DosDevName = \Device\HarddiskVolume3

Disk DosDevName = \Device\000000c6

Device Number = 1

Removal Policy = surprise removal

Partition Number = 1 of 1

Friendly Name = SAMSUNG HM250JI

USB Version = 2.0 (high speed)

USB Friendl. Name = JMicron USB to ATA/ATAPI Bridge

USB Serial = 30464D007365

USB Port Name = 5-5

MountPoint = U:\

Volume Label = ---

Volume Size = 130 MB (FAT32)

Volume Name = \\?\Volume{47ec7e40-d2e0-11db-9000-00170845b81d}\

MultiCardReader = no

NoMediaNoLetter = yes

Drive Type = removable drive

Bus Type = USB

Device Types = HotPlug

Drive DevID = USBSTOR\DISK&VEN_USB&PROD_FLASH_DRIVE&REV_1.12\070B000414B0B70&0

Ctrl DevID = USB\VID_1005&PID_B113\070B000414B0B70

Host Controller = Intel® 82801G (ICH7 Family) USB2 Enhanced Host Controller - 27CC

Volume DosDevName = \Device\Harddisk2\DP(1)0-0+8

Disk DosDevName = \Device\000000c9

Device Number = 2

Removal Policy = surprise removal

Partition Number = 1 of 1

Friendly Name = USB Flash Drive

USB Version = 2.0 (high speed)

USB Friendl. Name = USB Flash Drive

USB Serial = 070B000414B0B70

USB Port Name = 5-4

I want to make an script that reads this file and creates variables from the names after Frinedly Name. For exapmle $var1 =Kingston DT Elite HS 2.0 $var2 = SAMSUNG HM250JI and so one. If the text has more friendly names filled it must produce more variables. If tried out this test script below but doesn't work :

#include <File.au3>
;RunWait (@ComSpec &  " /c ListUsbDrives.exe > usbdrives.txt", @ScriptDir, @SW_HIDE )
$Usbfile = FileOpen ( @ScriptDir & "\usbdrives.txt", 1 )
$Lines = _FileCountLines($Usbfile)
ConsoleWrite ( $Usbfile & @LF )
ConsoleWrite ( $Lines & @LF )
$File = $Usbfile
For $Index = 1 To $Lines
    $Data = FileReadLine($File,$Index)
    If StringInStr($Data,"Friendly Name") Then
        $String = StringStripWS($Data,2)
        $Name = StringSplit($String," ")
        MsgBox(-1,"",$Name[4])
    EndIf
Next
FileClose($File)

I have already made an script for removing USB drives but this one only works with P: drive and U: drive. The end result must be something like this :

#include <GUIConstants.au3>

;Autoit Options
AutoItSetOption ( "TrayIconHide", 1 )

;Defining variables
$driveltr = ""
$prg = "RemoveDrive.exe"

;Creating GUI
GUICreate ( "Select Device", 175, 105, -1, -1, $WS_BORDER )
GUISetState ( @SW_SHOW )

;Creating radio buttons
$radio1 = GUICtrlCreateRadio ( "Portable Harddisk",5 ,5 ,150 )
$radio2 = GUICtrlCreateRadio ( "USB Flash Drive",5 ,25 ,150 )

;Creating action button
$button1 = GUICtrlCreateButton ( "Remove device" ,5 ,47 ,95 )
$button2 = GUICtrlCreateButton ( "Exit" ,130 ,47, 35 )

;Standard radio1 selected.
GUICtrlSetState ( $radio1, $GUI_CHECKED )

;Displaying GUI until it is closed.
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE or $msg = $button2
            ExitLoop
            
        Case $msg = $button1 
            If GUICtrlRead ( $radio1 ) = 1 Then $driveltr="P:"
            If GUICtrlRead ( $radio2 ) = 1 Then $driveltr="U:"
        ;RunWait ( $prg & " " & $driveltr, @ScriptDir )
            ShellExecute($prg,$driveltr,@Scriptdir)
            Exit
    EndSelect
Wend
Exit

But this code only works when you have and USB harddisk mounted to P: and an USB stick to U:. (I use USBDLM for this). I want to make an script that works on every PC without specific drive letters voor USB Stick/Harddisks.

Share this post


Link to post
Share on other sites
BrettF
Andreik

Use WMI Classes for USB.


When the words fail... music speaks

Share this post


Link to post
Share on other sites
jslegers

Use WMI Classes for USB.

How can I use that ?

Share this post


Link to post
Share on other sites
jslegers

Arrays are probably the easiest way to go here... :P

Hi,

I have tried this but kind figure out how to use the = sign to split it in to two columns.

#include <file.au3>
#include <Array.au3>
#include <String.au3>

Local $records
$file = ( @ScriptDir & "\Listdrives.txt" )
$array = $file
_FileReadToArray ( $array, $records )
_ArrayDisplay ( $records, "Test Array" )
Local $results = _ArraySearch ( $records, "Friendly Name     " )
_ArrayDisplay ( $results, "Results" )

Share this post


Link to post
Share on other sites
zorphnog

Here ya go:

$hInFile = FileOpen(@ScriptDir & "\usbdrives.txt", 0)
$szFileText = FileRead($hInFile)
FileClose($hInFile)
$arResult = StringRegExp($szFileText, "(?i)friendly name = (.+)", 3)
If @error <> 0 Then 
    ConsoleWrite("!> No matches or bad pattern" & @LF)
    Exit
EndIf
For $i=0 To UBound($arResult)-1
    ConsoleWrite(StringFormat(" [%02d] %s\n", $i, $arResult[$i]))
Next

Share this post


Link to post
Share on other sites
jslegers

Here ya go:

$hInFile = FileOpen(@ScriptDir & "\usbdrives.txt", 0)
$szFileText = FileRead($hInFile)
FileClose($hInFile)
$arResult = StringRegExp($szFileText, "(?i)friendly name = (.+)", 3)
If @error <> 0 Then 
    ConsoleWrite("!> No matches or bad pattern" & @LF)
    Exit
EndIf
For $i=0 To UBound($arResult)-1
    ConsoleWrite(StringFormat(" [%02d] %s\n", $i, $arResult[$i]))
Next
Wow you are great thanks a lot. :P

Share this post


Link to post
Share on other sites
jslegers

Hi,

Is it also posible to use an operator in StringegExp.

For example :

$arResult = StringRegExp($szFileText, "(?i)friendly name = (.+)", 3) NOT StringRegExp($szFileText, "(?i)Mountpoint = none (.+)", 3)

John

Share this post


Link to post
Share on other sites
SmOke_N

Hi,

Is it also posible to use an operator in StringegExp.

For example :

$arResult = StringRegExp($szFileText, "(?i)friendly name = (.+)", 3) NOT StringRegExp($szFileText, "(?i)Mountpoint = none (.+)", 3)

John

http://www.regular-expressions.info/

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.