Send variable from user input to text
Moderator: DosItHelp
Send variable from user input to text
Hello,
I have a script that prompt user to input a variable and passes that variable to a text file, along with another predefined string:
cls
@echo off
echo Enter your question
set /p question=
echo %question%
echo "<satu><item soalan='%question%' jawapanA='A: " >> Question1.xml
exit
This works well except I don't want the " sign in my text file
When I try removing the " sign by doing this:
cls
@echo off
echo Enter your question
set /p question=
echo %question%
echo <satu><item soalan='%question%' jawapanA='A: >> Question1.xml
exit
the batch doesn't run well because it takes the >< sign as command.
I want the file "Question1.xml" to contain "<satu><item soalan='%question%' jawapanA='A: " ; means everything inside the double qoutes, except the double quotes itself.
Anyone can help me with this? I'm pretty sure it's just an easy matter of putting "something" in the code that I don't know about
I have a script that prompt user to input a variable and passes that variable to a text file, along with another predefined string:
cls
@echo off
echo Enter your question
set /p question=
echo %question%
echo "<satu><item soalan='%question%' jawapanA='A: " >> Question1.xml
exit
This works well except I don't want the " sign in my text file
When I try removing the " sign by doing this:
cls
@echo off
echo Enter your question
set /p question=
echo %question%
echo <satu><item soalan='%question%' jawapanA='A: >> Question1.xml
exit
the batch doesn't run well because it takes the >< sign as command.
I want the file "Question1.xml" to contain "<satu><item soalan='%question%' jawapanA='A: " ; means everything inside the double qoutes, except the double quotes itself.
Anyone can help me with this? I'm pretty sure it's just an easy matter of putting "something" in the code that I don't know about
Re: Send variable from user input to text
Hi myjet,
you can solve this with delayed expansion, because the characters are not interpreted then.
hope it helps
jeb
you can solve this with delayed expansion, because the characters are not interpreted then.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
echo Enter your question
set /p question=
echo %question%
set "text=<satu><item soalan='%question%' jawapanA='A: "
echo !text!>> Question1.xml
exit
hope it helps
jeb
Re: Send variable from user input to text
Nice! That works! Just exactly what I need. Thank you very much
Re: Send variable from user input to text
One more thing:
How do I convert the following code in C, into batch script?
cin>>str1;
strlen(str1);
do{
//process;
}while(var1<=15)
How do I convert the following code in C, into batch script?
cin>>str1;
strlen(str1);
do{
//process;
}while(var1<=15)
Re: Send variable from user input to text
And what should be done to overwrite the existing text file, not just append the new string? For example if the text already contained "This is the question", if I run the program the text file will then contain "This is the question<satu><item soalan='%question%' jawapanA='A: ". Instead I want the text file to contain only the string "<satu><item soalan='%question%' jawapanA='A: "
Re: Send variable from user input to text
At last! I've got something very close to what I want. The only thing bugging me now is how to overwrite the existing file. I'd be glad if anyone care to leave some hint on how to do this. Better if there is option on whether to overwrite or not. Go on copy and run the following code. You'll see what it does. Special thanks to jeb for giving the first clue of what I should start off with.
cls
@echo off
setlocal EnableDelayedExpansion
set qnum=1
:mulaSoalan
::----------------Soalan------------------::
echo This is for Question%qnum% content
echo Enter your question:
set /p question=
echo ______________________________________________________
echo ______________________________________________________
::----------------Soalan------------------::
::----------------Jawapan A------------------::
goto startA
:ulangA
echo Sorry the length has exceeded 14 characters
:startA
echo Enter the answer for option A (14 characters maximum):
set /p ansA=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansA%
set soalan=1
goto panjang
:returnA
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangA
)
::----------------Jawapan A------------------::
::----------------Jawapan B------------------::
goto startB
:ulangB
echo Sorry the length has exceeded 14 characters
:startB
echo Enter the answer for option B (14 characters maximum):
set /p ansB=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansB%
set soalan=2
goto panjang
:returnB
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangB
)
::----------------Jawapan B------------------::
::----------------Jawapan C------------------::
goto startC
:ulangC
echo Sorry the length has exceeded 14 characters
:startC
echo Enter the answer for option C (14 characters maximum):
set /p ansC=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansC%
set soalan=3
goto panjang
:returnC
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangC
)
::----------------Jawapan C------------------::
::----------------Jawapan D------------------::
goto startD
:ulangD
echo Sorry the length has exceeded 14 characters
:startD
echo Enter the answer for option D (14 characters maximum):
set /p ansD=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansD%
set soalan=4
goto panjang
:returnD
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangD
)
::----------------Jawapan D------------------::
::----------------Jawapan Sebenar------------------::
echo Which is the real answer of the four?
echo Type A, B, C or D
set /p realAns=
echo The real answer of the four is %realAns%
echo ______________________________________________________
echo ______________________________________________________
::----------------Jawapan Sebenar------------------::
::----------------Dwipilihan------------------::
echo Enter the first of two answers that will be omitted if the user clicked on "Fifty-fifty chances"
echo Type A, B, C or D
set /p omit1=
echo First answer to be omitted is %omit1%
echo ______________________________________________________
echo ______________________________________________________
echo Enter the second of two answers that will be omitted if the user clicked on "Fifty-fifty chances"
echo Type A, B, C or D
set /p omit2=
echo Second answer to be omitted is %omit2%
echo ______________________________________________________
echo ______________________________________________________
::----------------Dwipilihan------------------::
set "text1=<satu>"
set "text2=<item soalan='%question%' jawapanA='A: %ansA%' jawapanB='B: %ansB%' jawapanC='C: %ansC%'jawapanD='D: %ansD%' jawapan='%realAns%' dwipilihan1='%omit1%' dwipilihan2='%omit2%'/>"
set "text3=</satu>"
echo !text1!>> Question%qnum%.xml
echo !text2!>> Question%qnum%.xml
echo !text3!>> Question%qnum%.xml
set /A qnum+=1
if not %qnum% == 15 goto mulaSoalan
goto end
:panjang
set length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
if %soalan%==1 goto returnA
if %soalan%==2 goto returnB
if %soalan%==3 goto returnC
if %soalan%==4 goto returnD
:end
:exit
cls
@echo off
setlocal EnableDelayedExpansion
set qnum=1
:mulaSoalan
::----------------Soalan------------------::
echo This is for Question%qnum% content
echo Enter your question:
set /p question=
echo ______________________________________________________
echo ______________________________________________________
::----------------Soalan------------------::
::----------------Jawapan A------------------::
goto startA
:ulangA
echo Sorry the length has exceeded 14 characters
:startA
echo Enter the answer for option A (14 characters maximum):
set /p ansA=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansA%
set soalan=1
goto panjang
:returnA
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangA
)
::----------------Jawapan A------------------::
::----------------Jawapan B------------------::
goto startB
:ulangB
echo Sorry the length has exceeded 14 characters
:startB
echo Enter the answer for option B (14 characters maximum):
set /p ansB=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansB%
set soalan=2
goto panjang
:returnB
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangB
)
::----------------Jawapan B------------------::
::----------------Jawapan C------------------::
goto startC
:ulangC
echo Sorry the length has exceeded 14 characters
:startC
echo Enter the answer for option C (14 characters maximum):
set /p ansC=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansC%
set soalan=3
goto panjang
:returnC
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangC
)
::----------------Jawapan C------------------::
::----------------Jawapan D------------------::
goto startD
:ulangD
echo Sorry the length has exceeded 14 characters
:startD
echo Enter the answer for option D (14 characters maximum):
set /p ansD=
echo ______________________________________________________
echo ______________________________________________________
set #=%ansD%
set soalan=4
goto panjang
:returnD
echo Length of character: %length%
FOR /L %%i IN (15,1,40) DO (
if %length% == %%i goto ulangD
)
::----------------Jawapan D------------------::
::----------------Jawapan Sebenar------------------::
echo Which is the real answer of the four?
echo Type A, B, C or D
set /p realAns=
echo The real answer of the four is %realAns%
echo ______________________________________________________
echo ______________________________________________________
::----------------Jawapan Sebenar------------------::
::----------------Dwipilihan------------------::
echo Enter the first of two answers that will be omitted if the user clicked on "Fifty-fifty chances"
echo Type A, B, C or D
set /p omit1=
echo First answer to be omitted is %omit1%
echo ______________________________________________________
echo ______________________________________________________
echo Enter the second of two answers that will be omitted if the user clicked on "Fifty-fifty chances"
echo Type A, B, C or D
set /p omit2=
echo Second answer to be omitted is %omit2%
echo ______________________________________________________
echo ______________________________________________________
::----------------Dwipilihan------------------::
set "text1=<satu>"
set "text2=<item soalan='%question%' jawapanA='A: %ansA%' jawapanB='B: %ansB%' jawapanC='C: %ansC%'jawapanD='D: %ansD%' jawapan='%realAns%' dwipilihan1='%omit1%' dwipilihan2='%omit2%'/>"
set "text3=</satu>"
echo !text1!>> Question%qnum%.xml
echo !text2!>> Question%qnum%.xml
echo !text3!>> Question%qnum%.xml
set /A qnum+=1
if not %qnum% == 15 goto mulaSoalan
goto end
:panjang
set length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
if %soalan%==1 goto returnA
if %soalan%==2 goto returnB
if %soalan%==3 goto returnC
if %soalan%==4 goto returnD
:end
:exit
Re: Send variable from user input to text
Ok I've got it
> will overwrite
>> will append
Oh such a simple thing...just what to be expected of beginner
> will overwrite
>> will append
Oh such a simple thing...just what to be expected of beginner
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Send variable from user input to text
There's a way to read and rewrite text files too (ie make specific line changes).
I recommend using strLen found on the homepage. It was just revised, it's very efficient.
I recommend using strLen found on the homepage. It was just revised, it's very efficient.
Re: Send variable from user input to text
orange_batch wrote:There's a way to read and rewrite text files too (ie make specific line changes).
I recommend using strLen found on the homepage. It was just revised, it's very efficient.
Huh?
Code: Select all
:strLen
SETLOCAL
set str=A!%~1!
set len=0
set /a n=1024
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
set /a n^>^>=1, len+=n
if !str:~%len%!. == . set /a len-=n
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET %~2=%len%
)
GOTO:EOF
You mean this right? Oh gosh..for one thing I don't understand much of it, especially the n^>^>=1 , . == . and NEQ part; likely because I'm a beginner. Another thing is that the code is too long.
I think I'd just stick with the one I've got:
Code: Select all
set length=0
set /p stringToFind=
set #=%stringToFind%
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
echo %length%
It's far more shorter, and easier to understand (though I don't really understand how it works; loop by loop) isn't it? Or did I miss some point?