批处理修改boot.ini大全

48 篇文章 0 订阅

 

 文章来自百度文库

wenku.baidu.com/view/c8471b1152d380eb62946d35.html

              
         boot.ini文件是Win2000、WinXP和Win2003系统存放启动菜单的文件,因此学会编辑boot.ini文件对于安装多系统的朋友来说尤其重要。
 
      以下都是本人在各大论坛求教并研究整理出来的批处理代码,相信还有不少朋友需要下面的代码。 
一、功能要求:       ㈠、添加启动项
修改timeout=4,并在最后加上一句c:\grldr="启动 GRUB4DOS" (添加时不要删除其他已有的启动菜单)
㈡、删除启动项
 在删除时不要删除其他已有的启动菜单。
    
二、代码如下: 
      ㈠、添加启动项           ⒈直接添加法: (此法会造成重复添加)
  @echo off  rem 解除boot.ini的各种属性
attrib -a -s -h -r %systemdrive%\boot.ini >nul  rem 在boot.ini中加入新启动项,默认是加在最后面
echo c:\grldr="启动 GRUB4DOS" >>%systemdrive%\boot.ini  rem 修改启动菜单默认等待时间为4秒
bootcfg /timeout 4  rem 恢复boot.ini的各种属性
attrib +a +s +h +r %systemdrive%\boot.ini >nul
 
           ⒉替换添加法
          ① 第一种替换 添加 法: (此法会删除其他非Windows系统启动菜单)
 @echo off
attrib -a -h -r -s c:\boot.ini >nul
echo [boot loader] >c:\boot.ini
echo timeout=4 >>c:\boot.ini
echo default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS >>c:\boot.ini
echo [operating systems] >>c:\boot.ini
echo multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect >>c:\boot.ini
echo c:\grldr="启动 GRUB4DOS" >>c:\boot.ini
attrib +a +h +r +s c:\boot.ini >nul
 
        ②第二种替换 添加 法:(此法较好,不会重复添加。)
  @echo off
rem 解除boot.ini的各种属性
attrib -a -h -r -s c:\boot.ini >nul
rem 把boot.ini另存为boot.bak
type c:\boot.ini >c:\boot.bak
rem 把boot.ini的内容替换为boot.bak的第一行内容
type c:\boot.bak|find "boot loader" /i>c:\boot.ini
rem 增加启动菜单默认等待时间为4秒
echo timeout=4 >>c:\boot.ini
rem 显示boot.bak的内容但不显示包含有boot loader 和 timeout 和的行,并把显示的内容添加到boot.ini内容的后面。
type c:\boot.bak|find "boot loader" /i /v|find "timeout" /i /v>>c:\boot.ini
rem 把新启动项添加到boot.ini中
echo c:\grldr="启动 GRUB4DOS" >>c:\boot.ini
rem 恢复boot.ini的各种属性
attrib +a +s +r +h c:\boot.ini >nul
 
          ③第三种替换 添加 法: (此法会造成重复添加)
@echo off
set n=0
setlocal enabledelayedexpansion
for /f "delims=" %%i in (c:\boot.ini) do (
set /a n+=1
if not !n! equ 2 (
echo %%i
) else (
echo timeout=4
)
)>>c:\boot.tmp
echo c:\grldr="启动 GRUB4DOS">>c:\boot.tmp
attrib -s -h -r "c:\boot.ini"
copy "c:\boot.tmp" "c:\boot.ini">nul
attrib +s +h +r "c:\boot.ini"
del c:\boot.tmp >nul
pause>nul
 
          ㈡、删除启动项
 
           ⒈ 用bootcfg删除法
 
        ①直接删除法 : (假设已知“启动 GRUB4DOS”是第二个启动项)
  @echo off  rem 解除boot.ini的各种属性  attrib -a -s -h -r %systemdrive%\boot.ini >nul  rem 删除第二个启动项
bootcfg /delete /id 2  rem 恢复boot.ini的各种属性
attrib +a +s +h +r %systemdrive%\boot.ini >nul
 
        ②定位删除法:
首先查找" 启动 GRUB4DOS "所在行的行号,赋值给%target% ,然后查找每一个"启动项目 ID:"的行号,赋值给%id% ,小于target的并且是最大的%id%即为" 启动 GRUB4DOS "所在的id ,然后用bootcfg /delete /id %id%删除即可。
 @echo off
for /f "delims=[,]" %%a in ('bootcfg /query^|find /i /n " 启动 GRUB4DOS "') do set target=%%a
for /f "delims=[,],: tokens=1,3" %%a in ('bootcfg /query^|find /i /n "启动项目 ID:"') do if %%a lss %target% set /a id=%%b
bootcfg /delete /id %id%
 
         ⒉替换删除法
 ①第一种 替换删除法:  @echo off  rem 解除boot.ini的各种属性
attrib -a -s -h -r %systemdrive%\boot.ini >nul  rem 把boot.ini另存为boot.bak
type %systemdrive%\boot.ini >%systemdrive%\boot.bak  rem 显示boot.bak的内容但不显示包含有“启动 GRUB4DOS”的那一行,并把显示的内容替换boot.ini的内容。
type %systemdrive%\boot.bak|find "启动 GRUB4DOS" /i /v >%systemdrive%\boot.ini  rem 恢复boot.ini的各种属性
attrib +a +s +h +r %systemdrive%\boot.ini >nul  rem 删除临时使用的boot.bak文件
del %systemdrive%\boot.bak
 
 ②第二种 替换删除法: (此法代码最简单)
 @echo off
