Reputation: 1
I am working on a batch file and I need to set a variable (a) to be equal to a SPACE " "
character.
This is my code. (following the suggestions of one of the answerers)
This is supposed to output something along the lines of: " + + "
@echo off
set "pix1= "
set "pix2= "
set "pix3= "
set "pix4= "
set /a a=%random% %%4
set /a b=%random% %%4
set /a c=%random% %%4
set /a d=%random% %%4
if %a% EQU 1 (Set "pix1=%pix1%+")Else Set "pix1=%pix1% "
if %b% EQU 1 (Set "pix2=%pix2%+")Else Set "pix2=%pix2% "
if %c% EQU 1 (Set "pix3=%pix3%+")Else Set "pix3=%pix3% "
if %d% EQU 1 (Set "pix4=%pix4%+")Else Set "pix4=%pix4% "
echo|set /p "p=%pix1%"
echo|set /p "p=%pix2%"
echo|set /p "p=%pix3%"
echo|set /p "p=%pix4%"
echo.
pause
the current output is this "++" (assuming that the random number generated one twice and in the same order as the first example.)
the spaces are being lost somewhere.
any help is appreciated.
Upvotes: 0
Views: 87
Reputation: 2951
place your quotes at the start of the variable name and the end of the desired variable value.
Set "space= "
to get your desired output, you need to concatenate the variable, in your example, your are just conditionally assigning them a new value instead of appending the value to the variable
if %a% EQU 1 (Set "pix1=%pix1%+")Else Set "pix1=%pix1% "
If you are looking to generate random strings, here is a function with documentation for that purpose.
Below is an exmple of using the above linked function to generate a 30x60 field of random whitespace and + characters:
/L:60
is the length of each line"/P: + "
is the character set to use. whitespace is supplied twice to weight the randomness towards whitespace/O
outputs the generated string to STDOUT
/S:3
tells GenStr how many characters are in the pattern. Not required, but saves the time that would have been spent determining the pattern length%~n0_Screen.Dat
; where %~n0
is the name of your script.Note: this function is a general purpose function for random strings of a varied degree of complexity. It is excessive to your needs, however is intended to meet the needs of others with more complex random string requirements as well as yours. The documentation within should easily provide you with a learning resource about how to generate random strings using a given character set, it's size, and random number generation.
@Echo off & CLS
Setlocal EnableDelayedExpansion
(For /L %%Y in (1 1 30)Do (
Call :GenStr "/p: + " /S:3 "/V:Line%%Y" /L:60 /O
)) >"%TEMP%\%~n0_Screen.dat"
TYPE "%TEMP%\%~n0_Screen.dat"
Endlocal
Pause
Goto :Eof
:GenStr ============================================:# Author: T3RRY :Created 09/03/2021 Version:1.0.3
::: Purpose: verbose Random String Generator to suit a wide range of needs.
::: Features: Supports Generation of Delimited Strings of Varying Lengths using a range of predefined
::: character sets in addition to accepting an argument for a custom character set.
::: Generates string without output unless /O arg is used, returns String To /V:Variablename
::: or RV if the /V: Arg is not used.
::: See the Examples and help documentation for more information.
rem /* Function Help */
If "%~1" == "" (
Mode 160,200
(For /F "Tokens=2* Delims=#" %%G in ('Findstr /BLIC:":#" "%~f0"')Do @Echo(%%G) | @More
Exit /B 1
)
If /I "%~1" == "Help" (
Mode 160,200
(For /F "Tokens=2* Delims=#" %%G in ('Findstr /BLIC:":#" "%~f0"')Do @Echo(%%G) | @More
Exit /B 0
)
:#---------------------------------------------------------------------------------------------------
:#| GenStr Args: Description:
:#|
:#| "/L:Int" : Length of each Substring [MANDATORY] - At least one length must be supplied.
:#| "/L:Int Int Int" : Varying Lengths may be provided for each Substring
:#| by supplying the length for the corresponding iteration
:#| If multiple lengths provided without /R, /R:Iterations is defined
:#| as the number of lengths provided.
:#|
:#| /V:ReturnVar : Define Supplied returnvar with generated String
:#| - If no return var is supplied, value will be stored in:RV
:#| /A:ReturnVar : Append generated string to supplied return variable.
:#|
:#| /P:Pattern Subargs: ANM - English Letters in Upper and Lower case + 0~9 [DEFAULT]
:#| ANL - English Letters in Lower case + 0~9
:#| ANU - English Letters in Upper case + 0~9
:#| AL - English Letters in Lower case
:#| AU - English Letters in Upper case
:#| N - 0~9
:#| H - Hex String: ABCDEF0123456789
:#| Characterstring - provide a custom string of characters to use.
:#| Example: "/P:-+@$^&()"
:#| /S:Int : Supply the length of the Custom Pattern. If not supplied
:#| the length is calculated, increasing proccesing time.
:#|
:#| /R:Int : Number of iterations to repeat pattern as an Integer
:#|
:#| "/D:Delim" : Delim Character to use.
:#| "/D:Delim Delim" : Supports Multiple Delims.
:#| - /D:SC for : Semicolon
:#| - /D:FS for / Forward-Slash
:#| - /D:AS for * Asterisk
:#| - /D:WS for " " whitespace
:#|
:#| /O : Output the Generated string to STDOUT
:#|
:#| "/B:Delim Delim" : Supply a pair of Delims to Bookend the string with.
:#|
:#---------------------------------------------------------------------------------------------------
Setlocal EnableExtensions EnableDelayedExpansion
For %%G in ( B _B{i} CharSet D _D L _L O P RV R RF S V )Do Set "%%G="
rem /* Function Arg Handling */
For %%G in (%*) Do For /F "Tokens=1,2 Delims=/:" %%H in ("%%~G")Do (
If "%%H" == "O" ( Set "O=_" )Else Set "%%H=%%I"
)
rem /* Default Repeat Iterations EQU 1 ; Flag R definition forced [To Redefine if multiple lengths provided] */
If not Defined R ( Set "R=1" & Set "RF=_" )
rem /* String Length Argument Mandatory */
If Not Defined L (
Echo "/L:Integer" Length Missing.
Endlocal & Exit /B 1
)
Echo(!L!|%__APPDIR__%findstr.exe /RXC:"[0123456789 ]*" > Nul || (
Echo(Invalid Arg For "/L:Length". Integers and whitespace only.
Pause
Endlocal & Exit /B 1
)
rem /* Facilitate Generation of Strings with substrings of varying length
rem Default Array to same /L:Length for each /R:iteration; Override Array if different lengths Provided */
rem If Length list provided and R definition is Forced; define /R:Iterations to match number of lengths.
Set "i=0"
For /L %%i in (1 1 !R!)Do ( Set "_L%%i=!L!" & Set "_D%%i=!D!" )
IF Not "!L: =!" == "!L!" For %%G in (!L!)Do (
Set /A "i+=1"
Set "_L!i!=%%G"
If Defined RF Set "R=!i!"
)
rem /* Handle Arg proccessing of Delim Characters "/" ":" asterisk and whitespace in /D: and /B: Delim Args.
rem /* Default is no Delim. */
rem /* Support Delim List via Array to facilitate different delims in string. */
(For /F "Tokens=1,2 Delims==" %%G in ('Set _D')Do Set "%%G=") 2> nul
For %%S in (B D)Do If defined %%S (
Set "i=0"
IF Not "!%%S: =!" == "!%%S!" (
For %%G in (!%%S!)Do (
Set /A "i+=1"
Set "_%%S=%%G"
Set "_%%S=!_%%S:SC=:!"
Set "_%%S=!_%%S:FS=/!"
Set "_%%S=!_%%S:AS=*!"
Set "_%%S=!_%%S:WS= !"
Set "_%%S!i!=!_%%S!"
Set "_%%S{i}=!i!"
)
)Else (
For /L %%i in (1 1 !R!)Do (
Set "_%%S%%i=!%%S!"
Set "_%%S%%i=!_%%S%%i:WS= !"
Set "_%%S%%i=!_%%S%%i:SC=:!"
Set "_%%S%%i=!_%%S%%i:FS=/!"
Set "_%%S%%i=!_%%S%%i:AS=*!"
Set "_%%S{i}=%%i"
)))
rem /* Default Generation Set is ANM. Use Arg /P:Pattername to use a specific Set */
rem /* Assign Character Sets and S index length based on /P:Pattern Arg */
rem /* Force Default /P:Pattern */
If Not Defined P Set "P=ANM"
If /I "!P!" == "ANM" Set "CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" & Set "S=62" )
If /I "!P!" == "ANU" ( Set "CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" & Set "S=36" )
If /I "!P!" == "ANL" ( Set "CharSet=abcdefghijklmnopqrstuvwxyz0123456789" & Set "S=36" )
If /I "!P!" == "AL" ( Set "CharSet=abcdefghijklmnopqrstuvwxyz" & Set "S=26" )
If /I "!P!" == "AU" ( Set "CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZ" & Set "S=26" )
If /I "!P!" == "N" ( Set "CharSet=1234567890" & Set "S=10" )
If /I "!P!" == "H" ( Set "CharSet=ABCDEF0123456789" & Set "S=16" )
rem /* facilitate custom "/P:Pattern" */
If not Defined CharSet (
Set "CharSet=!P!"
If Not Defined S (
For /L %%i in (0 1 200) Do If "!S!" == "" If "!P:~%%i,1!" == "" ( Set "S=%%i" )
)
)
rem /* For each /R:iteration append /L:length characters at %%c index of /P:pattern
rem - then append /D:Delim or Delim%%i if Delim list used */
For /L %%i in (1 1 !R!) Do (
FOR /L %%n IN (1 1 !_L%%i!)Do (
For /F "delims=" %%c In ( 'SET /A "!RANDOM! %% S"' )Do (
Set "RV=!RV!!CharSet:~%%c,1!"
))
Set "RV=!RV!!_D%%i!"
)
rem /* Trim trailing Delim if Defined */
If Defined D If not "!_D%R%!" == "" Set "RV=!RV:~0,-1!"
rem /* Add bookends from _B array if /B:BookendDelims Arg used */
If Defined B Set "RV=!_B1!!RV!!_B%_B{i}%!"
rem /* Return the Generated string across the endlocal barrier to Specified /V:Variable or Variable:RV */
If Not Defined A If Defined O Echo(!RV!
If "%V%" == "" If "%A%" == "" ( Set "RV=%RV%" & Exit /B 0 )
If Defined A (
Set "Rv=!%A%!%RV%"
If Defined O Echo(!RV!
For %%v in ("!RV!")Do Endlocal & Set "%A%=%%~v"
Exit /B 0
)
If Defined V (
Endlocal & Set "%V%=%RV%"
Exit /B 0
)
Exit /B 1
:#| GenStr Usage Examples
:#|
:#| Call :Genstr help
:#| Call :GenStr /L:5 "/V:Single Iteration Default Alphanumerical" /O
:#| Call :GenStr /P:H /L:3 /R:4 "/D:-" "/V:HEX 4x3 - Delim" /O
:#| Call :GenStr /P:N /L:4 /R:3 /D:SC "/V:Numerical 3x4 Semicolon Delim" /O
:#| Call :GenStr "/L:4 3 2" /R:3 "/D:|" /P:ANL "/V:AlphaNumLowercase 4;3;2 Pipe Delim" /O
:#| Call :GenStr /L:5 /R:2 "/P:+@#$^&()|" "/D:-" "/V:2x5 custom characters" /O
:#| Call :GenStr /L:4 /R:4 "/D:+ - +" /P:N "/V:4x4 Mulitple Delims Numerical" /O
:#|
:#---------------------------------------------------------------------------------------------------
example of using the above to create a popluted field:
Upvotes: 1