批處理修改boot.ini大全

來源:互聯網
上載者:User

 

 文章來自百度文庫

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 
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.