Soru

Zorluk: ZorBasic Scripting Languages and Constructs

A system administrator is reviewing an automated legacy maintenance batch script (`.bat`) designed to run natively in the Windows Command Prompt environment. The script must check if a custom environment variable named `ARCHIVE_PATH` has been defined before attempting to move log files to that destination. Which of the following code constructs uses the correct syntax to evaluate this environment variable inside a Windows batch script?

  1. IF "%ARCHIVE_PATH%"=="" (ECHO Archive path is not specified)Cevap
  2. B
    IF "$env:ARCHIVE_PATH"=="" (ECHO Archive path is not specified)
  3. C
    IF "$ARCHIVE_PATH"=="" (ECHO Archive path is not specified)
  4. D
    IF "WScript.CreateObject(ARCHIVE_PATH)"=="" (ECHO Archive path is not specified)

Cevap

The statement using the `%ARCHIVE_PATH%` syntax correctly references environment variables within a Windows batch (`.bat`) conditional statement.
In standard Windows Command Prompt batch scripts (`.bat`), environment variables are accessed by surrounding the variable name with percent signs, such as `%ARCHIVE_PATH%`. When placed inside an `IF` condition, string quotes ensure that empty or unassigned variables do not trigger command parser syntax errors.

Adım Adım Çözüm

1
Identify the scripting environment specified in the scenario.
The script is a Windows Command Prompt batch file (`.bat`).
Different scripting interpreters (Batch, PowerShell, Bash, VBScript) enforce distinct syntax rules for variable expansion.
2
Analyze how environment variables are enclosed and expanded in Windows batch scripting.
Batch files require enclosing environment variable names with percent symbols (e.g., `%VARIABLE_NAME%`).
This tells the `cmd.exe` interpreter to replace the placeholder with the runtime value stored in that variable.
3
Evaluate the choices to eliminate cross-language syntax constructs.
Distinguish `%VAR%` (Batch) from `env:VAR(PowerShell),env:VAR` (PowerShell), ` VAR` (Bash), and `WScript` (VBScript).
Only `%ARCHIVE_PATH%` complies with the native Windows Batch language specification.

Anahtar Kavram

Windows Batch Scripting Environment Variable Syntax
Bu soruyu puanla