i need it to cd to the directory
Why? A complete path to the file works better.
REM This batch will find a folder ( directory ) and
REM cd to that directory
REM USAGE: MSDIR MSInfo
@echo off
dir /s /b %1
dir /s /b %1 > ms.txt
echo ms.txt
echo.
type ms.txt
set /p var=< ms.txt
echo var = %var%
echo.
echo cd to %1 using variable
cd "%var%"
Output:
C:\>MSDir.bat MSinfo
C:\>REM This batch will find a folder ( directory ) and
C:\>REM cd to that directory
C:\>REM USAGE: MSDIR MSInfo
C:\Program Files\Common Files\Microsoft Shared\MSInfo
C:\WINDOWS\msapps\msinfo
ms.txt
C:\Program Files\Common Files\Microsoft Shared\MSInfo
C:\WINDOWS\msapps\msinfo
var = C:\Program Files\Common Files\Microsoft Shared\MSInfo
cd to MSinfo using variable
C:\Program Files\Common Files\Microsoft Shared\MSInfo>