@echo off
setlocal enabledelayedexpansion
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

echo ============================================
echo    分区工具   by：知足常樂  Ver：26.8
echo ============================================
echo.

:retry
cls
echo 正在获取磁盘信息...
(
echo list disk
echo exit
) > %temp%\listdisk
diskpart /s %temp%\listdisk
set "disknum="
echo 请直接按数字键选择磁盘（0-9）：
for /f "delims=:" %%a in ('findstr /n "^" "%temp%\listdisk"') do set /a total=%%a
choice /c 0123456789 /n /m ">"
set /a disknum=!errorlevel!-1

:select_partition_type
echo.
echo ============================================
echo           选择分区类型
echo ============================================
echo 0. 返回重新选择磁盘
echo 1. 系统分区 GPT（带EFI）
echo 2. 系统分区 MBR（带300MB引导）
echo 3. 存储分区 GPT（无EFI）
echo 4. 存储分区 MBR（无引导分区）
echo.
echo 请选择分区类型（按0-4）：
choice /c 01234 /n /m ">"
if %errorlevel% equ 1 (
    echo 返回重新选择磁盘...
    goto retry
) else if %errorlevel% equ 2 (
    set partition_type=gpt
    set is_system_disk=yes
    echo 选择系统分区GPT
) else if %errorlevel% equ 3 (
    set partition_type=mbr
    set is_system_disk=yes
    echo 选择系统分区MBR
) else if %errorlevel% equ 4 (
    set partition_type=gpt
    set is_system_disk=no
    echo 选择存储分区GPT
) else if %errorlevel% equ 5 (
    set partition_type=mbr
    set is_system_disk=no
    echo 选择存储分区MBR
)

echo.
echo 请选择分区容量：
echo 0. 退出
echo 1. 1个分区（使用全部容量）
echo 2. 60GB
echo 3. 100GB
echo 4. 120GB
echo 5. 200GB
echo 6. 300GB
echo 7. 500GB
echo 8. 自定义容量
echo 9. 清除磁盘（删除所有分区）

:retry_choice
set /p choice=请输入选择 (0-7): 

if "%choice%"=="0" (
    echo 退出程序
    exit /b
) else if "%choice%"=="1" (
    set size=
    set second_partition=no
    echo 创建1个分区（使用全部容量）
) else if "%choice%"=="2" (
    set size=61442
    set second_partition=yes
    echo 60GB
) else if "%choice%"=="3" (
    set size=102402
    set second_partition=yes
    echo 100GB
) else if "%choice%"=="4" (
    set size=122882
    set second_partition=yes
    echo 120GB
) else if "%choice%"=="5" (
    set size=204802
    set second_partition=yes
    echo 200GB
) else if "%choice%"=="6" (
    set size=307202
    set second_partition=yes
    echo 300GB
) else if "%choice%"=="7" (
    set size=512002
    set second_partition=yes
    echo 500GB
) else if "%choice%"=="8" (
    set /p input_gb=请输入自定义容量（GB为单位，输入数字）: 
    set /a size=input_gb * 1024 + 2
    set second_partition=yes
    echo 自定义容量: %input_gb% GB = %size% MB
) else if "%choice%"=="9" (
    set operation=clean_only
    echo 仅清除磁盘
) else (
    echo 无效选择：%choice%，请输入0-9之间的数字
    goto retry_choice
)

(
echo select disk !disknum!
if "!operation!"=="clean_only" (
    echo clean
) else (
echo clean

if "!partition_type!"=="mbr" (
    echo convert mbr
    if "!is_system_disk!"=="yes" (
        echo create partition primary size=300
        echo format quick fs=fat32
    )
    if "!size!"=="" (
        echo create partition primary
    ) else (
        echo create partition primary size=!size!
    )
    echo format quick fs=ntfs
    echo assign
    if "!second_partition!"=="yes" (
        echo create partition extended
        echo create partition logical
        echo format quick fs=ntfs
        echo assign
    )
) else (
    echo convert gpt
    if "!is_system_disk!"=="yes" (
        echo create partition efi size=300
        echo format quick fs=fat32 label="EFI"
        echo create partition msr size=16
    )
    if "!size!"=="" (
        echo create partition primary
    ) else (
        echo create partition primary size=!size!
    )
    echo format quick fs=ntfs
    echo assign
    if "!second_partition!"=="yes" (
        echo create partition primary
        echo format quick fs=ntfs
        echo assign
    )
)

)
echo exit
) > "%temp%\autopart"

echo 正在执行分区...
diskpart /s "%temp%\autopart"

if %errorlevel% neq 0 (
    echo 执行失败，错误代码：%errorlevel%
    del "%temp%\autopart"
    del "%temp%\listdisk"
    timeout /t 1 /nobreak >nul
    pause
)

del "%temp%\autopart"
del "%temp%\listdisk"
echo 分区完成！
timeout /t 1 /nobreak >nul
exit /b 0