The user manually types, in a window, the input data, which is stored in variables. Then the batch does an "echo" of the variables that contain the data that the user entered (first.bat):
set source=%HOMEDRIVE%\test\data.txt
mydialogbox=%HOMEDRIVE%\test\myprogram_dialogbox.exe
SetLocal EnableDelayedExpansion
:: dialog box
for /f "tokens=1-3 delims=" %%i in ('%mydialogbox% --forms --title="Dialog" --text="Set Data" --add-entry="USER" --add-entry="PASSWORD" --add-entry="DBNAME" --separator=","') do (
set line=%%i
echo(!line: =!> %source%
)
IF EXIST %source% (GOTO setdata) else (GOTO end)
:setdata
FOR /F "eol=; tokens=1-3 delims=," %%A IN (%source%) DO (
set dbuser=%%A
set dbpass=%%B
set dbname=%%C
)
echo your imput: %dbuser% %dbpass% %dbname%
out e.g.:
your input: mydb zuckerberg dadada
So I need to replace this values (of these variables: %dbuser%
%dbpass%
%dbname%
) in another script (second.bat)
set dbname=foo
set dbuser=bar
set dbpass=blabla
expected output (Change cannot be dynamic, but permanent):
set dbname=mydb
set dbuser=zuckerberg
set dbpass=dadada
how I do this?
PS: On linux this is easy
sed -e "s:$dbuser:zuckerberg:g" second.bat
PS: If what I'm asking doesn't work, then is there a way to use the output of the %dbuser%
%dbpass%
%dbname%
variables from first.bat
into second.bat
thanks
Update:
- I am using fart-it and it has worked:
first.bat
:
fart.exe second.bat "zuckerberg" "%dbuser%"
fart.exe second.bat "dadada" "%dbpass%"
fart.exe second.bat "mydb" "%dbname%"
But I have to execute 3 times the same command for each replacement. And I would like to know if there is another solution (.bat, .cmd) that does not depend on external programs
- Another solution is to put the output of the "echo" in variables in
second.bat
:
first.bat
:
echo your imput: %dbuser% %dbpass% %dbname%
second.bat
:
FOR /F "tokens=*" %%g IN ('echo %dbuser%') do (SET myuser=%%g)
etc
but this does not permanently replace the values of variables of the second script (which is what I want).
- Another solution is this (
first.bat
):
(for /f "tokens=1* delims=:" %%a in (%source%) do (
if "%%b"=="" (echo %%a) else (
echo %%a|find " id" >null&& echo %%a: ||echo %%a: %%b
)
))>second.bat
But I have not been able to adjust it to my interests, since it rewrites the output (does not search and replace the strings)
- Another solution is with replacer.bat:
call replacer.bat "second.bat" "zuckerberg" "%dbuser%"
It has the same problems as fart-it. I have to run a command for each replacement (this is not very optimized for my code) and would depend on an external script.