/?' was unexpected at this time error in batch file.
This is because you have used brackets (
and )
to group multiple commands.
Your code contains the following:
( ... Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZ ... @()\/?'=-_+ )
This means that the wrong )
(the one in the set
) is matching the first opening (
, hence the error.
In fact you don't need to use brackets (
and )
to group multiple commands if a couple of other small changes are made, which is to re-initialise some variables because you have a new outer loop in order to generate multiple passwords.
Fixed batch file:
@echo off set executecounter=0 setlocal setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION :loop Set _RNDLength=32 Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@()\/?'=-_+ Set _Str=%_Alphanumeric%987654321 SET _RndAlphaNum= set _RND= set _len= :_LenLoop IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop SET _tmp=%_Str:~9,1% SET /A _Len=_Len+_tmp Set _count=0 SET _RndAlphaNum= :_loop Set /a _count+=1 SET _RND=%Random% Set /A _RND=_RND%%%_Len% SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1! If !_count! lss %_RNDLength% goto _loop Echo Random string is !_RndAlphaNum!>> d:\password2.txt set /a executecounter=%executecounter%+1 if "%executecounter%"=="1000" goto done goto loop :done echo Complete! endlocal pause
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- brackets - Using parenthesis/brackets to group expressions.