emendelson Posted January 7, 2018 Posted January 7, 2018 I'm trying to write a script that will read an old Windows 3.x SETUP.INF file and find the name of a Program Manager group file for the language of the Windows 3.x setup. SETUP.INF has two lines that look like this (both lines will be the same): group1=StartUp The string "StartUp" is different in non-English versions. Is there a straightforward to find the first (or second) instance of the string "group1=" and then read the remaining characters up to the end of the line, so that I can use that string as a variable? The string could be any length, and I don't know in advance how many characters it will have. I thought of using this method of finding the first line number in the file, and then using FileReadLine: https://www.autoitscript.com/forum/topic/99081-get-line-number-of-string-in-text-file/?do=findComment&comment=711876 But I hope there's a simpler way. I simply can't figure out what it might be. I'll be grateful for any help.
mikell Posted January 7, 2018 Posted January 7, 2018 The fastest way $txt = "group1=StartUp" & @crlf & "group1=StartUp" Msgbox(0,"", StringRegExpReplace($txt, '(?s)group1=(\N+).*', "$1") ) Else you can FileReadLine the first line and then use StringSplit with "=" as delimiter
emendelson Posted January 7, 2018 Author Posted January 7, 2018 (edited) Thank you - but I think I must be missing something, because I can't figure out how to use that code to find out what comes after "Group1=" in the two lines (which are not adjacent in the file - they're widely separated; there's a lot more than a @CRLF between them). Probably I'm just being dense, but maybe I didn't ask the question clearly enough. I'm not trying to replace "StartUp" with something else. I'm trying to read what follows "Group1=" on either of the two lines that begin with "Group1=". In a US English system, the string after "Group1=" is "StartUp." But in a Dutch system it will be "Opstarten". It will be something else in a French, German, Danish, Italian, etc. system. I'm trying to read the line so that I can find out that that string might be in the system that the script is running on. Again, if your code will do that, apologies for being dense, but I couldn't figure out how to put it into effect. Edited January 7, 2018 by emendelson
iamtheky Posted January 7, 2018 Posted January 7, 2018 this? #include<array.au3> $txt = "group1=StartUp" & @crlf & "some other stuff" & @CRLF & "more stuff" & @CRLF & "group1=Opstarten" _ArrayDisplay(StringRegExp($txt, 'group1=(.*)', 3)) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
emendelson Posted January 7, 2018 Author Posted January 7, 2018 I must be being dense, because I still don't understand. Here's the problem again. This is part of the Windows 3.1x setup.inf for Dutch Windows: ; This section is used for upgrading from 3.X to 3.1. [new.groups] group7=Hoofdgroep,1 group2=Bureau-accessoires group8=Netwerk group6=Ontspanning group1=Opstarten ; This section is for new install. [progman.groups] group3=Hoofdgroep,1 group4=Bureau-accessoires group8=Netwerk group5=Ontspanning group1=Opstarten There are about a dozen versions of this file out there, maybe more. I don't know what the contents will be. I'm trying to read whatever version of the file happens be on the disk, and read what comes after "group1=". It could be any of a dozen different things, and I don't know in advance what it might be. All the strings after "group#=" will be different in different versions of the file. Again, I'm not trying to replace anything. I want to put the string after group1= into a variable so I can use it elsewhere. Apologies again for being unclear in the question, and thanks for any and all replies!
iamtheky Posted January 7, 2018 Posted January 7, 2018 do you not see the array with everything after group1? are you having an issue reading that file into a string or setting array elements to variables? ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
kylomas Posted January 7, 2018 Posted January 7, 2018 (edited) emendelson, Using mikell's slightly modified sre ... local $group1 = StringRegExpReplace(fileread(@scriptdir & '\group1.txt'), '(?s).*group1=(\N+).*', "$1") ConsoleWrite($group1 & @CRLF) Test file...group1.txt kylomas Edit: Do any of the files have multiple instances of "group1=" with different values? Edited January 7, 2018 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
emendelson Posted January 8, 2018 Author Posted January 8, 2018 (edited) OK, I finally understood what you were asking me to do, but there are still problems, because this code reports the wrong line numbers in files with blank lines: #include <Array.au3> #include <File.au3> Local $sFileName = "setup.inf" Local $searchStr = "group1=" $aResult = _LineNumsOfSearchStr($sFileName, $searchStr) _ArrayDisplay($aResult) ; Returns array of found line numbers that contain $searchStr contents. ; Optional delete found line from file. Func _LineNumsOfSearchStr($sFileName, $searchString) Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = "" If FileExists($sFileName) = 0 Then Return 1 Do $sFile = FileRead($sFileName) $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring" If $location > 0 Then $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number ;ConsoleWrite("CharPos: " & $location & " Ln: " & $iCurrentLineNum & @CRLF) $sRes &= $iCurrentLineNum & "|" $iOccur += 1 Else ExitLoop EndIf Sleep(10) Until 0 ;ShellExecute($sFileName) Return StringSplit(StringTrimRight($sRes, 1), "|") EndFunc ;==>_LineNumsOfSearchStr ; I can get the correct numbers by removing the blank lines, but I'm not sure what the most efficient way of doing this might be. I've attached the file I've been working with. Apologies for being so dense before - I couldn't figure out why the results were all wrong. Setup.INF Edited January 8, 2018 by emendelson
careca Posted January 8, 2018 Posted January 8, 2018 Local $Read1 = IniRead(@ScriptDir&'\Setup.INF', 'new.groups', 'group1', '') Local $Read2 = IniRead(@ScriptDir&'\Setup.INF', 'progman.groups', 'group1', '') MsgBox(64, '', $Read1&' - '&$Read2) because, you know, the syntax of the file is just like an ini. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
emendelson Posted January 8, 2018 Author Posted January 8, 2018 (edited) 1 hour ago, careca said: Local $Read1 = IniRead(@ScriptDir&'\Setup.INF', 'new.groups', 'group1', '') Local $Read2 = IniRead(@ScriptDir&'\Setup.INF', 'progman.groups', 'group1', '') MsgBox(64, '', $Read1&' - '&$Read2) because, you know, the syntax of the file is just like an ini. Yes!! Thank you! And here I've been wasting everybody's time with complicated solutions that didn't work. This is perfect. Thank you again! (And to answer an earlier question, I think the two strings are always identical.) Edited January 8, 2018 by emendelson
careca Posted January 8, 2018 Posted January 8, 2018 Great, happy i could help. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now