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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Batch file 在CMD中显示随机选择的.TXT文件内容_Batch File_Cmd - Fatal编程技术网

Batch file 在CMD中显示随机选择的.TXT文件内容

Batch file 在CMD中显示随机选择的.TXT文件内容,batch-file,cmd,Batch File,Cmd,我正在尝试创建一个批处理文件,该文件将从1个文件夹中的1500多个文件中选择一个随机的.txt文件,并在Windows的CMD中显示其内容 已达到此目的,但无法在文本编辑器中打开文件时在cmd中显示文本。最后一行应该更多,而不是呼叫开始 有人来帮忙吗?只要让自己的生活更轻松,使用delayedexpansion,事情就更易懂了 @echo off setlocal enabledelayedexpansion Set "SrcDir=\RECON\txt" Set "

我正在尝试创建一个批处理文件,该文件将从1个文件夹中的1500多个文件中选择一个随机的.txt文件,并在Windows的CMD中显示其内容

已达到此目的,但无法在文本编辑器中打开文件时在cmd中显示文本。最后一行应该更多,而不是呼叫开始


有人来帮忙吗?

只要让自己的生活更轻松,使用delayedexpansion,事情就更易懂了

@echo off
setlocal enabledelayedexpansion
Set "SrcDir=\RECON\txt"
Set "ExtLst=*.txt"
Set "i=0"
For /F "Delims=" %%A In ('Where /R "%SrcDir%" *%ExtLst%') Do (
   Set /A i+=1
)
Set /A rnd=!Random!%%%i%
type "!$[%rnd%]!"
还请注意,由于您使用的是where,因此需要确保SrcDir变量从不包含尾随\或者由于在for循环中使用双引号,因此将出现错误


显然,如果类型更好地满足您的需要,您可以使用更多来替换它。

好吧,这是一个非常糟糕的脚本,我使用文本文件来存储信息和powershell组合。所以很糟糕,你不应该做我刚才做的事。但它确实完成了任务

@echo off
set loc2=%CD%

:: replace the line below with "set location=[folder with the files]" if you want to use the same folder every time

set /p location=folder:
cd %location%
dir /b>"%loc2%\dir.txt"
cd %loc2%
find /c "." "dir.txt">"find.txt"
powershell -Command "(gc '%loc2%\find.txt') -replace '---------- DIR.TXT: ', '' | Out-File -encoding ASCII 'replace.txt'"
cd %location%
PowerShell "GC '%loc2%\replace.txt' -TotalCount '2'|Select -L 1">"%loc2%\temp_txt2.txt"
< "%loc2%\temp_txt2.txt" (
  set /p file_count=
)
del "%loc2%\replace.txt"&del "%loc2%\find.txt"
SET /A ran=%RANDOM% * %file_count% / 32768 + 1
PowerShell "GC '%loc2%\dir.txt' -TotalCount '%ran%'|Select -L 1">"%loc2%\temp_txt.txt"
< "%loc2%\temp_txt.txt" (
  set /p file_name=
)
type "%file_name%"
del "%loc2%\temp_txt.txt"&del "%loc2%\dir.txt"&del "%loc2%\temp_txt2.txt

:: put whatever other scripts you want after this
pause
@echo off
set loc2=%CD%

:: replace the line below with "set location=[folder with the files]" if you want to use the same folder every time

set /p location=folder:
cd %location%
dir /b>"%loc2%\dir.txt"
cd %loc2%
find /c "." "dir.txt">"find.txt"
powershell -Command "(gc '%loc2%\find.txt') -replace '---------- DIR.TXT: ', '' | Out-File -encoding ASCII 'replace.txt'"
cd %location%
PowerShell "GC '%loc2%\replace.txt' -TotalCount '2'|Select -L 1">"%loc2%\temp_txt2.txt"
< "%loc2%\temp_txt2.txt" (
  set /p file_count=
)
del "%loc2%\replace.txt"&del "%loc2%\find.txt"
SET /A ran=%RANDOM% * %file_count% / 32768 + 1
PowerShell "GC '%loc2%\dir.txt' -TotalCount '%ran%'|Select -L 1">"%loc2%\temp_txt.txt"
< "%loc2%\temp_txt.txt" (
  set /p file_name=
)
type "%file_name%"
del "%loc2%\temp_txt.txt"&del "%loc2%\dir.txt"&del "%loc2%\temp_txt2.txt

:: put whatever other scripts you want after this
pause