Batch file for running DiskPart

2019-07-23 14:50发布

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.

4条回答
一夜七次
2楼-- · 2019-07-23 15:04

Place your diskpart commands (the ones you type after typing diskpart) in a text file like script.txt and call diskpart with the following command.

@echo off
diskpart /s script.txt

But be very careful that your commands are correct and well tested and don't call the batch file diskpart.

See here for more information: http://support.microsoft.com/kb/300415

查看更多
Lonely孤独者°
3楼-- · 2019-07-23 15:08

So hey, I know I'm super late to the party, but I've been messing around with Diskpart and scripting, etc for some post-imaging stuff I want to do, and, though it's not a direct solution to your EXACT question, read through it anyways, and I bet you can kludge this into what you need to do: (Yes, it's very kludge-y. I don't care. It exists as ONE file, no external files necessary, and cleans up after itself.)

REM SCRIPT TO EXTEND A VOLUME - PICKS THE CORRECT ONE REGARDLESS OF VOLUME CONFIGURATION
REM THE VOLUME AFTER IMAGING WILL ALWAYS BE 39GB IN SIZE SO THAT'S WHAT IT HINGES ON
REM
REM THIS SCRIPT BECAME NECESSARY WHEN RUNNING DISKPART WITH A FIXED SCRIPT DIDN'T WORK 
REM FOR EVERY MACHINE DUE TO DIFFERENT DISK CONFIGURATIONS
REM
REM IT'S NOT PRETTY, BUT IT GETS THE JOB DONE AND CLEANS UP AFTER ITSELF
REM
REM <CHRISTOPHER T####N> 4/27/2016
REM
@ECHO OFF
@CLS
REM
REM GET LIST OF VOLUMES AND EXPORT TO TEXT FILE TO BE RUN AS SCRIPT BY DISKPART
ECHO LIST VOLUME>>C:\VOLLIST.TXT
REM
REM RUN TEXT FILE AS SCRIPT WITH DISKPART THEN EXPORT RESULTS TO NEW TEXT FILE
DISKPART /S C:\VOLLIST.TXT>>C:\VOLTEMP.TXT
REM
REM REMOVE THAT FIRST TEXT FILE WE CREATED
DEL C:\VOLLIST.TXT
REM
REM TAKE THE LIST OF VOLUMES AND GET THE ONE YOU NEED WITH THE FIND COMMAND (MODIFY AS NEEDED)
REM THEN USE THE TOKENS/DELIMS TO GRAB JUST THE VOLUME NUMBER YOU WANT
REM AND CREATE ANOTHER TEXT FILE CONTAINING THE FINAL SCRIPT TO EXTEND THE VOLUME YOU WANT
FOR /F "TOKENS=2 DELIMS= " %%A IN ('TYPE C:\VOLTEMP.TXT ^| FIND /I "39 GB"') DO ECHO SELECT VOLUME %%A>>C:\VOLEXT.TXT
REM
REM REMOVE THE SECOND TEXT FILE
DEL C:\VOLTEMP.TXT
REM
REM APPEND ADDITIONAL DISKPART COMMANDS TO THE FINAL TEXT FILE
ECHO EXTEND>>C:\VOLEXT.TXT
REM
REM APPEND ADDITIONAL DISKPART COMMANDS TO THE FINAL TEXT FILE
ECHO EXIT>>C:\VOLEXT.TXT
REM
REM RUN DISKPART USING THE FINAL TEXT FILE AS THE SCRIPT
DISKPART /S C:\VOLEXT.TXT
REM
REM REMOVE THE FINAL TEXT FILE SO NOTHING IS LEFT BEHIND AFTER SCRIPT EXECUTION
DEL C:\VOLEXT.TXT
REM
REM PAUSE SO YOU CAN SEE IF ANYTHING FAILED, AND HOPEFULLY, WHERE
@PAUSE
查看更多
别忘想泡老子
4楼-- · 2019-07-23 15:10

It is really very easy, and when i look at other answers i think "Why do it easy when it gets hard" :D

Okay, diskpart have his own CLI (command line interface) this is the reason, why you cannot wirte your commands via batch file. Now we only must lie to diskpart and emulate input to his CLI. We can do it this way:

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

And at the end the pipe redirect echo output, so it did not wirte to command line CLI but to diskpart CLI.

That is all!

查看更多
乱世女痞
5楼-- · 2019-07-23 15:16

I hit here while searching how to add comments to the diskpart script file. Later found that comments can be added using REM command.

REM This script is to build to do so and so.
sel disk 0
sel part 1
...
查看更多
登录 后发表回答