Jump to content

Get first letter of drive

Recommended Posts

hi I'm trying to get the first letter for a drive letter from each line using for command


for example : a line in paths.txt

"E:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"


setlocal enabledelayedexpansion
for /f "tokens=*" %%A in (paths.txt) do (
set drive=%%~dA
If Defined drive (
set drive=%drive:~0,1%
echo %drive%

I can't get the command part to work 


I appreciate any help on this



Edited by sweept
Link to comment
Share on other sites

The %variables% inside the rounded braces will be evaluated before executing the code line so they are empty. Using delayed expansion !variables! allows them to expand at execution time so they are updated at that time. So changing the % to ! makes your code work.

setlocal enabledelayedexpansionfor /f "tokens=*" %%A in (paths.txt) do (  set drive=%%~dA  If Defined drive (    set drive=!drive:~0,1!  )  echo !drive!)

So a minor change and it works. I get the letter E echoed.

Link to comment
Share on other sites

Strictly speaking there is no actual *need* to use delayed expansion.
I would write it like this:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=1 delims=;" %%A in (paths.txt) DO CALL :get_drive %%AECHO ENDGOTO :EOF:get_driveSET drive=%~d1SET driveletter=%drive:~0,1%IF DEFINED drive ECHO Drive found %driveletter%: in path %1GOTO :EOF

Please note how there is a need (no matter if using a subroutine like in the above or using delayed expansion inside the FOR loop) to make sure that the path/filename is valid, because the ~d will expand  to "current drive" for non-existing files (without the leading "<driveletter>:")
As an example, depending on the context, an additional test like:

FOR /F "tokens=1 delims=;" %%A in (paths.txt) DO IF EXIST %%A CALL :get_drive %%A

may be advised.
Example paths.txt:

"D:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" ; NON EXISTING file
"C:\batches\temp.txt" ; EXISTING file (on EXISTING drive)
"K:\nothing\" ; NON EXISTING drive
thefilethatis.not ;NON EXISTING file without full path
"E:\something"; EXISTING drive assigned to a multi-card reader (without media)

Please note the potential issue with the last item, compare with:
the ~d expansion will produce, just like IF EXIST, a "No disk" error.

Another approach would be direct parsing of the string.
Something *like*:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=1 delims=\" %%A in (paths.txt) DO CALL :get_drive %%~AECHO ENDGOTO :EOF:get_driveSET drive=%1IF %drive:~1,1%==: SET driveletter=%drive:~0,1%IF DEFINED driveletter ECHO Drive found %driveletter%:SET driveletter=GOTO :EOF

might all in all be more suited.


Link to comment
Share on other sites

I wouldn't even bother with the "If Defined", just make sure the command is only outputting lines containing drive letters!

@Echo OffSetLocalFor /F "Delims=" %%A In ('FindStr [A-Z]:\\ "%~dp0paths.txt"') Do (    Set "drive=%%~dA"    Call Set "drive=%%drive:~,1%%"    Call Echo( %%drive%%)Pause

Or, instead of using drive letter expansion, just look for the letter preceeding the first colon (:)

@Echo OffSetLocalFor /F "Delims=:" %%A In ('FindStr [A-Z]:\\ "%~dp0paths.txt"') Do (    Set "drive=%%~A"    Call Set "drive=%%drive:~-1%%"    Call Echo( %%drive%%)Pause
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...