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?

1 Answer 1



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

  • Additional comment: If you do not have any specific reason to restrict yourself to CMD.EXE, then a peek into more capable scripting languages might be worthwhile. PowerShell is one, and there is many more of them...
    – Hannu
    Jul 24, 2014 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.