用于运行DiskPart的批处理文件

时间:2013-06-07 13:37:24

标签: command-line command

我正在尝试开发批处理文件,以便在尝试删除所有分区时运行并删除Windows 7中的隐藏分区。通常我会在命令提示符窗口中逐行执行此操作,但是试图找出如何创建批处理文件以运行并加快此过程..

以下是我在命令提示符中输入的行。

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

我创建了一个BAT文件,但只能获得第一个命令。

5 个答案:

答案 0 :(得分:7)

将您的diskpart命令(在键入diskpart后键入的命令)放在script.txt等文本文件中,并使用以下命令调用diskpart。

@echo off
diskpart /s script.txt

但要非常小心,你的命令是正确的并经过充分测试,不要调用批处理文件diskpart。

有关详细信息,请参阅此处:http://support.microsoft.com/kb/300415

答案 1 :(得分:4)

这真的很容易,当我看到其他答案时,我认为"为什么在它变得困难时会很容易" :d

好的,diskpart有自己的CLI(命令行界面)这就是原因,为什么你不能通过批处理文件来打开你的命令。现在我们只能骗到diskpart并模拟他的CLI输入。我们可以这样做:

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

最后,管道重定向回显输出,因此它不是命令行CLI,而是对磁盘部分CLI。

就是这样!

答案 2 :(得分:2)

嘿,我知道我参加派对已经很晚了,但是我一直在忙着使用Diskpart和脚本等来处理我想做的一些后期成像,虽然这不是一个直接的解决方案。你的确切问题,无论如何都要仔细阅读,我敢打赌你可以把它变成你需要做的事情:(是的,它非常糟糕。我不在乎。它作为一个文件存在,不需要外部文件,并且自行清理。)

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

答案 3 :(得分:0)

从@Msprg的答案中汲取灵感,通过使用here-string,可以在PowerShell中更轻松地做到这一点

((@"
Rescan
List Disk
Select Disk 3
List Partition
Select Partition 3
Delete Partition Override
"@
)|diskpart)

如果需要将其放入批处理文件中,则始终可以使用其他StackOverflow poststhis thread中可以找到的技巧之一直接将其注入。

例如

<# :
@PowerShell -NoLogo -NoProfile -Command Invoke-Expression $('$args=@(^&{$args} ($input^|?{$_}));'+(${%~f0}^|Out-String)) & @GOTO :EOF
#>
((@"
Rescan
List Disk
Select Disk 3
List Partition
Select Partition 3
Delete Partition Override
"@
)|diskpart)

答案 4 :(得分:-1)

我在搜索如何向diskpart脚本文件添加注释时点击此处。后来发现可以使用REM命令添加注释。

REM This script is to build to do so and so.
sel disk 0
sel part 1
...