IniRead - How to search for all keys, and add them to a text file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

IniRead - How to search for all keys, and add them to a text file?

Post by Barnaby Ray » 23 May 2022, 09:10

Hi, please could anyone help me with this?

How would I go about reading an ini file, then creating a text document with each key from the ini file on a new line?

The problem is the ini file will always contain different things so I cant make it search for specfic sections or keys, I just want it to search the whole ini file, and FileAppend each key to a text file.

Is this possible? Thanks

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: IniRead - How to search for all keys, and add them to a text file?

Post by mikeyww » 23 May 2022, 09:22

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
out  = %A_ScriptDir%\out.txt
Loop, Read, %ini%, %out%
 FileAppend, % (pos := Instr(A_LoopReadLine, "=")) ? SubStr(A_LoopReadLine, 1, pos - 1) "`n" : ""
Run, %out%

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: IniRead - How to search for all keys, and add them to a text file?

Post by Barnaby Ray » 23 May 2022, 09:47

Thank you! That does exactly what I asked ... unfortunately i didn't explain what I wanted properly.. sorry!
So this saves the names of the keys to the text file.. what I actually wanted was the values of each key to be added to the text file. Sorry!

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: IniRead - How to search for all keys, and add them to a text file?

Post by mikeyww » 23 May 2022, 09:54

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
out  = %A_ScriptDir%\out.txt
Loop, Read, %ini%, %out%
 FileAppend, % (pos := Instr(A_LoopReadLine, "=")) ? SubStr(A_LoopReadLine, pos + 1) "`n" : ""
Run, %out%

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: IniRead - How to search for all keys, and add them to a text file?

Post by BoBo » 23 May 2022, 10:08

The INI-command allows extracting complete sections & section names, you only need the INI's filename. JFTR

Code: Select all

#SingleInstance, Force

IniRead, sectionNames, my.ini
ini := StrSplit(sectionNames,"`n")
for each, sectionName in ini
   {  IniRead, section, my.ini,% sectionName
      FileAppend, % StrSplit(section, "=").2 "`n", my.txt
   }

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: IniRead - How to search for all keys, and add them to a text file?

Post by Barnaby Ray » 24 May 2022, 07:55

Thanks very much guys, I'm almost there - but now have a couple of other questions..

I'll try to briefly explain what I'm trying to do and show the code I have so far...

I have a web page with a bunch of audio tracks - each one has an audio player and information regarding the track - like artist name, track title, etc etc.

I need to listen through the tracks, and put together a message with any problems I find. There are about 100 pre typed messages for the various problems, some just need the message, and some need track information added to the message. To further complicate things, sometimes the track info needs to be at the end of the message, and sometimes its in the middle, and also different problems need different bits of information included.

To save me having to keep going back and forth between pages, adding the message after every problem, I want to store all the problems and add them to the message at the end, after I've gone through all the tracks.

I had it working using arrays, so I had about 100 arrays, each time you find a problem, you select the track info and open a gui with a menu of all the problems - you select the right problem, and it adds the info to an array. Then at the end, it searches all the arrays - any that have info in them get added to the message.

It worked - kind of, but it was too slow and unreliable - especially when you have lots of problems.

So now I'm trying to do it with an ini file instead..

So I find a problem, select the track info, select the problem from the gui, and it FileAppends the info to a text file. A separate text file gets created for each problem type, so each time you add a problem, it gets added to its text file. At the same time, the messages for each problem get added to a comp ini file. At the end, it grabs the track info from the text files, and adds them to the comp ini file.. then the comp ini file gets converted to a new text file, which can then be added to the final message box.

Here's the code so far...
So this would be for one of the problems..

;This first bit copies the track info and stores the bits I need as variables:

Code: Select all

	Gui, Destroy
	sleep, 50
	Clipboard := ""
	winactivate, ahk_exe chrome.exe
	^5
	sleep,50
	Send, ^c
	ClipWait, 2
	data1 := clipboard
	arr1 := StrSplit(data1, "`n", "`r")
	TrackNum := arr1[1]
	RegexMatch(data1, "\b\w{5}\d{7}\b", code)
	Ctime := arr1[arr1.MaxIndex()]
;Now a Msgbox shows the info to check its copied correctly. If it is, the track info is stored as variable %TrkInfo%, and then added to a text file. Also the message for this problem is added to the comp ini file, and the track info gets added also.

Code: Select all

	MsgBox, 262145,Add Action? ,Audio Quality - Track %TrackNum% (%code%).
	IfMsgbox cancel
        Exit
	TrkInfo = Track %TrackNum% (%code%)|

	IniRead, Var1, %UAInipath%, AudioQualityAction, Message1
	IniRead, Var2, %UAInipath%, AudioQualityAction, Message2
	IniRead, Var3, %UAInipath%, AudioQualityAction, Info1
	IniRead, Var4, %UAInipath%, AudioQualityAction, Message3

	IniWrite, %Var1%, %UACompinipath%, AudioQualityAction, Message1
	IniWrite, %Var2%, %UACompinipath%, AudioQualityAction, Message2
	IniWrite, %Var3%, %UACompinipath%, AudioQualityAction, Info1
	IniWrite, %Var4%, %UACompinipath%, AudioQualityAction, Message3

	FileAppend, %TrkInfo%, AudioQuality.txt
	FileRead, TrkInfo2, AudioQuality.txt
	IniWrite, %TrkInfo2%, %UACompinipath%, AudioQualityAction, Info1
	return
; Then at the end, I run this part of the script to add the keys from the comp ini file to a new text file:

Code: Select all

