Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 从批处理文件中的文件中随机选取一行_String_Batch File_Generator - Fatal编程技术网

String 从批处理文件中的文件中随机选取一行

String 从批处理文件中的文件中随机选取一行,string,batch-file,generator,String,Batch File,Generator,我正在尝试制作一个Character\u Picker.bat,它从characters.txt一个长文本文件中回显随机选取的字符串。我不知道如何随机挑选 Characters.txt: Disco Jockey Acid Pol Double Agent Acid Pol Muaythai Acid Pol Paramedic Acid Pol WWII Acid Pol WWII Acid Pol PB Quinn Chou Secret Agent Chou Rider Chou Gene

我正在尝试制作一个
Character\u Picker.bat
,它从
characters.txt
一个长文本文件中回显随机选取的字符串。我不知道如何随机挑选

Characters.txt:

Disco Jockey Acid Pol
Double Agent Acid Pol
Muaythai Acid Pol
Paramedic Acid Pol
WWII Acid Pol
WWII Acid Pol
PB Quinn Chou
Secret Agent Chou
Rider Chou
General Chou
Grim Reaper Chou
Psycho Nurse Chou
Highschool Chou
Invasion Chou
Army Agent Chou
Kung Fu Chou
Rogue Agent Chou
Sweet Heart Chou
.
.
.
.333 strings more
我的批次代码:

@echo off
color a
cd Desktop
start "characters.txt"  ==> this is the thing where I stuck
set /a string=%random% %% 334
echo %string%

我想这就是你想要的

@echo off
set /a rand=%random% %% 334
for /f "tokens=1* delims=:" %%i in ('findstr /n .* "d:\characters.txt"') do (
if "%%i"=="%rand%" echo %%j
)
我们设置
rand
并将随机数限制为334。 然后我们读取文件并查找行号,如果该行号与随机字符串匹配,则打印该字符串

只是为了让您理解
delims=:
部分
findstr/n.*“d:\characters.txt”
将打印行号,然后是冒号和实际行。比如:

1:Disco Jockey Acid Pol
2:Double Agent Acid Pol
3:Muaythai Acid Pol
4:Paramedic Acid Pol
5:WWII Acid Pol
....

delims=:
使用
作为分隔符,我们将行号分配给
%%i
,将实际行文本分配给
%%j
,因此将
%%i
%rand%
匹配,然后打印
%%j

,我首先确定文本文件中的行数,然后确定该范围内的一个随机数,然后精确提取这一行,如下所示:

@echo关闭
对于(“^<”characters.txt“find/C/V”“”)中的/F%%C,请设置“COUNT=%%C”
设置/A“数字=%RANDOM%%%%%COUNT%”
如果%NUMBER%gtr 0(设置“SKIP=SKIP=%NUMBER%”),否则(设置“SKIP=”)
对于/F usebackq^%SKIP:SKIP=SKIP^%^delims^=^eol^=%L in(“characters.txt”)do(
回声(%%L
后藤:下一个
)
:下一个

如果目标行为空,则此代码将返回下一个非空行。

不工作:-(错误
C:\Users\alea\Documents>∩╗┐@回音'∩╗┐@echo'未被识别为内部或外部命令、可操作程序或批处理文件。C:\Users\alea\Documents>set/a rand=24832%334 C:\Users\alea\Documents>for/F“tokens=1*delims=:%i in('findstr/n.*.d:\characters.txt')do(如果“%i”=“116”echo%j)“findstr”不被识别为内部或外部命令、可操作程序或批处理文件。
您是按照我发布的方式复制并粘贴到批处理文件中的吗?还是将其粘贴到cmd.exe?为什么?只需按原样复制所有内容并粘贴到文件中,并将其命名为
char_picker.cmd
,然后从
cmd.exe
运行并查看结果。显然,将您的路径设置为
characters.txt
“@echo off”不起作用。它显示'∩╗┐@echo off'@JhonreyDaffon,
findstr
确实是一个外部命令,但是通常应该在不更改系统目录的情况下找到它,因为这无论如何都应该是
路径
环境变量的一部分,除非您弄乱了它。。。