Reputation: 11
I'm trying to create a batch-file which would select a random .txt file (from over 1500 files) from within 1 folder and display it's content in CMD in Windows.
Got to this but unable to display text in the cmd
as the file opens in text editor. The last line should be More
not Call Start
.
Set "SrcDir=\RECON\txt"
Set "ExtLst=*.txt"
Set "i=0"
For /F "Delims=" %%A In ('Where /R "%SrcDir%" %ExtLst%') Do (Set /A i+=1
Call Set "$[%%i%%]=%%A")
Set /A #=(%Random%%%i)+1
Call Start "" "%%$[%#%]%%"
Help, anyone?
Upvotes: 0
Views: 193
Reputation: 41
okay so this is a realy bad script that I made that uses text files to store info and powershell combinations. so pretty much it's bad and you shouldn't ever do anything like I just did. but it does get the job done.
@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
Upvotes: 0
Reputation:
Just make life easier for yourself and use delayedexpansion
it makes things more readable.
@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%]!"
Note also that with the fact you are using where
you need to ensure that your SrcDir
variable never contains a trailing \
or you will get an error, because of using double quotes in the for
loop.
Obviously, you can replace type
with more
if it suits your needs better.
Upvotes: 1