Imagine I have a txt file with a path like:
c:\programs\SRC_CODE\
How can I do a .bat file that open the txt file and get the string in order to set a variable witht the path catched from the txt?
thanks
Imagine I have a txt file with a path like:
c:\programs\SRC_CODE\
How can I do a .bat file that open the txt file and get the string in order to set a variable witht the path catched from the txt?
thanks
You have at least two possible options. You can either use set
and input redirection:
set /p myPath=<mypath.txt>nul
where set /p
will prompt for the path and the <mypath.txt
will actually work as if the contents of the text file where input directly.
You also can use the for
command which can iterate over lines in a text file:
for /f "tokens=*" %%x in (mypath.txt) do set myPath=%%x
Both methods actually have slightly different semantics on files with multiple lines. The first variant will store the first line of the file in the variable, the for
variant will use the last line. Shouldn't matter for single-line files, though. Oh, and it could happen that the first variant might output an empty line; I've added a redirect to nul
to void that.