-2

I am trying to integrate a password generator into my batch file so that it generates multiple passwords.

Unfortunately it give the following error:

\/?' was unexpected at this time.

The expected output is multiple (1000) lines of the form:

Random string is {password}

Where {password} consists of 32 random characters from the string _Alphanumeric.

Here is my batch file:

@echo off
set executecounter=0
setlocal
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789


:loop
(@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=32
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@()\/?'=-_+
Set _Str=%_Alphanumeric%987654321
:_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
pause 

)
set /a executecounter=%executecounter%+1
if "%executecounter%"=="1000" goto done
goto loop
:done
echo Complete!
endlocal
pause

How can I resolve this error?

  • Yes you did :)) But this is different. This was to other one I told you about – Unix Jul 22 '15 at 11:19
  • Don't remember, it was from this site but it was before I made my account and since I closed the browser there ware no history left. I'll edit it a bit later cause I'm on a mobile connection, I need to go and make a new contract now :D – Unix Jul 22 '15 at 11:57
  • 1
    Anyways I've fixed it ;) – DavidPostill Jul 22 '15 at 12:06
  • 1
    I've edited the question for you and answered with a corrected batch file ;) – DavidPostill Jul 22 '15 at 12:30
1

/?' 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

  • Sorry for late reply, I had to attend some other projects. Thanks for the help David, I'll give it a try and get back to you as soon as possible ;) – Unix Aug 7 '15 at 1:06
  • Damn, it works like a charm and, it just as I thought, it works extremely faster then the previous code (it generated the 1000 strings in under 10 minutes). It's gonna make a fine tool for pen-testing word listing! – Unix Aug 7 '15 at 1:15
0

Can you use PowerShell for something much simpler?

$randomObj = New-Object System.Random
$NewPassword=""
1..12 | ForEach { $NewPassword = $NewPassword + [char]$randomObj.next(33,126) }
$NewPassword
  • I never used PowerShell. It dosen't look to user friendly for me... – Unix Jul 22 '15 at 11:23
  • @Unix give it a shot. It's object oriented so takes a little bit to get used to. That script does essentially: 1. Create an object of type System.Random 2. Create a variable called $NewPassword as a string 3. Loop 12 times, each time adding a random character to the empty string 4. Display the resulting string – beeks Jul 22 '15 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.