Make a random string in batch - batch-file

I am trying to make a random string in batch, and I have no idea what to do. I have called a random string of numbers with the %random% function, but I have no idea how to permanently replace certain characters in my variable:
#echo off
set subkey1=%random%%random%%random%%random%%random%%random%
set subkey2=%random%%random%%random%%random%%random%%random%
set subkey3=%random%%random%%random%%random%%random%%random%
ECHO %subkey1:0=a%
ECHO %subkey1:1=b%
ECHO %subkey1:2=c%
ECHO %subkey1:3=d%
ECHO %subkey1:4=e%
ECHO %subkey1:5=f%
ECHO %subkey1:6=g%
ECHO %subkey1:7=h%
ECHO %subkey1:8=i%
ECHO %subkey1:9=j%
ECHO %subkey2:0=k%
ECHO %subkey2:1=l%
ECHO %subkey2:2=m%
ECHO %subkey2:3=n%
ECHO %subkey2:4=o%
ECHO %subkey2:5=p%
ECHO %subkey2:6=q%
ECHO %subkey2:7=r%
ECHO %subkey2:8=s%
ECHO %subkey2:9=t%
ECHO %subkey3:0=u%
ECHO %subkey3:1=v%
ECHO %subkey3:2=w%
ECHO %subkey3:3=x%
ECHO %subkey3:4=y%
ECHO %subkey3:5=z%
REM after going through all the letters, we just pick random ones
ECHO %subkey3:6=a%
ECHO %subkey3:7=g%
ECHO %subkey3:8=k%
ECHO %subkey3:9=o%
set key=%subkey1%%subkey2%%subkey3%
echo String: %key%
pause
So as you can see, I am able to ECHO different keys, but I want to save the new one to the variable (EX in subkey 1, I wanna replace all the 0s by a's, then SAVE that so if I look at my variable afterwards, I can see the new one, and be able to call that in different functions.

same effect with much less code:
#echo off
setlocal enabledelayedexpansion
set "string=abcdefghijklmnopqrstuvwxyz"
set "result="
for /L %%i in (1,1,30) do call :add
echo %result%
goto :eof
:add
set /a x=%random% %% 26
set result=%result%!string:~%x%,1!
goto :eof

To assign a variable a value, use the SET command:
SET subkey1=%random%%random%%random%%random%%random%%random%
SET subkey1=%subkey1:0=a%
SET subkey1=%subkey1:1=b%
SET subkey1=%subkey1:2=c%
.
.
.
ECHO %subkey1%
And, of course, beware that %random% should never, ever be used as a source for cryptographic purposes.

#echo off
setlocal EnableDelayedExpansion
set charSets=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!##$%^&*
set count=0
set /a countRaw=4+(%random%)%%20
for /L %%c in (1,1,%countRaw%) do (call :MAKERANDOMSTRING)
goto ENDRANDOMSTRING;
:MAKERANDOMSTRING
set buffer=% %
set count=0
set /a lowValue=30+(%random%)%%40
set /a length=10+!lowValue!
:Loop
set /a count+=1
set /a rand=%Random%%%69
set buffer=!buffer!!charSets:~%rand%,1!
if !count! leq !length! goto Loop
echo "%buffer%"
:ENDRANDOMSTRING

Related

Echoing a Space Character Into a File

I want to make a batch obfuscator with no outside tools, but I have a problem with echoing the spaces into the 'obfuscated' code('obfuscated' as in separating the code letter by letter between hundreds of random characters). I have found batch obfuscators that are used with CertUtil, but they can be de-obfuscated with another file, so I really want to get this working, and I haven't really found any unique ones.
When I run my file with the input file (contains #echo off and echo test), I get the obfuscated code:
$zz$j?$z?$#z#?$z?$z###$j?j#zz?$?##j$?$?$#z$$#zjj#zzz?j#jzj#zjjzj?$#$jj?j##z##?#$$$#zjz?$zj#j$zz#jzj###$z#j#j#??##zj#z#j$?$jzjz?$j?$?##zzz#???z$$jzj#??###jz$?$j$z$?$?$?j$j?$jzzj#j#z$zz$jzz#z#$jz#?z?$$$?e$?$j?$##?#zjjjjz#j$$zjjz##$?jzjj?$##?##j$#z#zj?zzjzj$$j#$j$????$z#j#$?j?#j$$$$#jzj$$j#z#z###?j#zjz#jc$#jjz$j#$zj?#z$$?j$z$#?zz$$zz#z?##j#zz#j#z##?$#z$j?$j?z$#?#$$z??j?zj#j$##?#$zz#$$$z?z$z?jz#?j?j#z$?jhj??z?zzj##$?#j$$j#j?#$?$j$$zzzzjjz?z?##$?$$j$zz#?j#?##z#jz$z#jz$z?zzj$###z#jzjzz$##?$j??$??#?zz##?$$ojj#$j$j##?#?jzj$z#?zz##$jjjj#j#?$##j?$?j$#jzzj#?$?jz#$#z#$j???z#$?z?jzzz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$##z$$?jjj$##z$$jz#z#z?#j##$?$$##z?z$z$$$#jj$?zj?#jz#z$jz?$#j$?#j##z$$jz?$z$$?z#?jz?$$zz$$$#$$?#jjjjz?zj?$$j?#$#?jjj?$$?j$$$#??j?zzz#?zjz$jjzz$?zjz##j#$?z#z$??$?$?jj?###jj##$zjjj$z$?###z#z$??z#jz#zjz?$?j#j$j#zj??j#z??z$$z#$$$$##z$?#?$$jjjj#zz#j###?j??z#$#$$?j##z#$#$$z#jjzj#$#jjjz?###$???zzzz$#$z?j$$$#??z?#j$j$???z?$jz$z?z???z?j#j??$z$$#$#$#j??$$##zjj#j#$$z$$?jj?jzjzjz$$z#zz?$?j$zjzjzz$j#j$#??j???jjj##z#$$$#j$#?j?#z$#$?#zj#?$$??jj?#z#?$##jzzz?$$$zjjjzzjz#$#?#$#jz$z?$z##jjjzj?z?j???j??##$zj$zj?#zjzj##j#z$$???#$jzj#z?z??jj##??z####?#?#z??j$????$$zz#jzj?z?j?z?#z$zz??j$?zz#jj##zz##??zj?#z??$?j###?zz#zjjzj$$z#??#?$?#$jj#j?$$$j?zzjz?$?#z#z$#jj#jz$???zj$jjjz$jz#j#$zz?jj#jj?#jj###?z#$j?zzj#jz?$#zj?#?j#?z??$z??jzz?#zj??j?$z##z#j$zjz#zz?$zj$?jjz?jjj$?j$$#z#?#j?zj$$j#jj#z?z#j#?jzzz$jjj$jzjz$j#j#j#?zzzz??z#z$jz#$#z$$?jz$j#?jz??jj#jj$z#z##j$j$##$???z??j?zzjz$??j#zzz#$jj#jzj#z$z$$?$z$jzzj#$#z$#z?z$###???$$?zz$#??j$?$j#j$#?zj$jzzj??j?z?zzz##?j$?z?j$jjz$jj?z#?j##$$?zjzjj#jjj??z$?#zzj$j#??z#zz$$j?jzj#j
However, when I manually remove all of the characters, I am left with:
#echooff
echotest
Is there a way to echo spaces into a file by itself so this problem will be fixed?
My code:
#echo off
set /a N=0
:file
set /p "file=Enter filename: "
if not exist %file% echo Please try again. The file you specified does not exist.
if not exist %file% pause>nul
if not exist %file% cls
if not exist %file% goto file
cls
echo Please wait.
set /a overallfirst=1
set /a double=0
for /F "usebackq tokens=*" %%f in ("%file%") do goto start
:start
set /a "tamp=%random%"
set /a loop2=0
set /a rand=%random% %%5 +1
if %rand%==1 echo #> %tamp%
if %rand%==2 echo $> %tamp%
if %rand%==3 echo ?> %tamp%
if %rand%==4 echo z> %tamp%
if %rand%==5 echo j> %tamp%
:chars
if %loop2%==100 goto next
set /a rand=%random% %%5 +1
if %rand%==1 <nul set /p=#>> %tamp%
if %rand%==2 <nul set /p=$>> %tamp%
if %rand%==3 <nul set /p=?>> %tamp%
if %rand%==4 <nul set /p=z>> %tamp%
if %rand%==5 <nul set /p=j>> %tamp%
set /a loop2+=1
goto chars
:next
for /f "usebackq tokens=*" %%f in ("%tamp%") do set chars=%%f
set "start=^<nul set /p=%chars%^>^> Obfuscated.bat"
del %tamp%
set /a loop=-1
set /a first=1
for %%i in (%file%) do #set count=%%~zi
echo #echo off> Chars.bat
if %double%==0 echo empty^> Obfuscated.bat>> Chars.bat
echo for /f "tokens=*" %%%%a in (%file%) do set id=%%%%a ^& call :processline %%%%a >> Chars.bat
echo cls>>Chars.bat
echo echo Obfuscated code:>>Chars.bat
echo echo -------------------------->>Chars.bat
echo type Obfuscated.bat>>Chars.bat
echo pause^>nul>>Chars.bat
echo exit >> Chars.bat
echo goto :eof >> Chars.bat
echo :processline >> Chars.bat
:loop
set /a loop=%loop%+1
if %loop%==%count% goto end
if %first%==1 set first=0 & goto other
set "command=^<nul set /p=%%id:~%loop%,1%%"
echo %command%^>^> Obfuscated.bat>> Chars.bat
goto next1
:other
set "command=%start%"
echo %command%>> Chars.bat
set /a loop=%loop%-1
goto loop
:next1
set /a "tamp=%random%"
set /a loop2=0
set /a rand=%random% %%5 +1
if %rand%==1 echo #> %tamp%
if %rand%==2 echo $> %tamp%
if %rand%==3 echo ?> %tamp%
if %rand%==4 echo z> %tamp%
if %rand%==5 echo j> %tamp%
:chars
if %loop2%==100 goto next2
set /a rand=%random% %%5 +1
if %rand%==1 <nul set /p=#>> %tamp%
if %rand%==2 <nul set /p=$>> %tamp%
if %rand%==3 <nul set /p=?>> %tamp%
if %rand%==4 <nul set /p=z>> %tamp%
if %rand%==5 <nul set /p=j>> %tamp%
set /a loop2+=1
goto chars
:next2
for /f "usebackq tokens=*" %%f in ("%tamp%") do set chars=%%f
set "command=^<nul set /p=%chars%"
echo %command%^>^> Obfuscated.bat>> Chars.bat
del %tamp%
goto loop
:end
echo echo. ^>^> Obfuscated.bat>> Chars.bat
echo goto :eof >> Chars.bat
echo :eof >> Chars.bat
start Chars.bat
if %overallfirst%==1 set /a overallfirst=0 & set /a double=1
I have looked for a solution, but I was unable to find one.
(I am fairly new to Batch coding so this script is not very efficient)
Try using <nul set /p and echo/. This post may help: Preserve spaces with <Nul set /p

Issues with rpg stat system

Im making an rpg game in batch, and here is the code for the stat system so far
:SPSPEND
CLS
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if %choice%=="1" Set /a Strength==%strength%+5
if %choice%=="2"
if %choice%=="3"
if %choice%=="4"
if %choice%=="5"
if %choice%=="6"
cls
set /a SP==%SP%-1
goto :rest
Not sure how to format soz. i need help with two things : how can i make it so that it doesnt close out and actually works, and how can i make a failsafe incase a person enters something other than what i already have set up.
Thanks in advance
I believe this is what you want.
I have tested it and am fairly certain it will work
:SPSPEND
CLS
if %SP% leq 0 goto end
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if "%choice%"=="1" Set /a Strength=%Strength%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="2" Set /a Agility=%Agility%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="3" Set /a Magic=%Magic%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="4" Set /a MaxHP=%MaxHP%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="5" Set /a Defence=%Defence%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="6" Set /a Archery=%Archery%+5& set /a SP=%SP%-1& goto SPSPEND
echo invalid choice
pause
goto SPSPEND
cls
:end
pause
goto :rest

display a variable like %%i in for statements in variable? batch

How do I use a variable like %%i in a permanent variable?
(I don't really know the correct terms, so I hope anyone can figure out what I mean)
This is the code I am using:
#echo off
color 0f
goto number
:number
title number
cls
echo number of options?
set /p num=
goto option
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter a option
set /p n%%i=
echo %%i = %n%%i% >> log.txt
)
goto select
:select
cls
echo %n2%
pause >nul
the "%n2%" works for whatever you put in second, but when I try to print it into a file ( echo %%i = %n%%i% >> log.txt ) it doesn't work.
I know the "%n%%i%" is not correct, But I don't really know what to actually put there.
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter an option
set /p option%%i=
)
goto select
:select
set option>log.txt
cls
echo %option2%
You may like to consider this.
The command
set option
will show every environment variable that starts option in the format option1=Gido, which is why I changed n to option (there are other variables set that start n)

batch script to add number to a string in loop

i need to generate list of dummy users. no of users will be provided as a input.. like 5 or 500 or 5000.
all i want is to have a standard text like usr and append a number and generate the list like usr1, usr2, usr3 etc. I thought i can do this in batch file quickly. but stuck with loop and appending the number to the string.. can some help?
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=%name1%%numb2%
echo %name1%
echo %name2%
)
with a slight modification of your code...
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=!name1!!numb2!
echo !name1!
echo !name2!
)
endlocal
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
>q28342811.txt ECHO tstusr%random%
:loop
set "name=tstusr%random%"
FINDSTR /x "%name%" q28342811.txt >NUL
IF ERRORLEVEL 1 SET /a numb -=1&>>q28342811.txt ECHO %name%
IF %numb% gtr 1 GOTO loop
TYPE q28342811.txt
GOTO :EOF
Produces q28342811.txt
generate a random username to the output file, then repeat the operation numb-1 times, checking that the new candidate doesn't already exist in the file first.

