Eric Cartt
Eric Cartt

Reputation: 11

Save user input in to text file through batch file

I have created this code and I cannot do so when I type some text to username and yearsoflife the text to be exported in a .txt file.

@echo off
color 0a
echo.
echo.
echo    Please insert the following
echo.
echo.
echo.
set /p username=name:
set /p yearsoflife=age:
%username% > name.txt
%yearsoflife% > yourage.txt

Upvotes: 0

Views: 2492

Answers (1)

double-beep
double-beep

Reputation: 5504

Here is one possible solution:

@echo off
color 0a
echo.
echo.
echo    Please insert the following
echo.
echo.
echo.
set /p _username=name: 
set /p yearsoflife=age: 
(echo=%_username%) > name.txt
(echo=%yearsoflife%) > yourage.txt

You were not so clear in your question; you referenced that you want both variable contents in one textfile. So, for this you can try (double the > symbol):

@echo off
color 0a
echo.
echo.
echo    Please insert the following
echo.
echo.
echo.
set /p _username=name: 
set /p yearsoflife=age: 
(echo=%_username%) >> your_name.txt
(echo=%yearsoflife%) >> your_name.txt

Note: Change of set /p username=name: line was just not to confuse this username variable with username environment variable!

Upvotes: 1

Related Questions