K, all you asked though was if it's possible, and didn't post any code for us to correct or suggest on. I'll just write it anyways...
Minimal error-handling, without enabledelayedexpansion:
clients.txt
choose.cmd (or .bat)
Code: Select all
@echo off&echo:&color 0B&title Client Select ^& Add by orange_batch
for /f "delims=" %%x in (clients.txt) do (
set /a counter+=1
call set "array[%%counter%%]=%%x"
)
set /a counter+=1
set array[%counter%]=Company Not Listed
echo: ^> Choose from the following:
echo:
for /l %%x in (1,1,%counter%) do call echo: %%x. %%array[%%x]%%
echo:
set /p "choice= Input: "
echo:
call echo: You chose %%array[%choice%]%%.
echo:
:: if %choice%==1 echo: Do whatever for client Microsoft.
:: if %choice%==2 echo: Do whatever for client Apple.
set /a last=%counter%,counter-=1
if %choice%==%last% (
echo: ^> Enter the name of the company to add.
echo:
set /p "new= Input: "
echo:
type nul>clients.txt
for /l %%x in (1,1,%counter%) do call echo:%%array[%%x]%%>>clients.txt
call echo:%%new%%>>clients.txt
)
echo: ^> Press any key to exit.
pause>nul
exit
Notes for learning:
-The
^ caret character escapes individual command characters (one-time only, beware of use in variables).
-Command Prompt does not process command characters surrounded with quotation marks, so
set "var=^&|" works where
set var=^&| would not. There's no need to use quotes with
set /p, but you wouldn't be able to see the trailing space character in the code otherwise.
-
call %%variable%% is like
!variable! but without delayed expansion.
-You can set multiple variables using
set /a.
set /a last=%counter%,counter-=1 is the same as
set /a last=%counter%&set /a counter-=1.
-
echo:whatever>clients.txt a single arrow mark will overwrite the target file.
-
echo:whatever>>clients.txt the double arrow marks append to the target file.
-
type nul>clients.txt clears a text file.
echo:>clients.txt would "clear" it but leave a line return.
Bonus:
-You can write multiple commands on the same line by separating them with
&.
-You can pipe the output of one command into another (that can accept it) using
|, like
echo:orange|set /p choice=Apple or orange?-You can handle the success or failure of a command using
&& and
||, like
set /a number+=22222222222&&echo:Calculation successful.||echo:Failed. Number is larger than 32-bits.. Beware that
|| has priority over
&& (like a separator) and the first-come-first-serve logic behind this. The command to do on failure will trigger if the command to do on success fails as well, like
set /a number+=10&&set /a number+=22222222222||echo:Failed. Number is larger than 32-bits.. Putting
|| before
&& will trigger the
&& command if the
|| command is successful, not the first command. However, like math, you can group commands using parentheses
(). For example,
set /a number+=22222222222&&(echo:Calculation successful.||echo:"Calculation successful" echo failed.).