User Input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

User Input

#1 Post by sp11k3t3ht3rd » 20 May 2010 15:52

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: User Input

#2 Post by aGerman » 20 May 2010 16:01

You need the command ECHO for outputting a variable value.

Code: Select all

echo %input%>>test.txt


Regards
aGerman

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: User Input

#3 Post by sp11k3t3ht3rd » 20 May 2010 16:09

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: User Input

#4 Post by aGerman » 20 May 2010 16:22

> 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

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: User Input

#5 Post by 1+2=46 » 20 May 2010 16:24

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%.

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: User Input

#6 Post by sp11k3t3ht3rd » 20 May 2010 16:28

@aGerman That only made 2 lines... and what does

Code: Select all

&setlocal
do?

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: User Input

#7 Post by 1+2=46 » 20 May 2010 16:34

Use mine code instead if you are having problems with aGerman one's, though his is better than mine, I guess.

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: User Input

#8 Post by sp11k3t3ht3rd » 20 May 2010 16:40

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: User Input

#9 Post by aGerman » 20 May 2010 16:43

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

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: User Input

#10 Post by 1+2=46 » 20 May 2010 16:44

Why shouldn't it work?

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: User Input

#11 Post by sp11k3t3ht3rd » 20 May 2010 16:49

Well I changed around the code so I thought it might not work and aGerman whooops! sorry and thanks

Post Reply