Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Win7 建立批次檔:記事本寫指令存為 .bat,兩種新手友善教學

編輯:Win7 系統應用技巧

批次檔是 Windows 自動化腳本,將多行 CMD 指令存成 .bat 檔,雙擊即可連續執行。本文教學兩種建立方法:另存為法(推薦)、重新命名法,並附贈「系統垃圾清理」實用腳本範例。

方法一:記事本「另存為」法(最直觀、不易出錯)

  1. 任意位置右鍵 → 「新增」 → 「文字文件」。
  2. 新增文字文件
  3. 雙擊開啟,輸入批次指令(範例:系統垃圾清理)。
  4. 輸入批次指令
  5. 點選「檔案」 → 「另存為」。
  6. 「儲存類型」選 「所有檔案」,「檔案名稱」輸入 cleanup.bat(副檔名必須是 .bat) → 「儲存」。
  7. 另存為 bat
  8. 使用時右鍵 .bat → 「以系統管理員身分執行」效果最佳。
  9. 以管理員身分執行

方法二:重新命名法(需先顯示副檔名)

  1. 資料夾選項 → 「檢視」 → 取消勾選「隱藏已知檔案類型的副檔名」。
  2. 資料夾選項顯示副檔名
    取消隱藏副檔名
  3. 建立文字文件,輸入指令,關閉視窗選「是」儲存。
  4. 儲存文字文件
  5. 右鍵 新增文字文件.txt → 「重新命名」 → 改為 cleanup.bat → 按 Enter → 確認警告選「是」。
  6. 重新命名為 bat

贈送:系統垃圾清理批次腳本(複製即用)

@echo off
echo 正在清除系統垃圾檔案,請稍等......
del /f /s /q %systemdrive%\*.tmp
del /f /s /q %systemdrive%\*._mp
del /f /s /q %systemdrive%\*.log
del /f /s /q %systemdrive%\*.gid
del /f /s /q %systemdrive%\*.chk
del /f /s /q %systemdrive%\*.old
del /f /s /q %windir%\*.bak
del /f /s /q %windir%\prefetch\*.*
rd /s /q %windir%\temp & md %windir%\temp
del /f /q %userprofile%\cookies\*.*
del /f /q %userprofile%\recent\*.*
del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
echo 清除系統垃圾完成!
echo. & pause

常用批次指令速查:

  • @echo off:關閉指令回顯
  • pause:暫停,按任意鍵繼續
  • cls:清除畫面
  • rem 註解:註解行
  • if exist 檔案 (指令):條件判斷
Copyright © Windows教程網 All Rights Reserved