Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Changing values in config files with a bat file

Posted on 2008-10-24
Medium Priority
Last Modified: 2012-06-21
I have written a bat file that will change some config settings for me on another file on the machine.
I found a useful tool called change.com from googling the net which allows me to do this but it is limited in that it has no wildcard function and the value I am changing is a variable between 0 and 255 with 2 channels so I end up with an extremely large file which does work but I'm wondering if there is a better way to do this? The change function changes the first value to the second ONLY if the first function matches one in the program.

change FolderA\settings.ini "left=255" "left=0"
change FolderA\settings.ini "left=254" "left=0"
etc all the way down to left=1 then I have another for right=255 down to right=0

I also don't know how to do this if the bat files are located in a sub folder to the one I want to change - do they have to be in a folder above the one you want to change? It also wouldn't work if the bat files were inside their own folder even if senior - I'm guessing this is my limited knowledge of file paths etc.
Any input from you more senior programmers would be appreciated :D
Question by:deathbybatfile
  • 6
  • 4
LVL 86

Expert Comment

ID: 22795696
What *exactly* are you trying to do? Just set the value "left" (and "right") to 0?
Try iniman.exe from the W2k3 Resource Kit.
iniman.exe "FolderA\settings.ini" <Section>.left="0"
iniman.exe "FolderA\settings.ini" <Section>.right="0"
Replace <Section> with the section name in which the values are defined.
As far as the folders are concerned: it doesn't really matter where the batch file is; to be on the safe side, it's easiest to always specify the full path to the file:
iniman.exe "C:\FolderA\FolderB\FolderC\settings.ini" <Section>.right="0"

Windows Server 2003 Resource Kit Tools
LVL 39

Expert Comment

ID: 22795791
OK, first of all I have to ask you to clarify exactly what it is within your target files that needs to be changed.  If you could perhaps paste a portion of the file into a new Code Snippet box (or even upload a complete text file as an attachment), and then indicate in another Code Snippet what you would like to change the file to, it would be a great help.  There may be alternative ways to do this without using 3rd party programs.

It would also be useful to know what operating system you are usining when performing this task.

In the meantime, I will try to help you understand Directory Paths.

Your easiest way to "call" a batch file is to place it in a System folder like C:\Windows, C:\Windows\System, or C:\Windows\System32 (depending on your operating system).  As long as a program is in one of the set "paths" that Windows looks in, it can be called by just using the program's filename rather than having to use the fully qualified path to it.  You can also create a new Windows Registry setting that allows you to specify the path to a program and set it as an "Application Path".  I can tell you how to do this if needed after we have a better idea what you need to do.

The easiest way to think about how file paths relate to each other is by viewing the Folder Tree in the My Computer window or in Windows Explorer (essentially the same thing), and then relate it to the way we normally notate paths, ie. what you see in the Address Bar while in Windows Explorer.  If you aren't seeing the Folder Tree in the left pane of Windows Explorer, then click on the "Folders" toolbar button, or use the View menu > Explorer Bar > "Folders".

Visually, if you change directory from a sub-folder to its containing (master) folder, you are going UP a level, and to move back from that folder into one of its sub-folders you are going DOWN a level.  Another more easily understood expression is Up = Back (up), and Down = Forward.  It's easier thinking of the folder "tree" as being the part of a tree below the ground, where the trunk (Root of the drive eg. C:\ ) is up at ground level and you are seeing the roots rather than branches.  If you visualised "branches" as the sub-folders, the metaphor would force you to imagine an upside down photo of a tree, with the trunk at the top of the screen and the branches and leaves going down the way.  That's a bit harder to picture in your mind.

Directory path notation, especially navigating a drive in DOS, isn't very visually intuitive because you only see the Current Path as the Command Prompt, and have to ask it to display other folders up or down from there.  The exception to the rule is the TREE command which existed in the last versions of proper DOS, was missed out in Windows 9x, and then came back in XP (and probably 2000).  This outputs a Directory listing in a visual layout.  For instance, the command:

tree c:\windows\system32 /f /a

would list ALL the files and sub-folders in the "system32" folder with dotted lines showing the hierarchy.  DON'T execute that command if you are in a hurry to shut down and go out, because it will take a long while.  Ctrl + Break key stops the command, just in case you want to get an idea what I'm talking about.

So, back to the topic of using commands in a batch file to go up or down (back or forward respectively) through folders.

If you have a batch file in the following directory:
and you want to place a command in it that acts on the following file:
you would have to go UP (back) from the "System32" folder into the "Windows" folder, and then Down (forward) from there into the "Config_Files" folder.

You could specify the path as this:

The .. symbol always tells the process to move UP one level, and, once there, by placing the \ at that point, you are then moving Down (forward) again.

This type of command would be useful for Changing Directory Into (CD) a named directory, but if you are using a batch file to call ANOTHER program to then do something with a named file, the path to that target file must be quoted as being FROM where the PROGRAM FILE is.  The easiest example of this is a command telling Windows Notepad to open a text file.

Notepad.exe is at:
so if you tell it to open a file, you either have to provide Notepad with the fully qualified path, or as a path that is "Relative" to Notepad.exe.