Variables disappear when making .txt's

I'm writing a batch code that when prompted will write a .bat file. This is an example of the code I'm using to create the .bat:
echo set /a num=0 >>%fileinfo%.bat
echo :repeat1 >>%fileinfo%.bat
echo set /a num=%num% +1 >>%fileinfo%.bat
echo echo %num% >>%fileinfo%.bat
echo if %num%==100 goto end >>%fileinfo%.bat
echo goto repeat1 >>%fileinfo%.bat
This is the original code:
set /a num=0
:repeat1
set /a num=%num% +1
echo %num%
if %num%==100 goto end
goto repeat1
What happens when I run the code and create the new .bat:
set /a num=0
:repeat1
set /a num= +1
echo
if ==100 goto end
goto repeat1
It takes out the %num%, can anyone think of a way around this or an alternative method to create the file? Thanks.
echo set /a num=0 >>%fileinfo%.bat
echo :repeat1 >>%fileinfo%.bat
echo set /a num=%%num%% +1 >>%fileinfo%.bat
echo echo %%num%% >>%fileinfo%.bat
echo if %%num%%==100 goto end >>%fileinfo%.bat
echo goto repeat1 >>%fileinfo%.bat
#ECHO OFF
SETLOCAL
SET "fileinfo=q20296357x"
(
echo set /a num=0
echo :repeat1
echo set /a num=%%num%% +1
echo echo %%num%%
echo if %%num%%==100 goto end
echo goto repeat1
)>>%fileinfo%.bat
TYPE %fileinfo%.bat
GOTO :EOF
Your fundamental problem is that %var% is replaced by the CONTENTS of var before the line is executed. For some characters which have a special meaning to batch like the redirectors >|< you need to prefix the special character with ^.
Bizarrely, to "escape" %, the escape charater is not ^ but % itself - so each literal % needs to be coded as %%
Note how your code can be condensed by parenthesising the ECHOs and redirecting to your target file.
Note also that you could also code ...goto :EOF instead of ...goto end if you actually want to go to the end-of-file. eof is a magic label - batch understands it to be end-of-file so it should not be declared in your batch. Case is not important, but the colon is required.

Resources