[solved]need help with diskpart script

Discussion in 'Scripting' started by josywong, May 28, 2014.

  1. josywong

    josywong MDL Novice

    Apr 18, 2014
    9
    1
    0
    #1 josywong, May 28, 2014
    Last edited: Jun 20, 2014
    this is my current diskpart script. im a lazy person, so i created this for legacy bios winpese + pxe. nvr thought uefi only board become so common recently(especially new laptops). so i had to add gpt support. i hav bootmgr.efi only in my pendrive if i had to boot any uefi only board using my usb3 flash disk. i added default drive letter check in too. but i think there shud b a better loop in there to: - check if default drive letter is being used - if being used, assign the 1st in drive letter variables im new to script,i do really hope i can get some help here. thanks in advance
     
  2. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,473
    625
    60
  3. josywong

    josywong MDL Novice

    Apr 18, 2014
    9
    1
    0
    #3 josywong, Jun 17, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    thanks for the suggestion. viewed and tested, i preferred mine :chair:

    Code:
    @echo off
    cls
    
    set andif=if
    set then=(
    set else=) else (
    set endif=)
    set lessequal=LEQ
    set greater=GTR
    set notequal=NEQ
    set lesser=LSS
    set greaterequal=GEQ
    set fs=mbr
    setlocal enabledelayedexpansion
    set title=format NEW disk
    
    :start
    ::check for free drive letter
    set driveletter=a b c d e f g h i j k l m n o p q r s t u v w x y z
    for /f "skip=1 tokens=1 delims=: " %%a in ('wmic logicaldisk get deviceid') do (
       set driveletter=!driveletter:%%a =!
    )
    
    ::check default drive letter
    for %%r in (%driveletter%) do (
    IF /I "%%r"=="r" set red=%%r&set driveletter=!driveletter:%%r =!
    if "!red!" == "" set red=%%r&set driveletter=!driveletter:%%r =!
    )
    for %%r in (%driveletter%) do (
    IF /I "%%r"=="s" set systemd=%%r&set driveletter=!driveletter:%%r =!
    if "!systemd!" == "" set systemd=%%r&set driveletter=!driveletter:%%r =!
    )
    for %%r in (%driveletter%) do (
    IF /I "%%r"=="c" set osd=%%r&set driveletter=!driveletter:%%r =!
    if "!osd!" == "" set osd=%%r&set driveletter=!driveletter:%%r =!
    )
    for %%r in (%driveletter%) do (
    set driveletter=!driveletter:a =!
    set driveletter=!driveletter:b =!
    set driveletter=!driveletter:c =!
    IF /I "%%r"=="d" set datad=%%r&set driveletter=!driveletter:%%r =!
    if "!datad!" == "" set datad=%%r&set driveletter=!driveletter:%%r =!
    )
    
    :gptcheck
    for %%w IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
    IF EXIST %%w:\bootmgr.efi SET fs=gpt
    %endif%
    
    title %title%
    :force
    cls
    echo Preparing drive list...
    echo.
    echo list disk|diskpart.exe
    echo.
    
    :disk
    set /p disk=Please enter disk # || goto disk
    ::check if disk exist
    ECHO. list disk | diskpart.exe | find /i "Disk %DISK%" >NUL
    IF "%ERRORLEVEL%" %notequal% "0" GOTO disk
    ::get disk size
    echo list disk | diskpart.exe | find /i "Disk %DISK%" > size.txt
    for /f "tokens=4" %%i in (size.txt) do set size=%%i
    
    ::format according to filesystem
    echo select disk %DISK% > diskpart.txt
    echo clean >> diskpart.txt
    echo convert %fs% >> diskpart.txt
    
    if "%fs%" == "gpt" %then%
    rem == 1. Windows RE tools partition ===============
    echo create partition primary size=300 >> diskpart.txt
    echo format fs=ntfs label="Windows RE tools" >> diskpart.txt
    echo assign letter="%red%" >> diskpart.txt
    echo set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac" >> diskpart.txt
    echo gpt attributes=0x8000000000000001 >> diskpart.txt
    rem == 2. System partition =========================
    echo create partition efi size=100  >> diskpart.txt
    rem    ** NOTE: For Advanced Format 4Kn drives,
    rem               change this value to size = 260 ** 
    echo format fs=fat32 label="System" >> diskpart.txt
    echo assign letter="%systemd%" >> diskpart.txt
    rem == 3. Microsoft Reserved (MSR) partition =======
    echo create partition msr size=128 >> diskpart.txt
    %else%
    echo create partition primary size=100 >> diskpart.txt
    set /a "primary+=1"
    echo format fs=ntfs label="System Reserved" >> diskpart.txt
    echo active >> diskpart.txt
    echo assign letter="%systemd%" >> diskpart.txt
    %endif%
    rem == 4. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    echo.  
    echo.              ---- OS size ----
    echo.  Minimum 16GB for x86, 20GB for x64
    echo.  Recommend 60GB (Lowest ssd size in market)
    echo.  Default to whole disk size, use only numbers
    echo.
    :ossizeaction
    set /p ossize=Input : [%size%] || set ossize=%size%
    for /f "tokens=1* delims=0123456789" %%a in ("A0%ossize:"=%") do if not "%%b" == "" goto ossizeaction
    
    if "%ossize%"=="%size%" %then%
    ::use whole disk size
    echo create partition primary >> diskpart.txt
    set /a "primary+=1"
    set d=0
    goto wpartiton
    %else%
    ::enable data disk
    set d=1
    set /a "gb=%ossize%*1024"
    %endif%
    
    :: check size, min = 16GB
    if %ossize% %lesser% 16 %then%
    echo OS size too small! should be greater or equal to 16GB
    goto ossizeaction
    %endif%
    
    if %ossize% %greater% %size% %then%
    echo OS size too big! should be lesser than %size%
    goto ossizeaction
    %endif%
    
    echo create partition primary size=%gb% >> diskpart.txt
    set /a "primary+=1"
    
    :wpartiton
    rem ==    c. Prepare the Windows partition ========= 
    echo format quick fs=ntfs label="OS" >> diskpart.txt
    echo assign letter="%osd%" >> diskpart.txt
    if "%d%" == "1" %then%
    echo create partition primary >> diskpart.txt
    set /a "primary+=1"
    echo format quick fs=ntfs label="Data" >> diskpart.txt
    echo assign letter="%datad%" >> diskpart.txt
    %endif%
    rem === 5. Recovery image partition ================
    :storage
    set /p userinp=SSD? : [n] || set userinp=n
    if "%userinp%"=="n" %then%
    set ssd=0
    set /a "recommend=%size%*5/100"
    %endif%
    
    if "%userinp%"=="y" %then%
    set ssd=1
    set /a "recommend=%size%/10"
    %endif%
    
    for /f "tokens=1* delims=ny" %%a in ("An%userinp:"=%") do if not "%%b" == "" goto storage
    
    if %recommend% %greater% 10 %then%
    set shrink=%recommend%
    set /a "unallocated=%recommend%-10"
    %endif%
    
    if %recommend% %lessequal% 10 %then%
    set shrink=10
    set recommend=10
    %endif%
    
    set /a "shrink*=1024"
    
    if "%ssd%" == "1" %then%
    call :rst
    %else%
    set /a "arecoverymb=%shrink%"
    %endif%
    
    echo shrink minimum=%shrink% >> diskpart.txt
    echo create partition primary size=%arecoverymb% >> diskpart.txt
    set /a "primary+=1"
    echo format quick fs=ntfs label="A RECOVERY" >> diskpart.txt
    
    if "%fs%" == "mbr" %then%
    echo set id=27 >> diskpart.txt
    %else%
    echo set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac" >> diskpart.txt
    echo gpt attributes=0x8000000000000001 >> diskpart.txt
    %endif%
    
    if "%rst%" == "1" %andif% "%fs%" == "mbr" %then%
    echo create partition primary >> diskpart.txt
    set /a "primary+=1"
    echo set id=84 override >> diskpart.txt
    %endif%
    
    if "%rst%" == "1" %andif% "%fs%" == "gpt" %then%
    echo create partition primary >> diskpart.txt
    set /a "primary+=1"
    echo set id=D3BFE2DE-3DAF-11DF-BA40-E3A556D89593 >> diskpart.txt
    %endif%
    
    if "%fs%" == "mbr" %andif% %primary% %greaterequal% 5 %then%
    echo mbr cant create more than 4 primary partition
    echo fs have been changed to GPT
    set fs=gpt
    pause >NUL
    goto force
    %endif%
    
    :check
    cls
    echo.
    echo partition table    : %fs%
    if "%ssd%" == "0" %then%
    echo storage type       : HDD
    %else%
    echo storage type       : SSD
    %endif%
    if "%d%" == "0" %then%
    echo data partition     : N/A
    %else%
    echo data partition     : %datad%:\
    %endif%
    set /a "arecoverygb=%arecoverymb%/1024"
    echo recovery partition : %arecoverygb% GB
    if "%rst%" == "1" %then%
    echo RST partition      : %memorygb% GB
    %endif%
    echo.
    :checkaction
    echo.Yes to continue, Capital 'Y', lower case 'es'
    echo.or change fs by 'gpt' or 'mbr'
    echo.
    set /p check=case sensitive! : || set check=blank
    
    if "%check%"=="Yes" %then%
    goto next
    %endif%
    
    if "%check%"=="gpt" %then%
    set fs=gpt
    goto force
    %endif% 
    
    if "%check%"=="mbr" %then%
    set fs=mbr
    goto force
    %endif%
    
    if "%check%" %notequal% "Yes" %andif% "%check%" %notequal% "gpt" %andif% "%check%" %notequal% "mbr" %then%
    goto check
    %endif%
    
    :next
    Diskpart.exe /s diskpart.txt
    
    :exit
    exit
    
    :rst
    set /p userinp=Intel Rapid Start Technology? : [y] || set userinp=y
    if "%userinp%"=="n" %then%
    set rst=0
    set /a "arecoverymb=%shrink%"
    GOTO :eof
    %endif%
    
    if "%userinp%"=="y" %then%
    set rst=1
    %endif%
    
    for /f "tokens=1* delims=ny" %%a in ("An%userinp:"=%") do if not "%%b" == "" goto rst
    
    :ramcheck
    set /p memorygb=total RAM size(GB) : || set memorygb=blank
    for /f "tokens=1* delims=0123456789" %%a in ("A0%ossize:"=%") do if not "%%b" == "" goto ramcheck
    
    set /a "memorymb=%memorygb%*1024"
    set /a "totalmb=%memorymb%+5120"
    if %shrink% %lesser% %totalmb% %then%
    set shrink=%totalmb%
    %endif%
    set /a "arecoverymb=%shrink%-%memorymb%"