Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file for running DiskPart

I am trying to develop a batch file to run and remove the Hidden partitions in Windows 7, when trying to remove all partitions. Normally I do this line by line in the Command prompt windows, but was trying to figure out how to create a batch file to run and speed this process up..

Here are lines I type in the command prompt.

disk part
Rescan
List Disk
Select Disk 3
List Partition
Select Partition 3
Delete Partition Override

I have created a BAT file but can only get the first command to work.

like image 906
Jack Hayes Avatar asked Jun 07 '13 13:06

Jack Hayes


1 Answers

It can be really very easy, but when I look at answers of others I think "Why do it the easy way, when you can do it the hard way" :D

Okay, diskpart is terminal application which means it has it's own CLI (command line interface). That means, that if you want to write a command to the diskpart, you have to write it to the diskpart's own CLI as a stdin (standard input). This is the reason, why you cannot wirte your commands via batch file, because your diskpart commands are run as next commands for cmd.exe after diskpart exits.

Now we only must "lie" to diskpart and emulate stdin to his CLI.

We can achieve it like this:

(echo Rescan
echo List Disk
echo Select Disk 3
echo List Partition
echo Select Partition 3
echo Delete Partition Override
)  | diskpart
pause

So, this code above does the following:

  1. The echo commands will generate stdout (standard output). Usually our command-line interpreter, cmd.exe would just print out this stdout on the screen. (You can try that by running only the echo commands with parenthesis in cmd.exe)

  2. Then, using the pipe | we redirect stdout of those echo commands into the diskpart application. So the stdout from echo's will now act as stdin for the diskpart application.

So, you run diskpart, diskpart gets input from echo's, similiarly as it would get input from keyboard, and of course it works!

That is all, simple and easy solution!

like image 94
Msprg Avatar answered Nov 15 '22 16:11

Msprg