1. echo

  1. Without Parameters

    • Displays the current state of echo: whether it is on or off.
  2. With Parameters

    • Parameter [on/off]: on turns on command echoing; off turns off command echoing. “Echoing” can be understood as repeating the display.
    • Parameter message: Prints the content of message.
    • @ before echo: Adding @ means the command itself will not be displayed; otherwise, the command itself will be shown.
    • Parameter message > file: Writes the content of message to file, where > means clear and write.
    • Parameter message >> file: Appends the content of message to file, where >> means append and write.
  3. Splitting a Path and Getting the Last Folder Name in a Batch Script

    1@echo off
    2
    3set MYDIR=C:\FOLDER1\FOLDER2\FOLDER3\
    4if "%MYDIR:~-1%" == "\" set "MYDIR1=%MYDIR:~0,-1%"
    5
    6for %%f in ("%MYDIR1%") do set "myfolder=%%~nxf"
    7echo myfolder=%myfolder%
    8
    9@REM The output will be myfolder=FOLDER3
    
  4. Batch Script Variable Assignment Outputs “ECHO is off”

    • If the variable value is empty, this prompt will appear.
    • Solution: Ensure there are no spaces around the variable. If there are spaces, the spaces will be treated as part of the variable name, resulting in an empty value.

2. enabledelayedexpansion

I haven’t systematically learned batch scripting, and I fell into a big pit. The scenario is as follows: I need to get the %errorlevel% within a nested if statement.

 1@echo off
 2set CURRENT_VM_NAME=Cold
 3
 4VBoxManage list vms | find "%CURRENT_VM_NAME%" >nul 2>nul
 5if %errorlevel% == 0 (
 6    echo virtual machine %CURRENT_VM_NAME% already exists
 7    VBoxManage showvminfo %CURRENT_VM_NAME% | findstr /i "State:.*running" > nul 2>nul
 8    if %errorlevel% == 0 (
 9        echo virtual machine %CURRENT_VM_NAME% is running
10       	VBoxManage controlvm %CURRENT_VM_NAME% poweroff >nul 2>nul
11        if %errorlevel% neq 0 (
12            echo virtual machine %CURRENT_VM_NAME% close failed...
13        ) else (
14            echo virtual machine %CURRENT_VM_NAME% is closed
15        )
16    ) else (
17        echo virtual machine %CURRENT_VM_NAME% is closed
18    )
19)

In reality, the %errorlevel% values here are all from the first if statement. It might run without issues by luck, but there is actually a risk.

The mechanism of running commands in batch scripts: Batch scripts read commands line by line (for example, the for command, and all statements enclosed in a pair of parentheses are treated as one line).

There are two methods:

  1. Use the goto statement to jump out of the if/for statement, which is a workaround.

  2. Enable delayed variable expansion: setlocal enabledelayedexpansion, and enclose the variable with a pair of exclamation marks “!!”.

     1@echo off
     2setlocal enabledelayedexpansion
     3set CURRENT_VM_NAME=Cold
     4
     5VBoxManage list vms | find "%CURRENT_VM_NAME%" >nul 2>nul
     6if %errorlevel% == 0 (
     7    echo virtual machine %CURRENT_VM_NAME% already exists
     8    VBoxManage showvminfo %CURRENT_VM_NAME% | findstr /i "State:.*running" > nul 2>nul
     9    if !errorlevel! == 0 (
    10        echo virtual machine %CURRENT_VM_NAME% is running
    11        VBoxManage controlvm %CURRENT_VM_NAME% poweroff >nul 2>nul
    12        if !errorlevel! neq 0 (
    13            echo virtual machine %CURRENT_VM_NAME% close failed...
    14        ) else (
    15            echo virtual machine %CURRENT_VM_NAME% is closed
    16        )
    17    ) else (
    18        echo virtual machine %CURRENT_VM_NAME% is closed
    19    )
    20)