Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Batch file or AHK script to swap out a single line in an .ini file?  (Read 1811 times)

0 Members and 1 Guest are viewing this topic.

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9272
  • Last login:July 24, 2025, 02:05:45 pm
  • ...
I'm looking for an example of a batch file or AHK script to swap out a single line in an .ini file?
I found a couple of batch files online, but they seem more complicated than they should be.


nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Batch file or AHK script to swap out a single line in an .ini file?
« Reply #1 on: December 02, 2012, 12:14:55 am »
Here's how I would do it with ahk. Say we're working with mame.ini. Have mame.ini be one setting (the one you're currently using) and mame_alt.ini be your alternate. The difference between the two files is just that line you want to change.

Then your script would be this:

Code: [Select]
FileCopy, mame.ini, temp_mame.ini ; this backs up your current setting.

FileCopy, mame_alt.ini, mame.ini, 1 ; this overwrites the mame.ini with your alternate setting.

FileMove, temp_mame.ini, mame_alt.ini, 1 ; this makes your original setting the new alternate.

Everytime you run this script it changes the ini file to your other setting.

There are other ways you could do it, but I think this is the most straight forward.

Hope this helps! :cheers:

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9272
  • Last login:July 24, 2025, 02:05:45 pm
  • ...
Re: Batch file or AHK script to swap out a single line in an .ini file?
« Reply #2 on: December 02, 2012, 09:38:55 am »
Thanks for replying.  I originally did it that way (for a different issue), but changes made to Mala while it was running would not be saved because they would be overwritten by the old .ini file.  It became annoying to have to disable the script and redo the alternate .ini file. 

I was trying to avoid a long explanation of everything I have going on that this could solve.  :P

Here are the problems being able to do this would address for me:

Some of the Taito Type X games will not run if Mala is running, so I've made an autohotkey script that kills Mala, launches the game, then relaunches Mala on game exit.
Add to that a rotating monitor that Mala has to stay oriented correctly with. 
The orientation issue was fixed via a Da'OldMan creation, malaori3, which he graciously modified to suit my purposes.  It overwrites the lines in the .ini file responsible for orientation.  However, since Mala is killed instead of shut down properly, Mala is not able to write the gamelist and position I was at to the .ini file.
So when Mala is relaunched, it will be on the gamelist and game that it was on last time it was shut down properly instead of where it was when the game was launched.

If I were able to change those lines of the .ini file as part of my script for launching the game and relaunching Mala, it could take me back to game Mala had highlighted before being killed.  This would be different for each of the four games that I had to kill Mala to launch.
Add to that the startup video needing to be disabled, and I'd have 5 .ini files that I would need to make changes to any time I made a change in Mala's options menu.

It would simplify things immensely if I could just have each script swap in the things it needs to.

I'm having a hard time understanding exactly what I need to change in the examples I've found online.
http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace
https://irfanview-forum.de/showthread.php?t=3263

I also don't know if the fact that I need to change lines that contain "=" will mean that it needs to be handled in a special way.

:dizzy:

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: Batch file or AHK script to swap out a single line in an .ini file?
« Reply #3 on: December 02, 2012, 11:33:08 am »
Have you checked out IniWrite for AHK?

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9272
  • Last login:July 24, 2025, 02:05:45 pm
  • ...
Re: Batch file or AHK script to swap out a single line in an .ini file?
« Reply #4 on: December 02, 2012, 12:36:14 pm »
Have you checked out IniWrite for AHK?

I never dreamed it would be that simple!
I'd been searching for how to swap text in a txt file.
Never came accross that ahk command.

One friggin' line and it works perfectly.
I can think of a lot of other uses for this too.
 :notworthy:

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Batch file or AHK script to swap out a single line in an .ini file?
« Reply #5 on: December 02, 2012, 02:13:25 pm »
Glad you got it worked out, but...D'oh! :banghead: I should have asked about the ini file. For a standard ini file IniWrite is obviously the way to go. I was thinking of Mame's ini file for which you would need something like the method I suggested.