Assuming I understood you correctly (I'm somewhat brain dead right now) but you want to set the output of ' dir /d MyOFFSetupFiles | findstr "\<Directory of\>" ' as the value of a variable.
You were correct in your assumption that you need a For loop. I'll post the code below and walk you through how it works.
Assuming your command gives the output: "Directory of C:\MyOFFSetupFiles" without the quotes.
:: So first we must enable delayed expansion, so we can change and use the value of variables inside the for loop.
setlocal EnableDelayedExpansion
:: Now we start the first for loop. This one will seperate the output of the command into two parts based on the possition of (:).
:: This will allow us to isolate the path, (which will be stored as %%B for the duration of the loop.
for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (
:: Now to deal with the fact that the drive is in the other segment, we must seperate that segment of output further until we only
:: have the drive letter. To do this we use another for loop. This time we seperate the string %%A (the first half of the original output)
:: based on the possition of spaces, only holding onto the first three segments. They will be stored in %%G, %%H, %%I respectively.
:: Now since the drive letter is found singularly in the 3rd segment, we move that into a variable until we can join it up with our
:: path (stored in %%B)
for /f "tokens=1-3 delims= " %%G in ("%%A") do (
set "findPath_drv=%%I:"
)
:: To complete the operation, we combine the two variables into a single one, and empty our temporary variable used to store
:: the drive letter
set findPath=!findPath_drv!%%B
set findPath_drv=
)
:: This is simply to allow us to test to see if the operation was completed successfully.
echo %findpath%
pause
This is possible to do without using 'setlocal EnableDelayedExpansion' via this code:
for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<Directory of\>"') do (
for /f "tokens=1-3 delims= " %%G in ("%%A") do (
set "findPath=%%I:%%B"
)
)
echo %findpath%
pause
Lemonilla;
PLease ignore the following!! DUH!! I labelled my file with extension "txt" instead of "bat". I have got to get some sleep!! Sorry.
Your c ode worked great. Now I have to build on it. Thanks again
I tried your code but was surprised to get the response I did. Any ideas of what I did wrong here? See below:
Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\>code: [select]
'code:' is not recognized as an internal or external command,
operable program or batch file.
C:\>setlocal EnableDelayedExpansion
C:\>for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\,D
irectory of\>"') do (for /f "tokens=1-3 delims= " %%G in ("%%A") do (set "findpa
th=%%I:%%B"))
%%A was unexpected at this time.
C:\>echo %findpath%
%findpath%
C:\>dir /d MyOFFSetupFiles ^| findstr "\,Directory of\>"
The system cannot find the file specified.
C:\>dir /d MyOFFSetupFiles | findstr "\
Directory of C:\MyOFFSetupFiles
C:\>setlocal EnableDelayedExpansion
C:\>for /f "tokens=1,2 delims=:" %%A in ('dir /d MyOFFSetupFiles ^| findstr "\<D
irectory of\>"') do (
%%A was unexpected at this time.
C:\>for /f "tokens=1-3 delims= " %%G in ("%%A") do (set "findPath=%%I:%%B"))
%%G was unexpected at this time.
C:\>echo %findpath%
%findpath%
C:\>pause