Tip of the Week: Parsing the WDIR Variable

Blog Post created by ken_foster on Oct 25, 2014

If you have automation for removing the local WDIR in your Windows environment, then you know the challenge is that %WDIR% can be made of up several paths, like this one:




In most circumstances all you really want to remove is the first folder in the path, the local WDIR. You could hard code the paths to it in your script, but then you'd have to depend on everyone using a standardized name or passing it as an argument.


Here is some nifty code you can but into your batch script that will parse the %WDIR% variable, get the first path name (the local WDIR path), and assign it to a variable:


:: These commands are required so that variable expansion works in your script environment

setlocal enableextensions enabledelayedexpansion


:: Echo WDIR, then using ; as a delimiter, get the first token and assign its value to MY_WDIR

for /f "usebackq tokens=1,2 delims=; " %%w in (`echo %WDIR%`) do set MY_WDIR=%%w


:: Now that we've isolated the local WDIR folder, we can remove the folder

rmdir /s /q %MY_WDIR%


There you have it. I hope this tip is useful.