1

I have a batch script that generates a random 75 digit long password string. When the password string starts with a 0 value other parts of automation I setup become unstable. I need a way to identify when the very first digit in the random password string is a zero.

My plan will be to reject the string if the first character of it is a 0 and generate a new password.

Note: I found a couple solutions but both were for small strings way less than 75 digits.

Batch File

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

set execute counter=0
:loop
Set "Chars=1234567890"
Set CharsCnt=10
Set "Pass="
For /L %%c in (1,1,75) do (
  Set /a Pnt=!Random! %% CharsCnt
  Call Set "Pass=!Pass!%%Chars:~!Pnt!,1%%"
)
Echo Password is:%Pass%
start "" https://check.pass/%pass%
goto loop

Output

Without leading 0

115792089237316195423570985008687907852837564279074904382605163141518161495

With a leading 0 (automation becomes unstable)

011579208923731619542357098500868790785283756427907490438260516314151816149
0

3 Answers 3

3

"reject output if there is leading 0 in string and recalculate random string"

Since you say you want to identify the very first character of the password string and if that first digit equals 0 to then generate another random password string, I provided a solution you can use below.

Essentially this will. . .

  • Use Variable Substring to get just the first character of the string (i.e. %var:~0,1%) and then with an If statement, check whether or not that equals 0.
    • If it equals 0 then goto :loop otherwise process on accordingly
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

set execute counter=0
:loop
Set "Chars=1234567890"
Set CharsCnt=10
Set "Pass="
For /L %%c in (1,1,75) do (
  Set /a Pnt=!Random! %% CharsCnt
  Call Set "Pass=!Pass!%%Chars:~!Pnt!,1%%"
)
if [%Pass:~0,1%]==[0] goto :loop
Echo Password is:%Pass%
echo start "" https://check.pass/%pass%
goto :loop

Note: You can use whatever logic in the if statement rather than goto :loop for whatever you want it to do if the value does equal 0I used if [%Pass:~0,1%]==[0] goto :loop assuming that's what you need though as it's not clear what you want it to do if it removes leading zeros from the string if you just want the batch to regenerate a string without a leading zero. Removing the leading 0 from the string is possible too but I'm not sure if the string has a character length requirement or whatever since you say you want it to regenerate a string, that seems like the simplest solution.


Further Resources

0
2

Easiest solution I can think of: generate one digit from 1-9, then 74 more digits from 0-9.

2
  • 1
    this is not what i asked
    – RAJA
    Commented Dec 18, 2018 at 13:18
  • 1
    True, but its result should 1) be functionally identical, 2) meet all specified requirements, and 3) perform faster. Commented Dec 18, 2018 at 14:07
0

It just so happens that string substitution happens to have something you are after..so you're in some luck and don't need to be a big batch expert to do this.

batch string substitution is described in set /? !

C:\Users\user>set /?

........
Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.
........

C:\Users\user>set z=0000123

C:\Users\user>set z=%z:*0=% & echo %z%
0000123

C:\Users\user>set z=%z:*0=% & echo %z%
000123

C:\Users\user>set z=%z:*0=% & echo %z%
00123

C:\Users\user>set z=%z:*0=% & echo %z%
0123

C:\Users\user>set z=%z:*0=% & echo %z%
123

C:\Users\user>

So if you run a line like set z=123 then run a line like z=%z:*0=% a bunch of times like 80 times then it will remove as much as up to 80 leading zeros, which is more than enough to cover all the leading zeros that might be part of the 75 digits of your string.

And in a batch file, like in your batch file, you'll have to use !var!(for expanding variables and %%(for string substitution instead of %var%(for expanding variables) and %(in string substitution), so, just as your batch file already does, as well as that top line like your batch file has with the setlocal and what follows it. So your batch file already has that fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .