Batch File: ( was unexpected at this time
Solution 1
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
Solution 2
Oh, dear. A few little problems...
As pointed out by others, you need to quote to protect against empty/space-containing entries, and use the !delayed_expansion! facility.
Two other matters of which you should be aware:
First, set/p will assign a user-input value to a variable. That's not news - but the gotcha is that pressing enter in response will leave the variable UNCHANGED - it will not ASSIGN a zero-length string to the variable (hence deleting the variable from the environment.) The safe method is:
set "var="
set /p var=
That is, of course, if you don't WANT enter to repeat the existing value.
Another useful form is
set "var=default"
set /p var=
or
set "var=default"
set /p "var=[%var%]"
(which prompts with the default value; !var! if in a block statement with delayedexpansion)
Second issue is that on some Windows versions (although W7 appears to "fix" this issue) ANY label - including a :: comment (which is a broken-label) will terminate any 'block' - that is, parenthesised compound statement)
Solution 3
you need double quotes in all your three if statements, eg.:
IF "%a%"=="2" (
@echo OFF &SETLOCAL 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 "%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
USB Write is Locked!
)
if "!param1!"=="2" (
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies\ /v WriteProtect /t REG_DWORD /d 00000000
USB Write is Unlocked!
)
)
pause
Related videos on Youtube
user2540289
Updated on June 13, 2021Comments
-
user2540289 over 1 yearI 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-
Darius X. over 9 yearsIf you turn echo on (first line), do you see something helpful?
-
-
David Ruhmann over 9 years+1 Great point about the hack label comments. Here is an interesting discussion about the weird label comment behavior here: dostips.com/forum/viewtopic.php?f=3&t=4204 -
Magoo over 9 years@David Interesting discussion, but I'd reject the idea that::is a failed logged-drive switch. If it was, it should gnerate an error outside of a block. Whereas the academic argument may be interesting, sadly the rules and regulations regarding positioning of labels/::-comments appear beyond the attention-span of today's average or casual batcher and the potential for breakage during maintenance makes "clever" usage too dangerous for my taste. Not sure whether the situation is version-dependent, either. I'll stick to a no-labels/::-comments-in-a-block philosophy for sanity's sake. -
David Ruhmann over 9 yearsI agree with you. Hack label comments should always be avoided inside parentheses due to their undefined behavior. The discussion was just for those who wanted to read more about the behaviors and why it is bad to use them. -
PhilC almost 8 years:: Comment within a for loop was my problem. This was the first item that came up in my search. Thank you! -
Ivandro Jao almost 3 years"%var%"=="expected" -
Nogard almost 2 yearsYeah, that's a great answer! -
bimjhi over 1 year@Endoro Your answer solved my problem which took many hours of my time. Thak you!