批处理文件输出随机数字和字母

时间:2014-08-22 15:02:23

标签: batch-file

我想创建一个批处理文件,将文本文件输出数字和字母的组合,我找到了一个很好的链接到这里的帖子,有人可以实现这个,但我有两个问题,他们是,我不喜欢理解它,当我运行它时它不起作用,有人能告诉我我做错了什么吗?我还想添加像@这样的字符吗?

谢谢

Creating a word list using batch

2 个答案:

答案 0 :(得分:0)

这将从其中的字符串中的字符集创建一行随机字符。

在批处理文件名之后用数字启动它 - 它会给你很多字符。

@echo off
if "%~1"=="" (
echo Run this batch file with a number after the name
echo It will put that number of random characters into the output.txt file
echo(
pause
goto :EOF
)

setlocal enableDelayedExpansion

:: set the characters here - do not use ! in the set

set "chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

:: count length of string
for /f "tokens=1* delims=:" %%a in (
    '^(for %%i in ^("%chars%" .^) do @echo %%i^) ^| findstr /o .^| findstr /v /b 0') do set /a maxPos=%%~a-5-1


:: output the %1 number of random characters into the output.txt file
(
   for /l %%I in (1 1 %1) do (
      set /a rand=!random! %% %maxPos%
      call set /p "var=%%chars:~!rand!,1%%"<nul
   )
)>output.txt

答案 1 :(得分:-1)

echo off
Setlocal EnableDelayedExpansion

:: "Set Alphanumeric=" you can define the Dictionary


set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

echo set length
set /p "Testlngth=>"
:Top_G_1

:: replace 62 with the amount of letters in the Dictionary

set /a Rnd=%random% %% 53-0
if %Rnd% gtr 53 goto Top_G_1


set Output=%Output%!Alphanumeric:~%Rnd%,1!

set /a Length=%length% +1

if %length% lss %Testlngth% goto Top_G_1

echo %Output%

pause