-1

When i try to run this program it does not set the variable right. Is there anyway so that after it is done to set the file location not the file itself to a variable and have it printed on screen.

@echo off
for %%a in (d) do if exist "%%a:\" dir /b /s /a-d "%%a:\gm_construct.bsp" set p=%%~dpnxa
pause
  • 2
    Which variable is not being set right? What is it currently being set to, and what do you want it to be set to? – Ruslan Mar 26 '16 at 12:34
  • The variable p in the above code. When i try to use it other places in my code it doen't work. I want it set to the file location not the file but not sure how to do that exactly and I have no idea what it is being set to. – Slightly Koala Mar 26 '16 at 12:44
  • 2
    So you want to set it to the output of the `dir` command? Have you tested that all other parts of the code work (i.e. it actually runs the `dir` command when it finds the file)? – Ruslan Mar 26 '16 at 12:46
  • Did you ever think of searching through SO? there are numerous of questions like this... – aschipfl Mar 26 '16 at 15:21
  • 1
    Possible duplicate of [Batch - Assign Command output to Variable](http://stackoverflow.com/questions/16203629/batch-assign-command-output-to-variable) – aschipfl Mar 26 '16 at 15:25

1 Answers1

3

What your code does:

dir /b /s /a-d "%%a:\gm_construct.bsp" set p=%%~dpnxa

lists all files "%%a:\gm_construct.bsp" and all files named set and all files named p=%%~dpnxa

What (I think) you want to do:

dir /b /s /a-d "%%a:\gm_construct.bsp"

and set it's output to the variable %p%

To get a command's output, you need another for:

for /f "delims=" %%i in ('dir /b /s /a-d "%%a:\gm_construct.bsp"') do set p=%%~dpnxi

integrated into your code (note the kind of the single quotes: '):

@echo off
for %%a in (d) do (
  if exist "%%a:\" (
     for /f "delims=" %%i in ('dir /b /s /a-d "%%a:\gm_construct.bsp"') do set p=%%~dpnxi
  )
)
pause

pause

Stephan
  • 47,723
  • 10
  • 50
  • 81