Send variable from user input to text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Send variable from user input to text

#1 Post by myjet » 09 Dec 2010 05:25

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Send variable from user input to text

#2 Post by jeb » 09 Dec 2010 06:16

Hi myjet,

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

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#3 Post by myjet » 09 Dec 2010 07:07

Nice! That works! Just exactly what I need. Thank you very much

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#4 Post by myjet » 09 Dec 2010 07:33

One more thing:
How do I convert the following code in C, into batch script?

cin>>str1;
strlen(str1);
do{
//process;
}while(var1<=15)

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#5 Post by myjet » 09 Dec 2010 08:15

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: "

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#6 Post by myjet » 09 Dec 2010 11:06

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

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#7 Post by myjet » 09 Dec 2010 11:28

Ok I've got it :D
> will overwrite
>> will append
Oh such a simple thing...just what to be expected of beginner

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Send variable from user input to text

#8 Post by orange_batch » 10 Dec 2010 05:14

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.

myjet
Posts: 10
Joined: 10 Aug 2010 18:50

Re: Send variable from user input to text

#9 Post by myjet » 10 Dec 2010 06:22

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? :?

Post Reply