Let's assume that your text file is at:
C:\Program Files\billpad\faq.txt

Any of the following commands would tell Notepad to open that file:

C:\Windows\Notepad.exe   C:\Program Files\billpad\faq.txt
(fully qualified path to the target file)
C:\Windows\Notepad.exe   ..\Program Files\billpad\faq.txt
(path to target file by stepping up one level then going down to target file)
C:\Windows\Notepad.exe  \Program Files\billpad\faq.txt
(path to target file by going right back to root of drive then going down to target file)

In the case of Notepad.exe, you don't have to specify the path to it because C:\Windows is a system path.

OK, so you have the program CHANGE.COM.
I suggest keeping this program in the same folder as your batch file that will call it.  That way, your command just needs to start off with:
change.com (or just change) and then your parameters and switches.

The parameters you have specified in your example commands are:

CHANGE  filename.ext  "string_to_find" "replacement_string"

I assume that you have this Change.com file:

If not, then perhaps you could provide the link from where you downloaded the program.

All you then have to worry about  adding to the command is telling Change.com where the file is located that it is being asked to modify.  That path can be Relative to where Change.com is, or it may be a full path to it.

Let's assume that change.com and your batch file are in a folder:
and your text file is:
C:\Program Files\Program1\Config\settings.ini

This command in the batch file would work:

change "..\Program Files\Program1\Config\settings.ini" "left=255" "left=0"
change "\Program Files\Program1\Config\settings.ini" "left=255" "left=0"

The double-quotes around the path to the file probably wouldn't be needed in Windows XP if the change.com program is able to accept paths or file names with spaces in them.  If the quotation marks confuse cange.com into thinking that the path is one of the other parameters, then you may have to try and specify the path using DOS 8.3 names, such as:

change \Progra~1\Program1\Config\settings.ini "left=255" "left=0"

I hope this helps you understand a it more about how to issue commands in a batch file, but we really do need a bit more detail of what you need to be changed.

I'm also curious to know why, because it might well be very easy and almost fast to:
1. Open your *.ini file in Notepad
2. Fined and replace the spaces with commas
3. Save as a *.CSV file
4. Open it in Microsoft Excel
5. Select the column to be changed
6. Do a Find and Replace, create an auto-numbered list, or whatever
7. Save the file
8. Open it in Notepad and do a Find and Replace to replace commas with spaces again
9. Back up your *.ini file
10. Rename the *.csv file to the name of the *.ini file you backed up.

Sounds like a lot of steps, but things like that can be done in less time than it may take you to type up a batch file.
LVL 39

Expert Comment

ID: 22795911
In fact, upload your Settings.ini file as an attachment, and I'll provide you with about 8 very fast and easy steps to modify it in Notepad and MS Excel.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


Author Comment

ID: 22796098
Thanks for the response guys.
Sorry I didn't post my OS - Windows XP

Basically what I am trying to do is change settings within an .ini file on 1000s of computers. This file is a simple text config file for an app running on the pc. It's a complex situation because I'm not handling these changes through a main server and policy - I'm able to get files onto the pc in a specific folder and can run my file from there. I seriously won't bore you with the full details of all that.

The settings within the .ini files could be completely varied from one machine to the next and are simply a list of names and their current value:



At first I just thought to overwrite this file with my updated settings but realised I couldn't do that as some of the machines have extra settings that can't go on all of them and vice versa. So I wanted to be able to manipulate key settings within the .ini file which is when I came across change.com.

Many thanks on the file path info - yesterday was the first time I'd ever written a bat file and I've really just patched together what I have and my knowledge is far from extensive.

I'm guessing from what you've said in your response that I can have the bat file anywhere in the folder structure and it will still work providing paths are correctly notated? I ask because I can only download to one specific folder on the machines so they must run from there - or I have to run another bat file to move them.
LVL 39

Expert Comment

ID: 22796491
>>> "I'm guessing from what you've said in your response that I can have the bat file anywhere in the folder structure and it will still work providing paths are correctly notated?" <<<

Yes indeed, but there may be limitations on eg. the total number of characters in the directory path, the folder depth that it is told to traverse to and find the file, or some other issues, depending on how old the program is.  If it was written for older DOS or Win9x programs, then it could be that there may be such limitations on the program.

So you need to modify the "settings.ini" files on about 1,000 computers? !!

Can you execute the Change.com program to modify the *.ini files directly on each server over the network connection?  ie. does it accept network paths?

Author Comment

ID: 22810965
My file is in a folder called media whose path is: C:\Program Files\Projects\November\Media

the file I have to change is in C:Program Files\Projects folder so in this case I'm going up two folders. Would this be an extra full stop? i.e. ...\Projects\settings.ini

Still struggling with this so your help still appreciated :D

I'm not sure if the server accepts network paths - basically I have a number of third party companies between me and the server which is why this is so difficult in the first place.
LVL 39

Expert Comment

ID: 22812062
Hi.  Almost there. You use two double-stops separated by backslashes to reflect each folder you have gone backwards through, like this:


Just think of each double-dot as clicking the "Up One Level" button in the Windows Explorer toolbar.

