Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
sp11k3t3ht3rd
- Posts: 26
- Joined: 20 May 2010 15:39
#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:
but when I type this code after that:
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
#2
Post
by aGerman » 20 May 2010 16:01
You need the command ECHO for outputting a variable value.
Regards
aGerman
-
sp11k3t3ht3rd
- Posts: 26
- Joined: 20 May 2010 15:39
#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
#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
#5
Post
by 1+2=46 » 20 May 2010 16:24
for the first line.
for the other lines.
Example:
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...
... 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%.
-
1+2=46
- Posts: 25
- Joined: 23 Mar 2010 14:19
#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
#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
#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
-
sp11k3t3ht3rd
- Posts: 26
- Joined: 20 May 2010 15:39
#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