Get path from txt file

Imagine I have a txt file with a path like:

c:\programs\SRC_CODE\

      

How can I make a .bat file that opens a txt file and gets a string to set a variable with a path fetched from the txt?

thanks

+1


a source to share


1 answer


You have at least two options. You can use set

input redirection as well:

set /p myPath=<mypath.txt>nul

      

where it set /p

will ask for the path, but it <mypath.txt

will work as if the contents of a text file were entered directly.



You can also use a command for

that 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 in multi-line files. The first option will store the first line of the file in a variable, the option for

will use the last line. However, this is not important for single line files. Oh, and it may be that the first option may output an empty string; I added a redirect to nul

to remove this.

+7


a source







All Articles