Sign in to follow this  
Followers 0
koenp

read string at the after a search in a text.

8 posts in this topic

Hello everybody,

I am wordering if somebody could bring into the right direction with the following..

1. search for a string in a txt file.

2. get the value after that is after the search.

example:

content of *.txt file:

MS SBS2011 5CAL extra HP PRoLiant ML150
G6 RAM 4x 4Gb HP HDD 1TB
Number 1 1 1 4     672.78 222.00
1,104.40 274.08 413.28 222.00 300.00 3,208.54
Sample ID: 7009146201

----------------------------------------------------------

What I need to find in the file is in green.

Is this possible with autoit?

Thanks a lot in advance for any tips that is putting me on the right track.

Kindly Regards,

Koen

Share this post


Link to post
Share on other sites



#include <Array.au3>
$re = StringRegExp(ClipGet(), 'Sample ID:\s+(\d+)', 3)
_ArrayDisplay($re)

ClipGet because I copied your "file content" to my clibboard for testing.


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Thanks a lot for your help.

However when I try your code I get the as result 1...

Here is the code I used...

#include <Array.au3>
$file = "D:\test.txt"
ClipPut ($file)

$re = StringRegExp(ClipGet(), 'FLEXLABR:\s+(\d+)', 3)
_ArrayDisplay($re)
MsgBox(64,"",$re)

Any other ideas?

Thanks

Koen

Share this post


Link to post
Share on other sites

That is very possible using AutoIT :)

Are the text file you want to search from always fixed name and location? Or you want to search on specific folder for *.txt on that folder?

Share this post


Link to post
Share on other sites

Try this:

#include <Array.au3>
$file = "D:\test.txt"
$re = StringRegExp(FileRead($file), 'FLEXLABR:\s+(\d+)', 3)
_ArrayDisplay($re)
MsgBox(64,"",$re)

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks UEZ,

this  is working how ever i need to have the result in a variable because after this I want to rename a file with this variable..

The origenal name of the text fiel ( test.txt) will always be the same.

To explain the hole process.

I actually converting a file called test.pdf to test.txt.

Inorder to retrieve the SIDname ( thanks to your script), knowing this I then want to rename the test.pdf into "$re.pdf".

Best regards and already a big thanks for this script..

Koen

Share this post


Link to post
Share on other sites

You can read out the result from the array and use it to rename your file.

Something like this here:

FileMove("test.txt", $re[0] & ".pdf")

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks a lot UEZ,

You saved my day :-)

Here is the complete code as an example for maybe somebody else..

#include <Array.au3>
#include <Date.au3>

Global $config_file  = @ScriptDir & "\etc\config.ini"
Global $rcvname = IniRead($config_file, "CONFIG", "rcvname","")
Global $pathtxt = IniRead($config_file, "CONFIG", "pathtxt","")
Global $orgnamepath = IniRead($config_file, "CONFIG", "orgnamepath","")
Global $finamepath = IniRead($config_file, "CONFIG", "finamepath","")
Global $prefixP = IniRead($config_file, "CONFIG", "prefixP","")
Global $idenP = IniRead($config_file, "CONFIG", "idenP","")
Global $prefixQ = IniRead($config_file, "CONFIG", "prefixQ","")
Global $idenQ = IniRead($config_file, "CONFIG", "idenQ","")
Global $prefixC = IniRead($config_file, "CONFIG", "prefixC","")
Global $idenC = IniRead($config_file, "CONFIG", "idenC","")



;==================================================
; Convert pdf to txt
RunWait(@ScriptDir & "\etc\pdftotext.exe " &  $orgnamepath & "\" & $rcvname & ".pdf")
Global $file = $pathtxt & "\" & $rcvname & ".txt"
Global $date = @MDAY & @MON & @YEAR
DirCreate($finamepath & "\" & $date)
$finamepath = $finamepath & "\" & $date

;==================================================
;Check QC Patient or Calibration
Global $QCcheck = StringRegExp(FileRead($file), 'QC Product:\s+(\S+\s+\S+)', 3)
;Msgbox(64,"QC",@error)
If @error = 0 Then _QC_result()

Global $PATcheck = StringRegExp(FileRead($file), 'Gender:', 3)
;Msgbox(64,"PAT",@error)
If @error = 0 Then _Patient()

;_Calibratie()


;==================================================
; QC result
Func _QC_result()
Global $SampleID = StringRegExp(FileRead($file), 'Sample ID:\s+(\S+)', 3)
Global $Level = StringRegExp(FileRead($file), 'Level\s+(\S+)', 3)
Global $QCprod = StringRegExp(FileRead($file), 'QC Product:\s+(\S+\s+\S+)', 3)


;_ArrayDisplay($SampleID)
;_ArrayDisplay($Level)
;_ArrayDisplay($QCprod)
$LevelS = "Level-" & $Level[0]
FileMove($orgnamepath & "\" & $rcvname & ".pdf", $finamepath & "\" & $prefixQ &  $SampleID[0] & "_" & $LevelS & "_" & $QCprod[0] & "_" & $date & ".pdf",1)
FileDelete($file)
sleep(500)
Exit
EndFunc


;==================================================
;PATIENT result
Func _Patient()
Global $re = StringRegExp(FileRead($file), 'Sample ID:\s+(\d+)', 3)
;_ArrayDisplay($re)
FileMove($orgnamepath & "\" & $rcvname & ".pdf", $finamepath & "\" & $prefixP &  $re[0] & ".pdf",1)
FileDelete($file)
sleep(500)
Exit
EndFunc




;==================================================
;CALIBRATION result
Func _Calibratie()
Global $dateT = @MDAY & @MON & @YEAR & "_" & @HOUR & @MIN & @SEC
FileMove($orgnamepath & "\" & $rcvname & ".pdf", $finamepath & "\" & $prefixC & "_" & $dateT & ".pdf",1)
FileDelete($file)
sleep(500)
Exit
EndFunc

 

 

 

Best regards

Koen

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  
Followers 0