Search code examples
batch-filewinpe

Batch file to find USB drive and change drive letter


I'm trying to automate our imaging process. We have multiple computer models with different images all stored on one USB drive with WinPE. I have gotten as far as creating a script that with give me options using Choice to select which computer I want to image but the problem is that depending on what computer I'm imaging the usb drive's letter changes. I'm looking to create a batch file that will find the USB drive and change the drive letter to say "X:" so that the Choice script can run properly with a static drive letter to look for the image files.

I search and found a script that will find the USB drive by name "Ace" and output the drive letter but havent been able to figure out how to take that drive and change it to "X:"

for /f "tokens=3 delims= " %%A in ('echo list volume ^| diskpart ^| findstr "Ace"') do (set Acedrive=%%A)

What I have so far is this:

echo off
cls
Echo Image Selection Menu
Echo =======================
Echo 1 Dell D820
Echo 2 Dell E6510
Echo 3 Dell E6520
Echo 4 Lenovo T431S
Echo 5 Lenovo T440S
Echo 6 Lenovo M73 Tiny
Echo =======================
Choice /C 123456 /M "What computer are you trying to image?"
If Errorlevel 6 goto 6
If Errorlevel 5 goto 5
If Errorlevel 4 goto 4
If Errorlevel 3 goto 3
If Errorlevel 2 goto 2
If Errorlevel 1 goto 1

Goto End

:6
cls
imagex /apply M73.wim 1 g:
Goto End

:5
cls
imagex /apply T440S.wim 1 g:
Goto End

:4
cls
imagex /apply T431S.wim 1 g:
Goto End

:3
cls
imagex /apply E6520.wim 1 g:
Goto End

:2
cls
imagex /apply E6510.wim 1 g:
Goto End

:1
cls
imagex /apply D820.wim 1 g:
:End

So This basically just gives me options to choose what computer I'm imaging then runs the associated imagex command for that wim. Any help would be greatly appreciated!

Thanks


Solution

  • Give a try this code:

    for /f "tokens=2,3 delims= " %%A in ('echo list volume ^| diskpart ^| findstr "Ace"') do (
    set Acedrive=%%B
    (echo select volume %%A
    echo assign letter=X) | diskpart
    )
    

    hope you find it helpfull.