Batch script to take the output of a command and create a variable out of it

batchcmd.execommand lineenvironment-variables

For example, I want to search for a file

where /r C:\ "myfile.txt"

and then make the output of that command which would be the file path to "myfile.txt" a variable I choose,

So then the path to that file is now a variable so I could do echo %variable% and it would print the path?

Best Answer

This

c:\> for /?

... will list how for works, look for the line

 FOR /F ["options"] %variable IN (`command`) DO command [command-parameters] 

and read about it.

Now note that %variable is LOCAL to for so you need to have e.g. set F=%variable within command

C:\> for /F "usebackq" %v in (`echo Check-it-out`) do @ set F=%v
C:\> echo To do: %F%

will print To do: Check-it-out

Related Question