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.
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:
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)
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With