Question:
How can I use a batch file to echo text to a text file with a variable file path?
marathon
2010-11-25 16:01:07 UTC
I am making a batch file script to quickly change a database of text files for all the clients at my local food pantry. When a new client needs to be added, I need the user to be able to format the text file exactly, so I am using the batch for it. The problem arises because I need to echo specifically formatted information in variables to a text file. The variables in the actual text file work fine, but the file name is always set to 0. Here's the code; I would appreciate any help anyone can offer.

:addclient
cls
echo.
echo.
echo.
echo Add New Client
echo.
echo Enter last name and press enter.
set /p addlastname=))
echo.
echo Enter first name and press enter.
set /p addfirstname=))
set /a newclientfile=%addlastname%-%addfirstname%
:repeatregister2
echo.
echo Would you like to register a visit for this person today?
echo 1) Yes
echo 2) No
set /p registerchoice2=))
if "%registerchoice2%"=="1" goto registernewclient
if "%registerchoice2%"=="2" goto addnewclient
if not "%registerchoice2%"=="1" goto repeatregister2
if not "%registerchoice2%"=="2" goto repeatregister2
:addnewclient
set /a newclientfile=%addlastname%-%addfirstname%
echo Last Name: %addlastname% >> %newclientfile%.txt
echo First Name: %addlastname% >> %newclientfile%.txt
goto menu
:registernewclient
set /a newclientfile=%addlastname%-%addfirstname%
echo Last Name: %addlastname% >> %newclientfile%.txt
echo First Name: %addfirstname% >> %newclientfile%.txt
echo Initial Visit: On %date%, registered at %time% >> %newclientfile%.txt
goto menu
Three answers:
keaton
2010-11-29 10:22:31 UTC
:addclient

cls

echo.

echo.

echo.

echo Add New Client

echo.

echo Enter last name and press enter.

set /p addlastname=))

echo.

echo Enter first name and press enter.

set /p addfirstname=))

set /a newclientfile=%addlastname%.%addfirstnam…

:repeatregister2

echo.

echo Would you like to register a visit for this person today?

echo 1) Yes

echo 2) No

set /p registerchoice2=))

if "%registerchoice2%"=="1" goto registernewclient

if "%registerchoice2%"=="2" goto addnewclient

goto repeatregister2

:addnewclient

set /a newclientfile=%addlastname%.%addfirstnam…

echo Last Name: %addlastname% >> "%cd%\%newclientfile%.txt"

echo First Name: %addlastname% >> "%cd%%newclientfile%.txt"

goto menu

:registernewclient

set /a newclientfile=%addlastname%.%addfirstnam…

echo Last Name: %addlastname% >> "%cd%\%newclientfile%.txt"

echo First Name: %addfirstname% >> "%cd%\%newclientfile%.txt"

echo Initial Visit: On |date|, registered at |time| >> "%cd%\%newclientfile%.txt"

goto menu





try this.....

i used %cd% to assure the file is created in the active directory. I used "quotes" to make sure that all spaces in names would not affect the directory location. I used .periods. instead of -dashes- in the file names for better compatability with batch. I also used the pipelines on date and time so the system will set the currents.It should work fine now..... i hope this helps, good day.
kurdt c
2010-11-25 21:39:42 UTC
You may want to check the syntax of this line:



set /a newclientfile=%addlastname%-%addfirstnam…



Close the % sign on the %addfirstname% variable and delete the ellipsis.
loar
2016-10-03 04:57:26 UTC
that's 'tree'. First browse to the needed itemizing using command cdaf1661b7fae8a9ce128836d6cadd2d then hit enter. then sort tree and you gets a itemizing of all information and folders in it. real click then go with mark and choose what you like then hit enter. then create new text textile report and paste. in case you like names of information n subfolders additionally then sort tree /f.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...