Search code examples
if-statementbatch-filecmd

Batch File: ( was unexpected at this time


I am getting this error:

( was unexpected at this time

The error occurs after accepting the value of a. I tried and checked for null values that could cause such a problem,, but was unsuccessful.

echo off
cls
title ~USB Wizard~
echo What do you want to do?
echo 1.Enable/Disable USB Storage Devices.
echo 2.Enable/Disable Writing Data onto USB Storage.
echo 3.~Yet to come~.

set "a=%globalparam1%"
goto :aCheck
:aPrompt
set /p "a=Enter Choice: "
:aCheck
if "%a%"=="" goto :aPrompt
echo %a%

IF %a%==2 (
title USB WRITE LOCK
echo What do you want to do?
echo 1.Apply USB Write Protection
echo 2.Remove USB Write Protection
::param1
set "param1=%globalparam2%"
goto :param1Check
:param1Prompt
set /p "param1=Enter Choice: "
:param1Check
if "%param1%"=="" goto :param1Prompt

if %param1%==1 (
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\ /v WriteProtect /t REG_DWORD /d 00000001 
echo USB Write is Locked!
)
if %param1%==2 (
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\ /v WriteProtect /t REG_DWORD /d 00000000
echo USB Write is Unlocked! 
)
)
pause

Solution

  • You are getting that error because when the param1 if statements are evaluated, param is always null due to being scoped variables without delayed expansion.

    When parentheses are used, all the commands and variables within those parentheses are expanded. And at that time, param1 has no value making the if statements invalid. When using delayed expansion, the variables are only expanded when the command is actually called.

    Also I recommend using if not defined command to determine if a variable is set.

    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    cls
    title ~USB Wizard~
    echo What do you want to do?
    echo 1.Enable/Disable USB Storage Devices.
    echo 2.Enable/Disable Writing Data onto USB Storage.
    echo 3.~Yet to come~.
    
    set "a=%globalparam1%"
    goto :aCheck
    :aPrompt
    set /p "a=Enter Choice: "
    :aCheck
    if not defined a goto :aPrompt
    echo %a%
    
    IF "%a%"=="2" (
        title USB WRITE LOCK
        echo What do you want to do?
        echo 1.Apply USB Write Protection
        echo 2.Remove USB Write Protection
    
        ::param1
        set "param1=%globalparam2%"
        goto :param1Check
        :param1Prompt
        set /p "param1=Enter Choice: "
        :param1Check
        if not defined param1 goto :param1Prompt
        echo !param1!
    
        if "!param1!"=="1" (
            REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\ /v WriteProtect /t REG_DWORD /d 00000001 
            echo USB Write is Locked!
        )
        if "!param1!"=="2" (
            REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\ /v WriteProtect /t REG_DWORD /d 00000000
            echo USB Write is Unlocked! 
        )
    )
    pause
    endlocal