ini := StrReplace(UACompinipath, ".ahk", ".ini")
out  = %A_ScriptDir%\out.txt
Loop, Read, %ini%, %out%
 FileAppend, % (pos := Instr(A_LoopReadLine, "=")) ? SubStr(A_LoopReadLine, pos + 1) "`n`n" : ""
Run, %out%[/code]

:This is now almost working perfectly.. if I run a couple of problems, the text file I end up with looks like this:

[Codebox=text file=Untitled.txt]Unfortunately, the audio quality for this release is not to a high enough standard. Audio recordings must be mixed, mastered and recorded to a professional level. This includes all components of your track(s). Please upload your tracks again.

This is in reference to the following tracks:

Track 01 (GX4R52297054)|Track 02 (GX4R52297056)|Track 05 (GX4R52297065)|

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

Unfortunately, some of your audio files have failed to upload, meaning that the release has not yet been moderated. We apologise for any delay that this may have caused with your release, please check the files that are missing and re-upload them using our audio upload tool.

This is in reference to:

Track 03 (GX4R52297061)|Track 04 (GX4R52297064)|

All tracks must be uploaded as MP3’s at 320 kbps or FLAC at 1411kbps. Sample rate must be 44.1 khz.`n`nOnce you have uploaded the files again, please check your track numbering as this could now be incorrect.

[STILL AWAITING MODERATION]

-------------------------------------
So the last couple of things I need to figure out are, how can I add line breaks between the track inforamtion? I need them to be added to one key of the comp ini, so cant add the line breaks first, and also the line breaks dont work within the ini file... I've tried adding '|' after each bit of info, as this is where I want to split them ie, Track 01 (GX4R52297054)|Track 02 (GX4R52297056)|Track 05 (GX4R52297065)| should be
Track 01 (GX4R52297054)
Track 02 (GX4R52297056)
Track 05 (GX4R52297065)
I'm not too sure how to do that and have been trying all morning :/ Not sure if it should be done before it gets added to the final text file, or if I should loop parse the text file and do it at the end?

The other thing is, ideally I want the Track numbers in the final message to be in bold type. Before when I used the arrays, I could add the track info like; ^bTrack 01 (GX4R52297054)^b to make it bold.. but now this doesn't work as it adds the ^b into the text file rather than making it bold... is there a way to do that afterwards? Again i'm not sure if it should be done before it gets added to the final text file, or if it can be done at the end?

Sorry for such a long post!


[Mod edit: Changed code boxes with AHK code from .txt format to the default .ahk format.]
Last edited by Barnaby Ray on 24 May 2022, 08:01, edited 1 time in total.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: IniRead - How to search for all keys, and add them to a text file?

Post by mikeyww » 24 May 2022, 08:00

Looks long, and also a different question? Sorry I am traveling & cannot reply. Others may be able to help. Best wishes!

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: IniRead - How to search for all keys, and add them to a text file?

Post by BoBo » 24 May 2022, 15:43

@Barnaby Ray
I had it working using arrays, so I had about 100 arrays, each time you find a problem, you select the track info and open a gui with a menu of all the problems - you select the right problem, and it adds the info to an array. Then at the end, it searches all the arrays - any that have info in them get added to the message.

It worked - kind of, but it was too slow and unreliable - especially when you have lots of problems.
Would you mind posting that script? Just out of curiosity.

I'd tend to an array-based solution over an Ini-based one. And you've provided a lengthy description but no sample (before&after) "source file" (that clipboard events original content) :eh: IMHO the most important thing, even if it's an annoying task to create 'original' dummy files/data sets (just in case you don't want to show sensitive data).

Code: Select all

trackLine := "Track 01 (GX4R52297054)|Track 02 (GX4R52297056)|Track 05 (GX4R52297065)|"
MsgBox % trackBlock := StrReplace(trackLine,"|","`n") ; would do this conversion right after the string has been created.

Regarding the 'boldness' of your Track-data, you could save your output as *.rtf that allows formatting, while it is still plain text:
https://www.autohotkey.com/boards/viewtopic.php?p=251347#p251347
https://interoperability.blob.core.windows.net/files/Archive_References/[MSFT-RTF].pdf
OTOH, creating your output in HTML looks easier to me.

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: IniRead - How to search for all keys, and add them to a text file?

Post by Barnaby Ray » 26 May 2022, 05:00

@Bobo - thanks! I've got it pretty much all working now with the ini version, still just trying to get my head around the rtf / html options for the bold formatting, but other than that, it works super fast.

I will post both versions as soon as I get a spare minute, as it would be interesting to get another opinion on how it works.. I'm sure I could have improved the array version, and in some ways that way was better as I could easily keep the bold formatting.. but this new way with saving to text files does seem to be much faster...

I will add both scripts asap. Thanks again!

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: IniRead - How to search for all keys, and add them to a text file?

Post by BoBo » 26 May 2022, 06:07

Would you mind providing a "worst case/mixed up" input sample and a finally converted/sorted output sample AKA "something to play with"? :)

Barnaby Ray
Posts: 45
Joined: 09 Nov 2021, 07:48

Re: IniRead - How to search for all keys, and add them to a text file?

Post by Barnaby Ray » 27 May 2022, 15:00

@BoBo - sorry.. yes I will upload this asap.. I tried to post it yesterday and ended up deleting the post as I was trying to send it :/

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: IniRead - How to search for all keys, and add them to a text file?

Post by boiler » 27 May 2022, 15:51

@Barnaby Ray - When posting AHK code, please use the AHK formatted code box (not .txt format), whether from the dropdown list or using the [code][/code] tags that are inserted by clicking the “Code” button. Thanks!

Post Reply

Return to “Ask for Help (v1)”