type %systemdrive%\boot.ini>%systemdrive%\boot.bak
attrib -h -r -s %systemdrive%\boot.ini
type %systemdrive%\boot.bak|find "启动 GRUB4DOS" /i /v>%systemdrive%\boot.ini
attrib +s +r +h %systemdrive%\boot.ini
 
 ③第三种 替换删除法: (此法代码较繁琐)
 @echo off
attrib -s -r -h %SystemDrive%\boot.ini
if exist %SystemDrive%\boot2.iii    del %SystemDrive%\boot2.iii
for /f "skip=2 tokens=* delims=$$$" %%i in ('find /v /i "启动 GRUB4DOS" c:\boot.ini') do echo %%i >>c:\boot2.iii
del %SystemDrive%\boot.ini
ren %SystemDrive%\boot2.iii boot.ini
attrib +s +r +h %SystemDrive%\boot.ini
exit
 
 
      需注意的地方:    当使用bootcfg添加启动项时  1、如果使用copy,那么copy 和 >> 必须在bootcfg之前使用,否则copy 和 >> 会失效。
2、>> 与 >nul 不能共用,否则 >> 会失效。
3、copy前必须解除boot.ini的属性,也就是执行attrib -a -s -h -r %systemdrive%\boot.ini,否则copy也会失效。  4、当使用 bootcfg命令 直接删除启动项时,必须先确定所要删除的启动项是第几个启动项,否则可能会造成误删。
 
 
 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
 
 翻阅了 Bootcfg 的帮助,确实没有添加自定义开关的选项。关于 Climbing 斑竹推荐的第三方软件 change.exe,老毛桃用过,确实好用。但我只是想尝试一下,用批处理不借助第三方软件到底能不能实现。
现在想使用批处理找到里面的
 
 multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /execute /fastdetect
 
这一行,并且能够在这行的末尾添加字符串,比如修改成
 
 multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /execute /fastdetect  /Kernel=Kernel.exe
 
完善的代码如下:
 
 :: ModBoot.cmd - V3 - Modify of boot.ini
:: Will Sort - 2006-6-11 -
CMD@WinXP
@echo off
if not exist boot.ini echo Not found boot.ini!&goto :eof
if exist %temp%\boot.new del /f /a %temp%\boot.new
find/i "/kernel=" boot.ini>nul && echo Modified boot.ini! && goto :eof
for /f "delims=" %%l in (boot.ini) do (
echo.%%l | find/i "/fastdetect" >nul && echo %%l /kernel=kernel.exe || echo %%l
)>>%temp%\boot.new
find/i "/kernel" %temp%\boot.new>nul 2>nul
if errorlevel 1 echo Fail to parse boot.ini!&goto :eof
attrib -s -h -r boot.ini
copy boot.ini %temp%\boot.bak>nul&&echo Pass to backup boot.ini.
copy %temp%\boot.new boot.ini>nul 2>nul
find/i "/kernel" boot.ini>nul 2>nul
if not errorlevel 1 echo Pass to wrtie boot.ini.
if errorlevel 1 copy %temp%\boot.bak boot.ini>nul & echo Fail to wrtie boot.ini!
attrib +s +h +r boot.ini
del %temp%\boot.new & del %temp%\boot.bak
 
代码解释:
for /f "delims=" %%l in (boot.ini) do (
echo.%%l | find/i "/fastdetect">nul && echo %%l /kernel=kernel.exe||echo %%l
)>>boot.new
 
这段代码的主要作用就是 检索BOOT.INI文件的每行,如果发现有/fastdetect的行就在这行的后面加上/kernel=kernel.exe这个参数,然后把文件保 存为boot.new,如果没有发现/fastdetect这行,就把整个的BOOT.INI文件内容保存为boot.new文件,如下:
 
 
如果BOOT.INI原文件是
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(2)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect /kernel=kernel.exe
multi(0)disk(0)rdisk(0)partition(1)\WINNT="Microsoft Windows 2000 Professional" /fastdetect /kernel=kernel.exe
这样我想楼主应该看出不同之处了吧。如果没有/fastdetect那么boot.new 文件的内容应该跟原BOOT.INI内容一样。
 
 
解释:echo.%%l | find/i "/fastdetect">nul && echo %%l /kernel=kernel.exe||echo %%l这句
 
|管道字符,表示第一条命令的输出作为第二条命令的输入,也就是显示echo %%i的内容作为find /i 查找的内容。
 
>nul表示执行这条命令不显示正常输出
 
&&表示如果前面的命令成功执行那么就执行后面的语句,如果执行不成功那就不执行后面的语句
 
||这个表示如果前面的执行的语句失败就执行这句,例如如果文件中没有/fastdetect字符存在的行,那么就执行 echo %%i ,如果前面的命令都执行成功就不执行这句了,唉,打字真累,大概的就这些了 ...........
 
 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
 
 修改BOOT.INI里面的时间
 
@echo off
title 修改启动菜单时间
color fc
mode con: cols=32 lines=6
c:
cd \
setlocal enabledelayedexpansion
 
set boot=c:\boot.ini
for /f "tokens=2 delims==" %%i in ('findstr /C:"timeout" %boot%') do echo 原始启动时间为: %%i
 
echo.
echo 请输入您想要的启动时间,并回车
set/p times= 修改为:
type boot.ini>boot.bak
attrib -h -r -s boot.ini
type boot.bak|find "boot loader" /i>boot.ini
echo timeout=%times% >>boot.ini
type boot.bak|find "boot loader" /i /v|find "timeout" /i /v>>boot.ini
attrib +s +r +h boot.ini
@del boot.bak >>nul 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值