批處理文件(*.bat)實用指南

1 概述

批處理文件(Batchfile)是 DOS 下的批處理文件,是一種無格式的文本文件,文件擴展名爲 .bat.cmd。在命令行輸入批處理文件名或雙擊批處理文件即可運行。批處理文件常用來簡化日常重複性任務,但批處理文件應該僅僅被用於小功能或者簡單的包裝腳本,複雜功能應該使用Python腳本。[1]

2 批處理命令

[Home]+R 打開cmd窗口,輸入help 顯示部分DOS命令

序號 命令 描述 示例
1 find 在文件中搜索字符串 find /?
2 findstr 在文件中尋找字符串 findstr /?
3 move 移動文件並重命名文件和目錄 move /?
4 copy 將一份或多份文件複製到另一個位置 copy /?
5 xcopy 複製文件和目錄樹 xcopy /?
6 del 刪除一個或數個文件 del /?
7 mkdir 創建目錄 mkdir /?
8 rmdir 刪除一個目錄 rmdir /? or rd /?
9 cls 清除屏幕
10 type 顯示文本文件的內容,與 Linux 系統 cat 命令功能類似
11 rename 重命名文件
12 pushd 改變當前目錄到指定目錄,並保存當前的目錄在堆棧頂端
13 popd 改變當前目錄,跳轉到堆棧頂端保存的目錄,並將堆棧頂端的目錄刪除
14 where 顯示符合搜索模式的文件位置。在默認情況下,搜索是在當前目錄和 PATH環境變量指定的路徑中執行的。 where /?

自定義變量:

set CURRDIR=%~dp0
SET REPO_ROOT=%cd%
SET BUILD_DIR=cmake_build
PATH %PATH%;..\externals\tcltk\bin
set PATH=%PATH%;..\externals\tcltk\bin
SET CMAKE_DIR=%REPO_ROOT%\tensorflow\contrib\cmake
SET MSBUILD_EXE="C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe"
SET PHP_BUILDCONF_PATH=%~dp0
SET PHP_SDK_SCRIPT_PATH=
set SDK_RUNNER=%PHP_BUILD_CACHE_SDK_DIR%\phpsdk-%PHP_BUILD_CRT%-%PLATFORM%.bat
set CMD_LINE_ARGS=%$
set DEFAULT_JVM_OPTS=
set TOOLS_PATH=packages\Grpc.Tools.1.14.1\tools\windows_x86
set GRPC_PYTHON_BUILD_WITH_CYTHON=1
set ARTIFACT_DIR=%cd%\%ARTIFACTS_OUT%
set GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS=2
SET SOURCE_DIR=F:\frameworks\tensorflow\
SET SOURCE_PYTHON_SCRIPT=%RETVAL%
SET SOURCE_VERSION_CC=%RETVAL%
DIR %REPO_ROOT%\%BUILD_DIR%\tf_python\dist\ /S /B > wheel_filename_file

set PYTHON_PROJECT_NAME=cntk
if /i %p_GpuBuild% equ true (
  set PYTHON_PROJECT_NAME=cntk-gpu
)

set PYTHON_WITH_DEPS=
if /i %p_CNTK_PYTHON_WITH_DEPS% equ true (
  set PYTHON_WITH_DEPS="--with-deps"
)

set SOURCEDIR=%~f1
set TARGETDIR=%~f2

注意事項:

  1. color 命令只能對整個控制檯設置前景和背景顏色,無法對部分顯示設置輸出顏色;
  2. echo.輸出空行;
  3. del .\*.rar Batchfile中路徑分隔使用\;
  4. 轉義字符 ^: 要輸出特殊字符,就需要用 echo ^>、echo ^|、echo ||、echo ^^……之類的格式來處理;如果要顯示%本身時,需要在前面用%來轉義: %%, 例如: echo.Please usemake <target>where ^<target^> is one of

學習資料:
[1] Windows批處理命令詳解
[2] Windows bat批處理基礎命令學習教程
[3] 一份比較詳細的DOS批處理命令說明
[4] bat腳本基本命令語法
[5] bat批處理命令大全
[6] Bat命令學習
[7] dos bat批處理命令幫助
[8] bat批處理腳本詳細教程

[未完待續]

3 應用舉例

示例-1: 清理系統垃圾.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  %systemdrive%\recycled\*.*
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

示例-2: 設置網絡靜態IP

@echo 更改“無線網絡連接”IP地址
@echo IP:192.168.6.112
@echo 網關:192.168.1.1
@echo DNS:10.10.198.31

@pause
@echo off
netsh interface ip set address name="無線網絡連接" source=static addr=192.168.6.112 mask=255.255.255.0 gateway=192.168.6.1
netsh interface ip set dns name="無線網絡連接" source=static addr=10.10.198.31
ping 192.168.6.112

示例-3: cmake構建VS2017工程

@echo off
set CURRENT_DIR=%~dp0
set PROJECT_NAME=project

echo %CURRENT_DIR%%PROJECT_NAME%
if not exist %CURRENT_DIR%%PROJECT_NAME% (
    mkdir %CURRENT_DIR%%PROJECT_NAME%
)
cd %CURRENT_DIR%%PROJECT_NAME%

@rem Generate VS2017 project.
cmake ../ -G "Visual Studio 15 2017 Win64"
pause

示例-4: 收集某一類文件到指定文件夾

@echo off

set ROOT_PATH=%~dp0
set SET_PATH=%ROOT_PATH%\nf_set


@rem Clear Workspace
if exist %SET_PATH% (
    rmdir /q /s %SET_PATH%
)
mkdir %SET_PATH%


@rem Collect NF set
for /F %%S in ('dir %ROOT_PATH%\*.set /s /b') do (
    move %%S %SET_PATH%
)
pause

示例-5: Batchfile版本聲明註釋

@rem Copyright 2017 gRPC authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem     http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.

@rem Move python installation from _32bit to _32bits where they are expected by python artifact builder
@rem TODO(jtattermusch): get rid of this hack
@echo off
rem ******************************************************
rem * Clean up output files from previous runs
rem ******************************************************
@echo off
rem =========================================================
rem We copy xgsubmit.exe to work around a known output issue,
rem and work with the copy to submit the actual response file
rem =========================================================
@echo off
echo.
echo # ======================================================================
echo #                       test_decimal: platform=x64
echo # ======================================================================
echo.

echo # ==================== refleak tests =======================
@rem TODO(jtattermusch): is there a better way to force using MSVC?
@rem select the MSVC compiler explicitly to avoid using gcc from mingw or cygwin
@rem (both are on path)
set "MSVC_COMPILER=C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe"

示例-6: PUSHD & POPD

@echo off
rem This batch file deletes all .txt files in a specified directory
pushd %1
del *.txt
popd
echo All text files deleted in the %1 directory

示例-7: Batchfile錯誤處理

mkdir -p %ARTIFACTS_OUT%
copy /Y cmake\build\%ARCHITECTURE%\grpc_csharp_ext.dll %ARTIFACTS_OUT% || goto :error
copy /Y cmake\build\%ARCHITECTURE%\grpc_csharp_ext.pdb %ARTIFACTS_OUT% || goto :error

goto :EOF

:error
echo Failed!
exit /b %errorlevel%

4 參考資料

[1] Google Shell 風格指南
[2] grpc/grpc
[3] tensorflow/tensorflow
[4] pytorch/pytorch

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章