What should I use for flexible but effective editing of ini files?

  Kiến thức lập trình

Options:

  • Manually edit files (cue the meme “NO! GOD PLEASE NO! NOOOO!”).
  • Use REGEX to split variable names and values into columns (tab will do best for manual copy paste), then paste into a spreadsheet, drag & drop cells or edit text, paste all back into TXT, REGEX back to INI format
  • Make a mini-software to automate some parts of the previous idea, with some GUI that I can modify with any new ideas. Which programming language would be suitable for that?

Note:
All REGEX examples here are working as expected. I’m NOT asking for help with REGEX.
Some of them use more advanced features, e.g. negative lookahead.
They are here only for completeness.
The syntax is for Notepad++ using
Boost regular expression library v1.80, originally based on PCRE

Simple example, easy to do by REGEX: change all entries that point to the drive letter F and change them to drive D.

Not so simple: If I want to move a section, as if I would want to select a block of items and drag & drop them, that can’t be done by regex because it doesn’t conceptually understand numeric values.

Same problem if I want to insert a new item somewhere in the middle.
All items after that will move down by 1 position. Again, like drag & drop everything. After creating a new index at the end.

If you’d want to do something like this
AND
you would not need to do it daily (maybe once a month)
would you make a program for it?

Which programming language would you use?

I like to do stuff in a way that lets me see the results immediately, e.g. when I use regex to replace something. But some of this would need to be put into an indexed array for easy modification.

Example content of a file, but it can be completely different, something that has no paths or mixed:

menu9=_E-pic-Screenshots
cmd9=cd E:MyDocspics____Screenshots
menu10=_E-ToDo
cmd10=cd E:MyDocs_ToDo
menu11=_D-ToDo
cmd11=cd D:MyDocs_ToDo
menu12=_E-Bike
cmd12=cd E:MyDocsHardware_Bike
menu13=_D-Bike
cmd13=cd D:MyDocsHardware_Bike

menu39=--
menu40=-Shopping
menu41=_E-Shopping
cmd41=cd E:MyDocs_Business_Shopping
menu42=_D-Shopping
cmd42=cd D:MyDocs_Business_Shopping
menu43=--

menu51=--
menu52=-Accounting
menu53=_E-TAX
cmd53=cd E:MyDocs_Business_AccountingTAX
menu54=_D-TAX
cmd54=cd D:MyDocs_Business_AccountingTAX

Different example:

menu40=-Tools
menu41=50 percent separator
cmd41=cm_50percent
menu42=Directory Hotlist - CTRL-D
cmd42=cm_DirectoryHotlist
menu43=Directory History List - ALT-DOWN
cmd43=cm_DirectoryHistory
menu44=-On Default bar
menu45=Path-CopyCurrent-OR-TypeDestination
cmd45=cd
param45=?%P
key45=5
menu46=--
menu47=Dialogue box - file name under cursor
cmd47=cd
param47=?%N
menu48=Dialogue box - path and file name under cursor
cmd48=cd
param48=?%P%N
menu49=-On cmdLaunch Bar
menu50=Files - Set Attributes  -  Change Date-Time - ALT-D - C
cmd50=cm_SetAttrib

menu63=--
menu64=-ICONS
menu65=attrib -r +s %n with directory AT CURSOR to show icon - CTRL-ALT-F6
cmd65=attrib -r +s
param65=%n
key65=6
menu66=UNDO directory AT CURSOR to show icon - attrib -s %n
cmd66=attrib -s
param66=%n

Example, I could use
find
^(w+=_)(F)(-[^rn]+)rn(w+=cd )2(:\[^rn]+)$
replace by
$1D$3rn$4D$5
and replace all matches by a manual search in a text editor like Notepad++ to change all entries that point to the drive letter F and change them to drive D.

Simple.

Move a section:
first split the variable names and values into columns, e.g.
find
^(w+)=([^rn]+)$
replace by
$1t$2
Then copy everything into a spreadsheet, move stuff around by drag & drop (only variable values, not names),
then fix it if there’s some mismatch (e.g. some entries have only 1 row, some have 2 with the same index, some have 3 or 4, this might mess things up)
pray 3 times that I didn’t mess it up, copy back to TXT, then
find
^(w+)t([^rn]+)$
replace by
$1=$2
to join the columns back to INI format.

Another way to do it, probably better, would be to remove the variable names by regex, but so that based on the type of the variable name the variable value would be in different columns.
find


^menud+=([^rn]+?)rn
(?:cmdd+=(?!^menu[^rn]+)([^rn]+)rn)?
(?:paramd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)([^rn]+)rn)?
(?:pathd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)(?!^param[^rn]+)([^rn]+)rn)?
(?:keyd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)(?!^param[^rn]+)(?!^path[^rn]+)([^rn]+)rn)?

not really, that needs to be in one row (in 5 rows only to show the structure), so find:

^menud+=([^rn]+?)rn(?:cmdd+=(?!^menu[^rn]+)([^rn]+)rn)?(?:paramd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)([^rn]+)rn)?(?:pathd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)(?!^param[^rn]+)([^rn]+)rn)?(?:keyd+=(?!^menu[^rn]+)(?!^cmd[^rn]+)(?!^param[^rn]+)(?!^path[^rn]+)([^rn]+)rn)?

replace by
$1t$2t$3t$4t$5rn

Example:
This

menu4=Notepad++ (IF already launched THEN show it ELSE load last session into Default session) with it's own start path
cmd4=""C:Program Files (x86)Notepad++notepad++.exe""
path4=C:Program Files (x86)Notepad++

and this

menu65=attrib -r +s %n with directory AT CURSOR to show icon - CTRL-ALT-F6
cmd65=attrib -r +s
param65=%n
key65=6

would be each in 1 row removing ^w+d+= and replacing rn by t in 5 columns and

menu66=UNDO directory AT CURSOR to show icon - attrib -s %n
cmd66=attrib -s
param66=%n

would fill only the first 2 and 4th column because there’s no path66 and key66.
This would solve the issue that some items are in multiple rows.
Then after copy & paste into a spreadsheet and then drag & drop and copy back to text,
I could use different versions of regex to return to INI format for 1 to 5 filled columns.
Or use 1 regex
find
^(d+)t([^trn]*?)t([^trn]*?)t([^trn]*?)t([^trn]*?)t([^trn]*?)$
replace by
menu$1=$2rncmd$1=$3rnparam$1=$4rnpath$1=$5rnkey$1=$6
that creates empty parameters like
key=
in the part

menu=UNDO directory AT CURSOR to show icon - attrib -s %n
cmd=attrib -s
param=%n
path=
key=

Then insert parameter name index numbers – I could use Notepad++ column input after selecting every 4th line manually.
Then regex to find empty parameters and remove them
find
^(w+)=rn
replace by nothing

Better:

On the spreadsheet include an index column and copy that with the rearranged variable values back to TXT. Then I could use regex to put that number to each variable name.
find
^(d+)t([^trn]*?)t([^trn]*?)t([^trn]*?)t([^trn]*?)t([^trn]*?)$
replace by
menu$1=$2rncmd$1=$3rnparam$1=$4rnpath$1=$5rnkey$1=$6
and then remove empty variable rows.

Still cumbersome but vastly better than pure manual edits.

What would you do?

New contributor

BitByBit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT