Yes, it is possible.
Dynamic editing of files is best done with sed or awk (gawk) which can
be found at (<http://gnuwin32.sourceforge.net/packages.html>). You
would probably need some help getting started, but without even a tiny
clue to what needs to be done, I can't write a gawk script (I don't
use sed).
Simple replacement of a unique string in the file is probably better
done with gsar (same site).
--
T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
- - - - - - - - - - begin screen capture WinXP - - - - - - - - - -
C:\cmd>type c:\temp\mytestinifile.txt
this is my test ini file
some stuff
more stuff
[section one]
blah blah
still more stuff
C:\cmd>changes c:\temp\mytestinifile.txt c:\temp\mynewinifile.txt "blah
lah" "yacketty yack"
[note: preceding line got wrapped in screen capture; it's all one line.]
C:\cmd>type c:\temp\mynewinifile.txt
this is my test ini file
some stuff
more stuff
[section one]
yacketty yack
still more stuff
C:\cmd>wyllist test\changes.cmd
==========begin file C:\cmd\test\changes.cmd ==========
001. @echo off
002. ::
003. :: changes (oldstring) to (newstring)
004. ::
005. :: %%1 - infile
006. :: %%2 - outfile
007. :: %%3 - oldstring
008. :: %%4 - newstring
009. ::
010. setlocal enabledelayedexpansion
011. set infile=%1
012. set outfile=%2
013. set oldstring=%~3
014. set newstring=%~4
015.
016. for /f "tokens=1,2 delims=:" %%a in (
017. 'findstr /n /v /c:"Osama Bin Laden" %infile%'
018. ) do call :changeit "%%b"
019. endlocal&goto :EOF
020.
021. :changeit
022. set rec=%~1
023. if not defined rec (
024. echo/>>%outfile%
025. goto :EOF
026. )
027. set rec=!rec:%oldstring%=%newstring%!
028. echo>>%outfile% %rec%
029. goto :EOF
==========end file C:\cmd\test\changes.cmd ==========
- - - - - - - - - - end screen capture WinXP - - - - - - - - - -
Certainly, it's possible. A few examples of exactly what you want to do
would be a help, though.
Either one and the commands depend on what you are trying to do.
For a change of pace, how about if you just explain what you are trying to
do and in what OS, and others will assist you if they have enough details to
go on. Some may even guess and hope you find something useful but is 'poke
and hope' really the way you want to learn?
--
Todd Vargo (double "L" to reply by email)
Alright, here it goes. Most my operaters use a varity of operating
systems. Anywhere from 98, 2000, or XP. Within the .ini file of this
program we need to change values of 3 sepearte variables.
The file name is c:\program files\program\global.ini
Currently it reads this:
[MISC]
accountability=0
cashier=cashier (this variable may be set to anything)
fast sweeps=0
We need a batch or script file that changes it to this
[MISC]
accountablitiy=1
cashier=lane
fast sweeps=1
The batch file posted eariler by Phil was good but I believe it searchs
and replaces the entire line. These variables aren't always on the
same lines and cashier doesn't always equal cashier. It could equal
clerk, employee, what ever the user set it too. So basically I need a
batch file or script that will search for these 3 variables within the
global.ini file and just replace the values. I don't know the commands
to acheive this. Any ideas?
Sounds like a simple hard coded solution will suffice, but for clarity, I
must ask two more questions.
1) Is there anything else in this file?
If you answered NO, proceed to question 2.
If YES, use one of the many INI changing utilities readily available (one
has already been suggested by Cameron).
2) Do you want the exact same file copied to all of the machines?
If you answered NO, then see answer first question, otherwise you could just
rebuild the entire file with plain ECHO commands as follows.
::Suitable for Windows 98, 2000 and XP
@echo off
set ini=c:\program files\program\global.ini
echo> "%ini%" [MISC]
echo>>"%ini%" accountablitiy=1
echo>>"%ini%" cashier=lane
echo>>"%ini%" fast sweeps=1
set ini=
Let us know if this helps.
1) yes, many other variables
2) yes
Although you said try the dosini utility, I tried the echo commands for
grins. It replaced the global.ini with the 3 lines like it is suppose
to, leaving the ini file missing a lot of information. The good news
is that when I restarted the program it rebuilt the missing ini
variables within the global.ini file. Anything I should weary of?
Never mind, It doesn't rebuild the program just reverts to defualt
values.
What is involved in using a ini changing utility?
Directions vary by utility. I Usually download and read the manual first.
Oh dear - seems to be a communications problem with this question. Lots of
questions and answers, but not much clarification.
AIUI, the problem is that there are lines to be edited in a file called
'program.ini', which is a text file. In this text file, there is a line
[MISC]
and following that line, there are 3 lines of interest
accountability=0
cashier=something
fast sweeps=0
which need to be changed to
accountability=1
cashier=LANE
fast sweeps=1
by a script, and it needs to run under W98, 2000 and XP.
What we DON'T know appears to be:
1) whether any of these lines can appear elsewhere in program.ini
2) whether they will appear in any specific order
3) whether they will appear consecutively, or directly after the [MISC] line
4) whether LANE is a literal or a parameter (ie. it will be specified when
the script is run)
So, to solve it, we're having to make assumptions.
This is not a trivial problem, and a standard NT+ FOR/f solution is not
applicable because FOR/f is not available in W98.
We cannot eve use mighty EDLIN as it isn't installed in W98.
So - looks like a job for SED (OP: look for SED on the net - it's a
well-known utility. The version I use is Howard Helman's version dated Oct.
1st 1991 (22,104 bytes in SED.EXE) )
Here's a script that should do the job:
--------- EI.BAT begins
[01]@echo off
[02]:: presence of parameter
[03]if [%1]==[] for %%i in (echo goto) do %%i error - no parameter
[04]:: W98 and NT+ have different syntax...find version
[05]ver|find "Windows 98">nul
[06]if not errorlevel 1 goto W98
[07]
[08]:: NT version to create ini.sed - a SED script to edit program.ini
[09]echo /^^\[MISC\]$/,$ s/accountability=0/accountability=1/>ini.sed
[10]echo /^^\[MISC\]$/,$ s/cashier=.*/cashier=%1/>>ini.sed
[11]echo /^^\[MISC\]$/,$ s/fast sweeps=0/fast sweeps=1/>>ini.sed
[12]goto common
[13]
[14]:W98
[15]:: W98 version to create ini.sed - a SED script to edit program.ini
[16]echo /^\[MISC\]$/,$ s/accountability=0/accountability=1/>ini.sed
[17]echo /^\[MISC\]$/,$ s/cashier=.*/cashier=%1/>>ini.sed
[18]echo /^\[MISC\]$/,$ s/fast sweeps=0/fast sweeps=1/>>ini.sed
[19]
[20]:common
[21]:: Process program.ini and produce program.txt
[22]sed -f ini.sed program.ini>program.txt
[23]:: Display differences
[24]fc program.txt program.ini
[25]:: zap sed tempfile
[26]del ini.sed
[27]
[28]:error
--------- EI.BAT ends
Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.
I've assumed that you will run this as
EI LANE
where LANE is a lane number. It'll work if LANE is the literal LANE, too.
Just delete lines 02 and 03 and replace the "%1" on lines 10 and 17 with
LANE. This takes care of point 4)
With this solution, 2) and 3) are irrelevant - it will only change any
matching text that follows the [MISC] line.
1) may be a problem, I don't know. The script will replace ALL occurrences
of the three strings following the [MISC] line. I suspect this will be fine,
but if it is possible that one of the target strings occurs in the next
section of the .ini file, then the script will need an adjustment.
Note that this script will simply create a file "program.txt" and then
compare this file with "program.ini". After testing, if you are satisfied,
replace lines 23 and 24 with
copy program.txt program.ini >nul
del program.txt
HTH
...Bill
::INIFILE 1.0 (c) 2002, Horst Schaeffer
::Add/Change/Remove/Read entries in INI files
::Syntax:
::Add or Change:
:: INIFILE ini_file_name [section] item=ini_string
::Remove:
:: INIFILE ini_file_name [section] item=
::Read:
:: INIFILE ini_file_name [section] item
:: (generates SET statement to STDOUT)
ex usage...
::
:: read the current values
::
inifile program.ini [MISC] accountability > "%temp%\temp.bat"
call "%temp%\temp.bat"
inifile program.ini [MISC] cashier > "%temp%\temp.bat"
call "%temp%\temp.bat"
inifile program.ini [MISC] fast sweeps > "%temp%\temp.bat"
call "%temp%\temp.bat"
::
:: write new values
::
inifile program.ini [MISC] accountability=1
inifile program.ini [MISC] cashier=LANE
inifile program.ini [MISC] fast sweeps=1
::-------------------------------------------------------------------
nice and clean, easy to learn, easy to script etc
[snip]
>Alright, here it goes. Most my operaters use a varity of operating
>systems. Anywhere from 98, 2000, or XP. Within the .ini file of this
>program we need to change values of 3 sepearte variables.
>
>The file name is c:\program files\program\global.ini
>
>Currently it reads this:
>
>[MISC]
>accountability=0
>cashier=cashier (this variable may be set to anything)
>fast sweeps=0
>
>We need a batch or script file that changes it to this
>
>[MISC]
>accountablitiy=1
>cashier=lane
>fast sweeps=1
>
>The batch file posted eariler by Phil was good but I believe it searchs
>and replaces the entire line. These variables aren't always on the
>same lines and cashier doesn't always equal cashier. It could equal
>clerk, employee, what ever the user set it too. So basically I need a
>batch file or script that will search for these 3 variables within the
>global.ini file and just replace the values. I don't know the commands
>to acheive this. Any ideas?
Difficult with only on-board tools. Others have suggested various
third-party tools, here's another - 4DOS/4NT; the code couldn't be simpler:
SET dummy=%@INIWRITE[c:\program files\program\global.ini,MISC,accountability,0]
SET dummy=%@INIWRITE[c:\program files\program\global.ini,MISC,cashier,cashier]
SET dummy=%@INIWRITE[c:\program files\program\global.ini,MISC,fast sweeps,0]
@INIWRITE will create new values in case of spelling errors (accountablitiy).
It is documented at <http://jpsoft.com/help/f_iniwrite.htm>.
4DOS is free and runs on all platforms you mention; 4NT is not free and is
better not run on Win4 platforms.
--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"