用gdisk实现dos下自动格式化c:以外的所有分区(包括ntfs)
如果启动的是还原分区上的dos系统,那还原系统所在分区一定是c:
有的时候用ghost或者TI 等还原系统前,需要自动格式化还原系统所在分区以外的全部分区,以避免其他分区的病毒在进入系统的时候再次感染。而dos下用软件手动对ntfs分区的格式化有很些麻烦,可如果不格式化,一旦这些ntfs分区上有病毒呢?
所以考虑编辑此脚本,利用awk分析分区结构,用gdisk格式化除c盘以外的全部分区,NTFS分区仍然被格式化成NTFS格式。
gdf2 1 表示对第一硬盘操作
gdf2 2 表示对第二硬盘操作
gdf2.bat 代码如下:注意红色部分是GDISK命令,我已经用 ECHO 注释掉,如果真实情况下需要运行本脚本,必须把前面的ECHO去掉。
@echo off
cls
set d=%1
if *%1*==** set d=1
echo Format all partitions in HD%D% ,but C: !
echo Please wait....
gdisk %d%|awk 'substr($0,20,3)=="LOG"&&substr($0,2,2)!="C:";substr($0,20,3)=="PRI"&&substr($0,2,2)!="C:"'>gdf1.txt
if exist gdf1.txt type gdf1.txt
awk 'substr($0,53,10)!~"NTFS" {print "/del /p:"substr($0,6,1)" /y";print "/cre /"substr($0,20,3)" /for /Q /Y" }' gdf1.txt>gdf2.txt
awk 'substr($0,53,10)~"NTFS" {print "/del /p:"substr($0,6,1)" /y";print "/cre /"substr($0,20,3)" /For /NTFS /Q /Y" }' gdf1.txt>>gdf2.txt
awk '{if (substr($0,15,1)=="A") print "/act /p:"substr($0,6,1)" /y"}' gdf1.txt>>gdf2.txt
if exist gdf1.txt del gdf1.txt
echo.
if not exist gdf2.txt goto end
type gdf2.txt
pause
echo gdisk %d% /batch:gdf2.txt
del gdf2.txt
:end
set d=
--------------------------
脚本解读:
1、用awk分析gdisk得到的硬盘分区列表,将c盘以外的逻辑分区和主引导分区信息传递给文件gdf1.txt,并显示gdf1.txt
2、用awk分析gdf1.txt,找出分区格式不是ntfs的分区,把删除这些分区和重建并格式化这些分区的gdisk脚本命令传递给文件gdf2.txt
3、用awk分析gdf1.txt,找出分区格式是ntfs的分区,把删除这些分区和重建并格式化这些分区的gdisk脚本命令追加给文件gdf2.txt
4、用awk分析gdf1.txt,找出激活状态的分区,把激活这些分区的gdisk脚本命令追加给文件gdf2.txt
5、用gdisk调用 gdf2.txt 中的指令。
全程用到2个文本文件gdf1.txt,gdf2.txt和一个环境变量%d%
gdf1.txt---------存放硬盘上除了c盘以外的分区列表
gdf2.txt---------存放gdisk调用的脚本指令
%d%-------------用于存放待操作硬盘的编号
http://bbs.wuyou.com/viewthread.php?tid=158594&extra=page%3D1
http://bbs.wuyou.com/viewthread.php?tid=158553&extra=page%3D1
Last edited by DXSX on 2010-1-7 at 10:44 ]
Using gdisk to Automatically Format All Partitions Except C: Under DOS (Including NTFS)
If the booted DOS system is on a restore partition, then the partition where the restore system is located must be C:. Sometimes, before restoring the system using Ghost or TI, etc., it is necessary to automatically format all partitions except the partition where the restore system is located to avoid the virus in other partitions from infecting again when entering the system. And formatting NTFS partitions manually under DOS is quite troublesome. But if not formatted, once there is a virus on these NTFS partitions?
So consider editing this script, using awk to analyze the partition structure, and using gdisk to format all partitions except the C drive. NTFS partitions are still formatted to the NTFS format.
gdf2 1 means operating on the first hard disk, and gdf2 2 means operating on the second hard disk.
The code of gdf2.bat is as follows: Note that the red part is the GDISK command, which I have commented out with ECHO. If you need to run this script in a real situation, you must remove the ECHO in front.
@echo off
cls
set d=%1
if *%1*==** set d=1
echo Format all partitions in HD%D% ,but C: !
echo Please wait....
gdisk %d%|awk 'substr($0,20,3)=="LOG"&&substr($0,2,2)!="C:";substr($0,20,3)=="PRI"&&substr($0,2,2)!="C:"'>gdf1.txt
if exist gdf1.txt type gdf1.txt
awk 'substr($0,53,10)!~"NTFS" {print "/del /p:"substr($0,6,1)" /y";print "/cre /"substr($0,20,3)" /for /Q /Y" }' gdf1.txt>gdf2.txt
awk 'substr($0,53,10)~"NTFS" {print "/del /p:"substr($0,6,1)" /y";print "/cre /"substr($0,20,3)" /For /NTFS /Q /Y" }' gdf1.txt>>gdf2.txt
awk '{if (substr($0,15,1)=="A") print "/act /p:"substr($0,6,1)" /y"}' gdf1.txt>>gdf2.txt
if exist gdf1.txt del gdf1.txt
echo.
if not exist gdf2.txt goto end
type gdf2.txt
pause
echo gdisk %d% /batch:gdf2.txt
del gdf2.txt
:end
set d=
--------------------------
Script Interpretation:
1. Use awk to analyze the partition list obtained from gdisk, pass the information of logical partitions and primary boot partitions except the C drive to the file gdf1.txt, and display gdf1.txt.
2. Use awk to analyze gdf1.txt, find partitions whose format is not NTFS, and pass the gdisk script commands for deleting these partitions and rebuilding and formatting these partitions to the file gdf2.txt.
3. Use awk to analyze gdf1.txt, find partitions whose format is NTFS, and append the gdisk script commands for deleting these partitions and rebuilding and formatting these partitions to the file gdf2.txt.
4. Use awk to analyze gdf1.txt, find activated partitions, and append the gdisk script commands for activating these partitions to the file gdf2.txt.
5. Use gdisk to call the instructions in gdf2.txt.
The entire process uses 2 text files gdf1.txt, gdf2.txt and an environment variable %d%.
gdf1.txt---------Stores the partition list except the C drive on the hard disk
gdf2.txt---------Stores the script commands called by gdisk
%d%-------------Used to store the number of the hard disk to be operated
http://bbs.wuyou.com/viewthread.php?tid=158594&extra=page%3D1
http://bbs.wuyou.com/viewthread.php?tid=158553&extra=page%3D1
Last edited by DXSX on 2010-1-7 at 10:44 ]