how to edit a batch file value useing a batch

March 29, 2010 at 04:08:15
Specs: Windows XP
I have a Batch file which contain the following code
@echo off
set userVar1=100
set userVar2=300
echo.

i hav saved the file as userVariableFile.bat

i want to create another batch script useing command prompt to edit the variable value in the above batch file. if i name this 2nd batch file as modifyVar.bat
once i run it with new value (lets say command "modifyVar userVar1 400" ) it must modify the value attached to userVar1 as 400. so if i open the 1st batch file in notepad it should show the new code in the batch file as
@echo off
set userVar1=400
set userVar2=300
echo.
how can i create this modifyVar.bat file


See More: how to edit a batch file value useing a batch

Report •


#1
March 29, 2010 at 15:55:22
My first idea would be to use the FOR /F command in your modifyVar.bat.

FOR /? will give you examples and syntax.

If you need an example let me know.


Report •

#2
March 29, 2010 at 16:29:23
i left in all the debugging stuff, knowing it will be needed!

@echo off>new & setlocal enabledelayedexpansion
set va=%1
set nv=%2
call :len %va%
echo.determined length:!LL!
set va=set !va!=
set /a LL+=5
for /f "tokens=*" %%a in (mybat.bat) do (
set xx=%%a
set test=!xx:~0,%LL%!
echo.[!test!] compare to:[!va!]
pause
if /i "!test!" equ "!va!" set xx=!va!!nv!
>> new echo !xx!
)
goto :eof
:len
echo.incoming percent one:%1
pause
for /L %%a in (0 1 100) do (
set LL=%%a
echo.!LL! [!t2!]
set t2=!test:~%%a,1!
echo.[!t2!]
if '!t2!' equ '' goto :done
pause
)
:done
::------- end
ps: i feel sure there's easier ways to accomplish your objective, but fwiw here's this to work with.


Report •

#3
March 29, 2010 at 19:01:13
Thnak you for the guide i'll try it out

Report •

Related Solutions

#4
March 29, 2010 at 19:18:18
This is another one without the for /f

@echo off
set strVar=%1
set intNum=%2
type userVar1.bat | find /V /I "%strVar%" >userVar1.txt
echo set %strVar%=%intNum%>>userVar1.txt
del userVar1.bat
ren userVar1.txt userVar1.bat


Report •

#5
March 29, 2010 at 20:19:59
Yeah, i think i like yours better! definitely less cumbersome, but you might want to make the findstring more specific in case the variable is used elsewhere in the batch:
@echo off
set strVar=%1
set intNum=%2
:: might consider to add this line, if it works (not tested)
set strVar=set %strVar%=
type userVar1.bat | find /V /I "%strVar%" >userVar1.txt
echo set %strVar%=%intNum%>>userVar1.txt
del userVar1.bat
ren userVar1.txt userVar1.bat


Report •

#6
March 30, 2010 at 02:12:34
Thanks alot for the help but my problem is i want to add the code in between commands not to add it below..
to try it w/o "for/f" i used ur example bit modified..i saved the following under modify.bat

@echo off
set strVar=%1
set intNum=%2

set %strVar%=%intNum%
type userVar.bat | find /V /I "%strVar%=" >userVar.txt
echo set %strVar%=%intNum% >> userVar.txt
del userVar.bat
ren userVar.txt userVar.bat

then i execute the command "modify userVar1 400" but the userVar.bat file was like:
@echo off
set userVar2=300
echo %dsa1%
echo %dsa2%
echo.
set userVar1=400

the command didn't add in to the prefered location ( i need to add it after "set userVar2=300") i think i would need to use for/f to locate that but i hav no idea how to use it...


Report •

#7
March 30, 2010 at 02:17:09
for your infor the initial uservar.bat file is

@echo off
set userVar1=100
set userVar2=300
echo %dsa1%
echo %dsa2%
echo.

after modification i need it to show the code as

@echo off
set userVar1=400 rem (only this variable value must change)
set userVar2=300
echo %dsa1%
echo %dsa2%
echo.



Report •

#8
March 30, 2010 at 03:07:25
And what if you do this

--------------------------------------
modifyVar
--------------------------------------
@echo off

echo set %1=%2> C:\temp.cmd
call C:\temp.cmd
del C:\temp.cmd


Report •

#9
April 5, 2010 at 19:26:48
@tvc: sorry i didn't get u? how to use this??

@all: could u provide me with a example to use for /f for this ...


Report •

#10
April 6, 2010 at 03:38:41
modifyVar.cmd variable_name new_value

(cmd can also be bat)


Report •


Ask Question