Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Get first letter of drive

Featured Replies

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

 

TIA

Edited by sweept

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.

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:
http://www.msfn.org/board/topic/137714-install-xp-from-a-ram-loaded-iso-image/?p=895361
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.



jaclaz

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

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.