コンテンツへスキップ

バッチ処理でディスクパーティションを作成し、ドライブを割り当てる。

最近この手のコマンドについてはBING さん(現在はコパイロット)に聞くことが多くなりました。
検証は必要ですが、かなり精度が高くてありがたいです。

処理の程度によりますが、一発でそのまま使えるものは難しいので、メモしておきます。
加えて、Bing さん(現在はコパイロット)のすばらしさも共有したい感じです。
(ちなみにBing さんのアイコンは、自画像としてBing さん自身に生成してもらったものです。)

バッチファイルでCドライブに250GBのパーティションを作成し、Dドライブに割り当てることはできます。以下は、そのためのバッチファイルの例です。この例では、Diskpartコマンドを使用してパーティションの作成とドライブの割り当てを行います。

「パーティションを変更するので、バックアップとか万が一の対策をしましょう」とのこと。

@echo off
echo Creating a 250GB partition on C drive and assigning it to D drive

diskpart /s create_partition.txt

echo Done!
pause

create_partition.txt

select disk 0
create partition primary size=256000
assign letter=D
exit

上記を基本形にして、ちょっと贅沢を言い、バッチ内でスクリプトのテキストを生成して、バッチの最後でそのテキストを消す仕様に変更、さらにディスク番号と、ドライブレターをユーザー入力で変更できるように改造してもらった。

unimoniのアバター

・create_partition.txt をバッチ処理で作成して、バッチの最後に消す流れにしたい。
・select diskの番号をバッチ起動時にユーザーが入力して変数として変更できるようにしたい。

わかりました。バッチファイル内で create_partition.txt を作成し、必要な操作を実行した後に削除する方法を紹介します。
なるほど、バッチファイルを実行する際にユーザー入力を受け取り、その入力を変数として使用する方法を追加しましょう。

@echo off
set /p diskNumber=Please enter the disk number to select (e.g., 0): 
set /p driveLetter=Please enter the drive letter to assign (e.g., D): 
echo Creating a temporary text file for Diskpart commands

REM Create create_partition.txt
echo select disk %diskNumber% > create_partition.txt
echo create partition primary size=256000 >> create_partition.txt
echo assign letter=%driveLetter% >> create_partition.txt
echo exit >> create_partition.txt

REM Run Diskpart with the created file
diskpart /s create_partition.txt

REM Delete the temporary text file
del create_partition.txt

echo Partition created and temporary file deleted!
pause

検証すると、空いたボリュームありきのバッチだったので、ボリューム縮小の処理を追加。
さらにサイズ入力の単位をGB にしてもらい、クイックフォーマットも追加した。

volumeLetter= と、driveLetter= は概念が違うので、一緒の変数にはできないとのこと。

あと、管理者権限での実行が必要です。

@echo off
set /p volumeLetter=Please enter the volume letter to shrink (e.g., C): 
set /p shrinkSizeGB=Please enter the amount in GB to shrink the volume by (e.g., 10): 
set /p diskNumber=Please enter the disk number to select (e.g., 0): 
set /p driveLetter=Please enter the drive letter to assign (e.g., D): 
echo Creating a temporary text file for Diskpart commands

REM Convert GB to MB
set /a shrinkSizeMB=%shrinkSizeGB%*1024

REM Create shrink_volume.txt
echo select volume %volumeLetter% > shrink_volume.txt
echo shrink desired=%shrinkSizeMB% >> shrink_volume.txt
echo exit >> shrink_volume.txt

REM Run Diskpart with the created file
diskpart /s shrink_volume.txt

REM Delete the temporary text file
del shrink_volume.txt

echo Volume shrunk. Creating new partition...

REM Create create_partition.txt
echo select disk %diskNumber% > create_partition.txt
echo create partition primary size=%shrinkSizeMB% >> create_partition.txt
echo assign letter=%driveLetter% >> create_partition.txt
echo exit >> create_partition.txt

REM Run Diskpart with the created file
diskpart /s create_partition.txt

REM Delete the temporary text file
del create_partition.txt

echo Partition created. Formatting the new partition...

REM Quick format the new partition
format %driveLetter%: /fs:ntfs /q /v:NewPartition /y

echo Partition created, formatted and temporary files deleted!
pause

実行前にディスク管理を開いて、ディスクの番号と、ドライブレターを確認しておけば、入力には困らない。
新しいPC 、またはストレージではなければ、空き容量とかも確認した方がいいかも。バックアップも。

(Visited 5 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です