0

I have been tasked to edit multiple configuration files on my company's proprietary software (Windows OS). These configuration files are in INI file format (config.ini) which structures are composed of sections, properties, and values. The requirement is to:

  1. Search for the section name and remove all corresponding properties on config.ini file.

    Example: Remove entire section [RegistryService] and its properties.

    [DummyProcessor]
    CCLTsVersion=112
    ETransformsDescription=
    ETransformsVersion=0.0.0.0
    LWTs=21.10.25
    Transform=10.2.2.0
    [RegistryService]
    LoadRegistry=1
    
  2. Delete an entry from a different configuration file (not limited to section):

    Example: Delete just line with LoadRegistryManager=1 entry from:

    [DummyService]
    InitInstructions=0
    ESAPsVersion=
    ESVersion=10.2
    LoadRegistryManager=1
    

Can I use Windows command line batch scripting to make these edits?

Please provide an example. I am more comfortable with Linux commands and not as privy to Windows batch scripting aside of creating/deleting files and folders.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • If bash is in your wheel house then use Cygwin or UnixUtils. If you are on Windows 10 you can install the Linux Subsystem which is Ubuntu based. Regardless of that, you need to at least make an attempt at coding it and we will help you with the code from there. This is not a free code writing service. – Squashman Oct 27 '17 at 17:48
  • When you were tasked with this job, what did you do? nothing! Not doing anything could potentially lose you your job, so that seems implausible. You want us to write code to perform a task you are paid to do, quicker and with ease but for no remuneration and haven't yet had the courtesy to show us the steps you've taken thus far towards that goal. You used the batch-file tag, so that shows that you've already decided on a specific scripting language. What you need to do now, if you haven't done so, is write the script, try it and post here with it's content and details should it fail somehow. – Compo Oct 28 '17 at 10:49
  • Although the question is not a duplicate, [this answer](https://stackoverflow.com/a/29691444/6738015) using PowerShell may prove useful in this case. – Compo Oct 28 '17 at 12:55

1 Answers1

0

The question How can you find and replace text in a file using the Windows command-line environment? has lots of answers. One of those answers links to JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid making it possible to use regular expressions as supported by JScript from Windows command line.

The requirements can be fulfilled with jrepl.bat and following batch file both stored together in same directory:

@echo off
call "%~dp0jrepl.bat" "^\[RegistryService\][^[]+" "" /M /F config.ini /O -
call "%~dp0jrepl.bat" "^LoadRegistryManager=[01].*\r\n" "" /X /F config.ini /O -

The regular expression search string ^\[RegistryService\][^[]+ searches case-sensitive for [RegistryService] at beginning of a line and matches everything after this string up to next opening square bracket [ or end of file.

Note: This search string as is can't be used if one of the entries in section [RegistryService] contains by chance an opening square bracket.

The regular expression search string ^LoadRegistryManager=[01].*\r\n searches case-sensitive for a line starting with LoadRegistryManager= followed by digit 0 or 1, 0 or more characters except newline characters and carriage return and line-feed.

The replace string is for both replaces an empty string to delete everything matched by the search expression.

The first replace requires Multi-line mode enabled with /M. The second replace requires eXtended ASCII and escape sequences enabled with /X.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • jrepl.bat /?
Mofi
  • 46,139
  • 17
  • 80
  • 143