Batch Script Help - Parsing Data to Variable
Hey guys,

I have a batch script that currently works to pull the name of a folder on a system that meets criteria, as follows:
@echo off
SET DIR=---

FOR /D %%d IN ("C:\BEW-*") DO (
  SET DIR=%%d
)

IF EXIST "%DIR%" (GOTO WINDIR) ELSE (GOTO NOWINDIR)

:WINDIR
  ECHO Yes
  ECHO %DIR%
  GOTO END

:NOWINDIR
  ECHO No

:END
What I am trying to do is get the %DIR% to ONLY be the name of the directory and not the full path of C:\<DIRECTORY>. I am rusty as hell with batch file logic but have to use it due to a load of ancient servers I will be running this against. Can anyone point me in the right direc
June 18th, 2015 2:36pm

Do you mean:

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 3:10pm

No dice, using %%~nxd (or %%~nxD) ends up giving me a "no" response which tells me the %DIR% variable isn't being populated with anything. How would I go about testing that mask change? Is there somewhere online I can read through the different options? By mask change I mean the "%%~nxd" bit. I'm not sure what it'd be called
June 18th, 2015 4:28pm

Sorry, then I don't understand your question. %%~nxV returns the name and extension of the referenced file (or directory name). See for /f for more information.

It would be helpful if you would describe what your script is actually supposed to accomplish.

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 4:35pm

I ended up sorting it out using the FOR /F help info and reading further on variable manipulation. All I had to do was change the line ECHO %DIR% to ECHO %DIR:~3% and it would trim the first 3 characters form the variable %DIR% which, in this case, was the C:\. Thanks for your help
June 18th, 2015 5:07pm

Your original statement said that you wanted only the directory name. You are wanting the full directory name without the drive letter. Two differe
Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 5:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics