Access to a disk drive using volume ID in batch

Discussion in 'Scripting' started by mephistooo2, Jun 2, 2022.

  1. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    With the powershell command, I can find the volume id using the label of a partition without a drive letter.

    Code:
    ls -l (Get-Volume | ? FileSystemLabel -eq "mylabel").Path
    
    
    But I want to run this command with a bat file and assign the output "Directory" to a variable.

    How is this done?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,197
    84,762
    340
    Code:
    for /f "tokens=2 delims={}" %%# in ('powershell -nop -c "(Get-Volume | ? FileSystemLabel -eq 'mylabel').Path"') do set "vid=%%#"
    
     
  3. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    old-fashioned wmic since it's back from the dead ;)
    used twice to accommodate labels with spaces, wmic column order is alphabetic (still faster than alternatives)
    Code:
    set label=ESD
    for /f "tokens=1 delims= " %%s in ('wmic path Win32_Volume get DeviceID^,Label /format:table ^| findstr /c:%label%') do set "DeviceID=%%s"
    for /f "tokens=2 delims= " %%s in ('wmic path Win32_Volume get DeviceID^,Name /format:table ^| findstr /c:%DeviceID%') do set "vid=%%s"
    echo;[%vid%]
    
    but it's doable without powershell or wmic as well, via fsutil:
    Code:
    set label=ESD
    for /f %%a in ('fsutil volume list') do for /f "tokens=4" %%V in ('fsutil volume queryLabel %%a ^| findstr /c:%label%') do set "vid=%%~V"
    echo;[%vid%]
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0

    @abbodi1406 The command you gave did not fully meet my request, but I achieved the result I wanted by string concatenation, thank you very much.

    Code:
    @echo off
    
    for /f "tokens=2 delims={}" %%# in ('powershell -nop -c "(Get-Volume | ? FileSystemLabel -eq 'mylabel').Path"') do set "usb=%%#"
    SET a=\\?\Volume{
    SET b=}\
    SET c=%a%%usb%%b%
    
    echo %c%
    start %c%
    pause
    @BAU also thank you for the alternative commands.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    Masters, I have one more question.

    How can I find the disk number of a disk whose drive letter we know?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,734
    5,179
    120
    #6 Dark Dinosaur, Jun 3, 2022
    Last edited: Jun 3, 2022
    Demo code.
    Modify to your needs.

    Edit
    got a better answer
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    No need to format.

    For example, I need to find the disk number of a disk with drive letter H
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    @Dark Dinosaur, always assume the user won't be able to modify and filter whatever code you drop on them (specially something dangerous like above), so take the time and do it yourself to make the answer clear and "on the money".

    @mephistooo2, probably something like this would do:
    Code:
    set drive="H:\"
    
    for /f delims^=^"^ tokens^=2 %%s in ('wmic path win32_logicaldisktopartition get Antecedent^,Dependent /format:table ^| findstr "%drive%"') do set part=%%s
    for /f "tokens=2 delims=# " %%s in ('echo;%part%') do set disk=%%s
    
    echo;%disk%
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    @BAU

    Perfect, thank you so much...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,734
    5,179
    120
    @BAU about that, you right. totally agree.
    what do you say, replace my diskpart Code with WMIC?
    I don't know what happens with MS these days.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    @BAU

    Master can we find this without wmic command?

    You know, wmic will be removed in future versions of Windows.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    Microsoft tried to deprecate wmic but failed. They themselves use a ton of admin scripts relying on it. So, it's back. De ja vu with bitsadmin, wscript and others..
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    I didn't know that, good news...

    Thanks a lot again...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,734
    5,179
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    Anyway, powershell can do everything wmic does, but it's just more verbose and slower:

    Code:
    set drive=H:\
    
    set rel=__RELPATH LIKE '%%%drive%\%%'
    for /f %%s in ('powershell -nop -c "((gwmi win32_logicaldisktopartition -Filter $env:rel).Antecedent -split '<?Disk #(\d+)')[1]"') do set disk=%%s
    
    echo;%disk%
    
    PS: Drop the superlatives, please
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0

    wmic command was working perfectly but I think variable is not working in this command

    A warning appears in the form of "ObjectCommand"

    [​IMG]

    Here are some of my code content:

    Code:
        call :MsgBox "In Ventoy setup, you must select 'Partition Configuration' from the 'Option' section and allocate 50 MB of space for Grub2FM with the 'Preserve some space at the end of the disk' option."  "VBInformation" "..:: ATTENTION ::.."
    
        cd /d %~dp0Ventoy
        Ventoy2Disk.exe
        cd /d %~dp0files >nul
        call :NTFSLetter
        Letter.exe "-Ventoy" %ntfsdrive%: /Force >nul
    
        cd /d %~dp0
        set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
        > "%scriptFile%" (
        echo LIST VOLUME 
        echo SELECT VOLUME %ntfsdrive%
        echo FORMAT QUICK FS=NTFS LABEL="Ventoy"     
        echo ASSIGN LETTER=%ntfsdrive%
        )
        type "%scriptFile%" >nul
        echo     Converting Ventoy ExFAT partition to NTFS format...
        diskpart /s "%scriptFile%" >nul
        del /q "%scriptFile%" >nul
        echo. 
      
        TIMEOUT /T 2 >nul
        mkdir %ntfsdrive%:\ventoy >nul
        cd /d %~dp0 >nul
        echo     Copying the ventoy_grub.cfg file to the NTFS partition of the disk...
        robocopy files %ntfsdrive%:\ventoy ventoy_grub.cfg >nul
        echo.
        echo     Copying the ventoy_wimboot.img and ventoy_vhdboot.img files to the NTFS partition of the disk
        robocopy files %ntfsdrive%:\ventoy ventoy_wimboot.img >nul
        robocopy files %ntfsdrive%:\ventoy ventoy_vhdboot.img >nul
        TIMEOUT /T 2 > nul
    
        cd %~dp0
        for /f "tokens=2 delims={}" %%# in ('powershell -nop -c "(Get-Volume | ? FileSystemLabel -eq 'VTOYEFI').Path"') do set "usb=%%#"
        SET a=\\?\Volume{
        SET b=}\
        SET c=%a%%usb%%b%
        md fix >nul
        mountvol %~dp0fix %c% >nul
        echo.
        echo     Fixing mbr boot file from Grub2FM to Ventoy...
        robocopy files fix\grub\i386-pc core.img >nul
        mountvol %~dp0fix /d >nul
        rmdir fix >nul
    
        ::set drive="%ntfsdrive%:\"
        ::for /f delims^=^"^ tokens^=2 %%s in ('wmic path win32_logicaldisktopartition get Antecedent^,Dependent /format:table ^| findstr "%drive%"') do set part=%%s
        ::for /f "tokens=2 delims=# " %%s in ('echo;%part%') do set disk=%%s
      
        set drive="%ntfsdrive%"
        set rel=__RELPATH LIKE '%%%drive%\%%'
        for /f %%s in ('powershell -nop -c "((gwmi win32_logicaldisktopartition -Filter $env:rel).Antecedent -split '<?Disk #(\d+)')[1]"') do set disk=%%s
        echo;%disk%
    
        call :FAT32Letter
        set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
        > "%scriptFile%" (
            echo LIST DISK 
            echo SELECT DISK %disk%
            echo CREATE PARTITION PRIMARY SIZE=49
            echo FORMAT QUICK FS=FAT32 LABEL="GRB2FM_BOOT"     
            echo ASSIGN LETTER=%fat32drive%
        )
        type "%scriptFile%" > nul
        echo.
        echo     Formatting the Free Space of the Disk...
        diskpart /s "%scriptFile%" >nul
        del /q "%scriptFile%" >nul
        echo. 
        echo     Copying Grub2FM files...
        robocopy Grub2FM %fat32drive%:\ /E >nul
      
        set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
        > "%scriptFile%" (
            echo SELECT VOLUME %fat32drive%
            echo REMOVE LETTER %fat32drive%
            echo SET ID = 27 OVERRIDE
            echo SET ID = EF OVERRIDE
        )
        type "%scriptFile%" >nul
        diskpart /s "%scriptFile%" >nul
        del /q "%scriptFile%" >nul
    
        call :MsgBox "Ventoy & Grub2FM MultiBoot Installation completed."  "VBInformation" ".:: Ventoy & Grub2FM MultiBoot ::."
        echo.
        echo     OK
        echo.
        choice /C:MX /N /M "Press the X button for EXIT -- Press M button for MAIN MENU : "
        if errorlevel 2 Exit
        if errorlevel 1 goto :Main
    
    ::===============================================================================================================
    :NTFSLetter
        set "ntfsdrive="
        for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set ntfsdrive=%%a
        exit /b 0
    ::===============================================================================================================
    :FAT32Letter
        set "fat32drive="
        for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set fat32drive=%%a
        exit /b 0
    ::===============================================================================================================
    :MsgBox
        Rem 64=vbInformation, 48=vbExclamation, 16=vbCritical 32=vbQuestion
        setlocal enableextensions
        set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
        >"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
        set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
        endlocal & exit /b %exitCode%
    ::===============================================================================================================
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. Enthousiast

    Enthousiast MDL Tester

    Oct 30, 2009
    47,256
    94,677
    450
    Was it really removed? Iirc it was only missing on a few IP builds created from UUP, after the apps were integrated again all was back, including wmic. Afaik it was never missing on IP TB ISOs, or am i incorrect?
     
  18. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    I was under the impression the wmic_commandline_utility etc. package was removed from the some_edition.esd itself for couple dev builds.

    Not to break your wings, but authoring complex scripts requires more than minimal knowledge, you must be aware of any quirks, specially when combining languages (batch, vbscript, powershell) - you can't just rely on others to do it for you.
    You haven't noticed that the powershell version has no quotes around the drive value.
    The wmic one works with or without, but the powershell one cannot, as passing quotes across languages is not fun. Every little detail matters when string manipulation is involved
    I usually surround all batch variables under quotes or followed by &; to be unambiguous:
    set "drive=H:\"
    Which I should have done on both versions. The assumption is that you get that drive letter from some other command output, usually in the format letter : \
    So, try with set "drive=%ntfsdrive%:\"
    Edit: Because you use %ntfsdrive% that is just a single letter
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. mephistooo2

    mephistooo2 MDL Junior Member

    Feb 5, 2008
    69
    111
    0
    Yes, I didn't notice the quotation marks, I'll be more careful from now on.

    Thanks a lot again...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    And the pm requested "find the disc number of a disc whose drive label we know"
    Code:
    set "label=MEDIA"
    
    set q1= $Q1=(gwmi win32_logicaldisk -filter $('VolumeName='+[char]34+$env:label+[char]34)).DeviceID
    set q2= $Q2=(gwmi -Query $('Associators of {win32_logicaldisk.DeviceID='+[char]34+$q1+[char]34+'} Where AssocClass=win32_logicaldisktopartition')).Name
    for /f %%s in ('powershell -nop -c "%q1%; if ($Q1) {%q2%; ($Q2 -split '<?Disk #(\d+)')[1]}"') do set disk=%%s
    
    echo %disk%
    
    pause
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...