Jump to content

Recommended Posts

Posted (edited)
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

Posted

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.

Posted

Here is the way of getting active drive letter with VBS scripting

 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim Drv  For Each Drv In Fso.Drives     If Drv.IsReady Then    WScript.Echo Drv.DriveLetter   End If    Next 
Posted

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

Posted

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 members

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