Here is a simple solution that uses the convenient hybrid JScript / batch utility REPL.BAT , which searches for and replaces regular expressions with stdin and writes the result to stdout. This is a clean script that runs on any modern Windows machine with XP and beyond - no third-party executable file is required.
It has many options, including option A , which displays only matching strings, and S , which are read from the environment variable instead of stdin. Full documentation is built into the script.
Assuming REPL.BAT is in your current directory or, even better, somewhere inside your path, the following sets the value accordingly. It will be undefined if \branches\ cannot be found, or if nothing follows \branches\ :
set "mypath=c:\test\branches\9.1\_build" set "value=" for /f "eol=\ delims=" %%A in ( 'repl.bat ".*\\branches\\([^\\]*).*" "$1" AS mypath' ) do set "value=%%A" echo %value%
dbenham
source share