You said:
"My file is in a folder called media ..."
Are you referring to the Change.com program file or your batch file?

If your batch file is:
C:\Program Files\Projects\November\Media\MakeChange.bat
and the file to modify is:
C:\Program Files\Projects\settings.ini
you could reference the *.ini file like this in the batch file:


OR just use the full path, possibly in quotation marks if there are errors:
"C:\Program Files\Projects\settings.ini"

Let me demonstrate using output as would be displayed in a command window by typing various commands.  I have attached this as a new "Code Snippet" so it is in plain text.  Where you see the > in a line, that is where the processing stopped and it displayed the "Prompt".  Where you see some text after the > in the following lines, that is where I typed in a new command.  Copy the code snippet out to Notepad and hopefully you will be able to follow it.

Using the SET command right at the start in your batch file can make things easier for two reasons:
1. You only have to specify the path once, and if you need to change that path you only need to change it in that one place at the start of the batch file
2. It keeps your actual command lines much shorter.

Something like this:

@echo off
set INIFILE=C:\Program Files\Projects\settings.ini
:: Your commands go in here, and you use %INIFILE% in place of the full path

The name you have given, ie. INIFILE is just a name, and it can be any short meaningful name either in uppercase or lowercase.  What happens is that the SET command literally "sets" the value that you make it = to (in this case it is a directory path) into a memory space that can be pulled out of memory at any time by using %INIFILE% in the batch file.  This is what is referred to as a "Variable" name.  You are "setting" the directory path into a variable named "INIFILE", and by calling it back using the % % around the name, you can use that stored value again in other commands, such as opening the file in Notepad:

notepad %INIFILE%

There is one thing that may be important given that you are going to modify a file on a large number of computers.  You cannot be absolutely sure in advance that the "Program Files" folder will be on the C:\ Drive.  Someone may have installed Windows to the D:\ Drive.  There are two things you can do to avoid specifying C:\ in your batch files.

There is a built-in variable in Windows XP named "SystemDrive".  This will always hold the directory path of the drive on which windows is installed.  In most cases the value held in this variable is  C:   note that there is no \ after it.

So, I could refer to your folder as:

%SystemDrive%\Program Files\Projects\settings.ini

notepad %SystemDrive%\Program Files\Projects\settings.ini

You can also use the \ right at the start of the directory path in place of the drive.  That will take you right back to the ROOT of the drive on which the batch file is being run (in most cases C: ):

\Program Files\Projects\settings.ini

notepad \Program Files\Projects\settings.ini

Hopefully this makes it clearer to you.

One thing is certain.  BEFORE you go testing any batch files, PLEASE test them.


Starting from C:\> Prompt use the CD command to "change directory" to
the named folder with each step.
You will see the new folder reflected each time as the current
directory you have changed into.  It becomes the new Prompt ending >
C:\>cd Program Files
C:\Program Files>cd Projects
C:\Program Files\Projects>cd November
C:\Program Files\Projects\November>cd Media
C:\Program Files\Projects\November\Media>cd
You could have got to the same place by specifying the entire path in your "change directory" command like this (from C:\> again):
C:\>cd Program Files\Projects\November\Media
C:\Program Files\Projects\November\Media>
Now change directory back upwards using .. as follows:
C:\Program Files\Projects\November\Media>cd ..
C:\Program Files\Projects\November>cd ..
C:\Program Files\Projects>cd ..
C:\Program Files>cd ..
Starting at the C:\Program Files\Projects\November\Media> prompt, the
following single command would take you back to the same place just the same as all the separate "change directory" commands shown above.
C:\Program Files\Projects\November\Media>cd ..\..\..\..
Count the folders you need to go back (excluding the one you are currently in), and you will know how many backslash-separated  groups of dots you need to get to where you want:
C:\Program Files\Projects\November\Media>
..\     ..      \    ..  \    ..  \

Open in new window

LVL 39

Accepted Solution

BillDL earned 2000 total points
ID: 22812095
"BEFORE you go testing any batch files, PLEASE test them."
should read:
"BEFORE you go testing any batch files on THOSE computers, PLEASE test them on YOUR computer."

Author Comment

ID: 22812572
Thats fantastic thanks Bill - for some reason it wouldn't work with the specified file path i.e. C:\Program Files\Projects\settings.ini whether in quotes or not but I think this is an issue with the Change application not the format.

The use of ..\..\  to move up the directories worked brilliant though - many thanks for that.

While looking for the source code for the Change app I also happened across an improved version which allows wild cards!!! So I now have a 5 line bat file that will alter my ini files from the folder it will be placed in. Fantastic! Thanks for the help and just to re-assure you - I have tested it!! Thanks again :D

Author Closing Comment

ID: 31509581
Cheers mate :D
LVL 39

Expert Comment

ID: 22816634
Thank you deathbybatfile (what a name!).
I don't suppose you copied the download link for the updated and better Change program?  I wouldn't mind having a go with it myself.

There is one program like this named xChange that I've had for a long time.  It was part of a package designed to be run from a bootable floppy disk for forensic purposes, but it's now too old to be useful with NTFS formatted hard drives, SATA, etc.  I wouldn't mind having a replacement program.

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question