我正在处理一个批处理文件,我需要将变量 (a) 设置为等于空格 " "
字符。
这是我的代码。 (按照其中一位回答者的建议)
这应该输出一些类似的东西:“++”
@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
当前输出是这个“++” (假设随机数生成了两次,并且顺序与第一个示例相同。)
空间正在某处丢失。
感谢任何帮助。
答案 0 :(得分:1)
将引号放在变量名称的开头和所需变量值的结尾。
Set "space= "
要获得所需的输出,您需要连接变量,在您的示例中,您只是有条件地为它们分配一个新值,而不是将值附加到变量
if %a% EQU 1 (Set "pix1=%pix1%+")Else Set "pix1=%pix1% "
如果您想生成随机字符串,here 是一个带有用于此目的的文档的函数。
以下是使用上述链接函数生成随机空白和 + 字符的 30x60 字段的示例:
/L:60
是每行的长度"/P: + "
是要使用的字符集。空白被提供两次以加权向空白的随机性/O
将生成的字符串输出到 STDOUT
/S:3
告诉 GenStr 模式中有多少个字符。不需要,但可以节省确定模式长度所花费的时间%~n0_Screen.Dat
;其中 %~n0
是您的脚本名称。注意:此函数是用于各种复杂程度的随机字符串的通用函数。它超出了您的需求,但旨在满足其他具有更复杂随机字符串要求的需求以及您的需求。里面的文档应该很容易为您提供关于如何使用给定字符集生成随机字符串、它的大小和随机数生成的学习资源。
@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
:#|
:#---------------------------------------------------------------------------------------------------
使用上述创建填充字段的示例: