DISKPART script question

Discussion in 'Scripting' started by ciscokid, Dec 23, 2021.

  1. ciscokid

    ciscokid MDL Senior Member

    Jun 3, 2007
    317
    74
    10
    I am using this rudimentary code to format hard drives in an enclosure, I can modify this since I have 2 bay and 4 bay enclosures.

    Here's the question: Whatever computer I use to run this code has different amounts of hard drives already installed. In this example, I need MBR with quick format and the drives are disk 3 and 4. In another computer, they may be 1 and 2 or 4 and 5 etc.

    I saved this as quickformat.txt. I am running it in Windows Terminal using "diskpart /s c:\temp\quickformat.txt"

    It works fine, only thing is I want to automate it to ask me for the disk numbers upon running the script instead of having to modify the script every time, can this be done?

    Other options to add would be MBR or GPT and FULL or QUICK format.

    TIA

    Code:
    select disk 3
    clean
    convert mbr
    create partition primary
    format quick fs=ntfs label="Disk 1"
    assign
    select disk 4
    clean
    convert mbr
    create partition primary
    format quick fs=ntfs label="Disk 2"
    assign
    
     
  2. boyonthebus

    boyonthebus MDL Expert

    Sep 16, 2018
    1,168
    752
    60
    I know what you are trying to do. The only thing I can think of is to write a command file which lets you input the values, then have it write all that to a text file for diskpart to call.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,390
    1,322
    120
    Why do you need a script when without it you can format with two mouse clicks but using a script with many more clicks.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. ciscokid

    ciscokid MDL Senior Member

    Jun 3, 2007
    317
    74
    10
    #4 ciscokid, Dec 24, 2021
    Last edited: Dec 24, 2021
    (OP)
    Because sometimes I need to do a full format on 4 drives in an enclosure and I don't have the time to sit there and watch it. I can use a script to let it run overnight.

    I'm also a believer that a computer should work for me instead of the other way around. I'd rather run a script and walk away.

    Isn't stuff like this all part of the fun of what we used to call "hacking" in the 80's?
     
  5. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,702
    5,104
    120
    #5 Dark Dinosaur, Dec 24, 2021
    Last edited: Dec 24, 2021
    my way of doing things.
    take arguments like E F G
    I hope its useful for you.
    credit its yours.

    Edit [1]: you should use :: setlocal enabledelayedexpansion
    Edit [2]: its clean all partitions found on this drive :D
    Edit [3]: its E not E: or E:\ just pure letter

    Code:
    :CleanDrive
    
    set diskIndex=
    set volumeIndex=
    set targetDrive=%*
    pushd %SystemDrive%
    
    >"%temp%\tmp" echo list volume
    for /f "tokens=1,2,3 delims= " %%m in ( 'diskpart /s "%temp%\tmp"') do (
        if /i 'Volume' EQU '%%m' (
            if /i '%targetDrive%' EQU '%%o' (
                REM echo Volume [%%n] Letter [%%o]
                set volumeIndex=%%n
            )
        )
    )
    
    if defined volumeIndex (
        >"%temp%\tmp" echo select volume !volumeIndex!
        >>"%temp%\tmp" echo list disk
    )
    for /f "tokens=1,2,3 delims= " %%m in ( 'diskpart /s "%temp%\tmp"') do (
        if /i '*' EQU '%%m' (
            REM echo Select Disk [%%o]
            set diskIndex=%%o
        )
    )
    
    if defined diskIndex (
        >"%temp%\tmp" echo select disk !diskIndex!
        >>"%temp%\tmp" echo clean
        >>"%temp%\tmp" echo clean
        >>"%temp%\tmp" echo Convert gpt
        >>"%temp%\tmp" echo create partition primary
        >>"%temp%\tmp" echo Select partition 1
        >>"%temp%\tmp" echo Format quick fs=ntfs
        >>"%temp%\tmp" echo ASSIGN LETTER=%targetDrive%
    )
    1>nul diskpart /s "%temp%\tmp"
    popd
    goto :eof
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. ciscokid

    ciscokid MDL Senior Member

    Jun 3, 2007
    317
    74
    10
    Dark Monkey, thank you so much for your help, I will try this tonight once I get home from work. I really appreciate it!

     
  7. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,962
    904
    60
    This is what I used in WinPE ages ago:
    Code:
    @echo off
    :: FM X - Clean, Format, Label as Data and Assign
    
    if "%1"=="/?" goto Help
    if "%1"=="" goto Help
    
    Set FMDisk=%1
    
    echo sel dis %FMDisk% >x:\tmp.txt
    echo cle >>x:\tmp.txt
    echo cre par pri >>x:\tmp.txt
    echo for quick fs=ntfs label="Data" >>x:\tmp.txt
    echo assign >>x:\tmp.txt
    diskpart /s x:\tmp.txt
    del x:\tmp.txt /q /f
    goto eof
    
    :help
    echo FM X - Clean, Format, Label as Data and Assign.
    echo.
    echo      - Where X is the Drive to format...
    echo.
    goto eof
    
    :eof
    You can change x: to %temp% and add convert to mbr or gpt...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. ciscokid

    ciscokid MDL Senior Member

    Jun 3, 2007
    317
    74
    10
    @Flipp3r thanks, will test it tonight after work!