Page 1 of 1

User Input

Posted: 20 May 2010 15:52
by sp11k3t3ht3rd
Okay this may sound like a stupid question but how can you ask the user to type something and then have what they typed be put into a .txt file? i know the command for user input is:

Code: Select all

set /p input =

but when I type this code after that:

Code: Select all

%input% >> test.txt

It creates the .txt file and then says that what ever the user typed is not a valid command. PLEASE HELP!

Re: User Input

Posted: 20 May 2010 16:01
by aGerman
You need the command ECHO for outputting a variable value.

Code: Select all

echo %input%>>test.txt


Regards
aGerman

Re: User Input

Posted: 20 May 2010 16:09
by sp11k3t3ht3rd
Thanks!! I have another question though. How can you make multiple lines get copied in the file? Like I want to be able to have text be entered on the second line in the .txt file.

Re: User Input

Posted: 20 May 2010 16:22
by aGerman
> is for NEW
>> is for APPEND

Code: Select all

@echo off &setlocal

set /p "line1=1st line: "
set /p "line2=2nd line: "
set /p "line3=3rd line: "

>test.txt echo.%line1%
>>test.txt echo.%line2%
>>test.txt echo.%line3%


Regards
aGerman

Re: User Input

Posted: 20 May 2010 16:24
by 1+2=46

Code: Select all

echo %input%>test.txt
for the first line.

Code: Select all

echo %input%>>test.txt
for the other lines.


Example:

Code: Select all

echo 1>test.txt
echo 2>>test.txt


The file "test.txt" would be like this:

1
2



If you then would want to add another line to it you would have to put...

Code: Select all

echo 3>>test.txt


... which would make the file be like this:

1
2
3



So, the code "echo %something%>>%some file%" will add the %something% to the end of the %some file%.

Re: User Input

Posted: 20 May 2010 16:28
by sp11k3t3ht3rd
@aGerman That only made 2 lines... and what does

Code: Select all

&setlocal
do?

Re: User Input

Posted: 20 May 2010 16:34
by 1+2=46
Use mine code instead if you are having problems with aGerman one's, though his is better than mine, I guess.

Re: User Input

Posted: 20 May 2010 16:40
by sp11k3t3ht3rd
I don't know why but this seems to work just fine:

Code: Select all

@echo off
set /p line1= line:
echo %line1%>test1.txt
set /p line2= line:
echo %line2%>>test1.txt
set /p line3= line:
echo %line3%>>test1.txt
pause


Thanks for all the help!

Re: User Input

Posted: 20 May 2010 16:43
by aGerman
sp11k3t3ht3rd wrote:@aGerman That only made 2 lines...

You have to "close" the last line of the code. That means place the cursor behind the last line and hit [ENTER].

The SETLOCAL command limits changings of the environment to the current batch window. It's not strictly recommented but it's one of my defaults.

Regards
aGerman

Re: User Input

Posted: 20 May 2010 16:44
by 1+2=46
Why shouldn't it work?

Re: User Input

Posted: 20 May 2010 16:49
by sp11k3t3ht3rd
Well I changed around the code so I thought it might not work and aGerman whooops! sorry and thanks