So I was wondering if there is a way to make a batch file read a separate text document and convert specific lines of code into a variable with the same value given, as from the document. So make the batch script read the text document, and use the information in there to create it's own variable. eg.
TEXT.txt:
test==a
test2==b
test3==c
and then the batch script would read that text and convert it into usable variables with the same values as given from the TEXT.txt document
Like so: Usable batch variable:
%test% is "a"
%test2% is "b"
%test3% is "c"
and so on
And also, would it be possible for the batch file to read a user input using set /p opt= Userinput:
and then take user input and convert every letter typed into a seperate individual variable?
eg.
User types in: "hello" when prompted with set /p opt= Userinput:
and batch script converts user input into:
%letter1% is "h"
%letter2% is "e"
%letter3% is "l"
%letter4% is "l"
%letter5% is "o"
And would it also be possible for the batch script to read spaces in user input like if user input was "hi all" and then the batch script would say
%letter1% is "h"
%letter2% is "i"
%letter3% is "0"
%letter4% is "a"
%letter5% is "l"
%letter6% is "l"
To get the content as shown in your example TEXT.txt
, you could just use a For /F
loop and use the =
character as the delimiters:
@For /F "Tokens=1*Delims==" %%G In ('find "=" ^<"TEXT.txt"')Do @Set "%%G=%%H"
Although, I'd prefer not to rely upon %PATH%
and %PATHEXT%
:
@For /F "Tokens=1,* Delims==" %%G In ('%SystemRoot%\System32\find.exe "="
0^<"TEXT.txt"') Do @Set "%%G=%%H"
As for your more interesting question, the batch-file below attempts to save each character of a users input to separate variables.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
:GetInput
Set "Input="
Set /P "Input=Enter your string here>"
If Not Defined Input GoTo GetInput
For /F "Delims==" %%G In ('"(Set Char[) 2> NUL"') Do Set "%%G="
Set "i=0" & For /F Delims^=^ EOL^= %%G In ('(%SystemRoot%\System32\cmd.exe /V /U
/S /D /C "(Echo=!Input!)"^) ^| %SystemRoot%\System32\find.exe /V ""') Do (
Set /A i += 1 & SetLocal EnableDelayedExpansion
For %%H In (!i!) Do EndLocal & Set "Char[%%H]=%%G")
(Set Char[) 2>NUL
Pause
The last two lines are added just to give you some visual feedback of what has been done, you would obviously use your own code there…