簡體   English   中英

批量設置變量等於“ ”

[英]Setting a variable to be equal to “ ” in batch

我正在處理一個批處理文件,我需要將一個變量 (a) 設置為等於空格" "字符。

這是我的代碼。 (根據其中一位回答者的建議)

這應該是 output 的東西:“+ +”

@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

當前的 output 是這個“++”(假設隨機數生成了兩次,並且與第一個示例的順序相同。)

空間正在某處丟失。

任何幫助表示贊賞。

將引號放在變量名稱的開頭和所需變量值的結尾。

Set "space= "

要獲得所需的 output,您需要連接變量,在您的示例中,您只是有條件地為它們分配一個新值,而不是將值附加到變量

if %a% EQU 1 (Set "pix1=%pix1%+")Else Set "pix1=%pix1% "

如果您正在尋找生成隨機字符串,這里是 function 與用於此目的的文檔。

編輯。

下面是使用上面鏈接的 function 生成隨機空格和 + 字符的 30x60 字段的示例:

  • /L:60是每行的長度
  • "/P: + "是要使用的字符集。 提供兩次空白以對空白加權隨機性
  • /O將生成的字符串輸出到STDOUT
  • /S:3告訴 GenStr 模式中有多少個字符。 不需要,但節省了確定模式長度所花費的時間
  • 用於生成字符串的 for 循環用括號括起來,所有 output 都被重定向到臨時文件%~n0_Screen.Dat 其中%~n0是腳本的名稱。

注意:此 function 是通用 function,用於各種復雜程度的隨機字符串。 它超出了您的需求,但是旨在滿足具有更復雜隨機字符串要求的其他人以及您的需求。 里面的文檔應該很容易為您提供有關如何使用給定字符集、大小和隨機數生成生成隨機字符串的學習資源。

@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
:#|
:#---------------------------------------------------------------------------------------------------

使用上述方法創建填充字段的示例:

使用上述方法創建填充字段的示例:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM