0

is it possible to make a batch file choose a totally random text file from within a specified folder

and can somebody help me make it?

example lets say that i got a folder located here: C:\Workstation\Jobs and in that folder there is maybe 10-20 .txt files at a time

the goal here is to make that batch file check the folder and then choose a random file and output the choosen file to a variable

2 Answers2

2

In the case of the said folder only containing text files, you'd need the following:

  • a list of folder contents in some sort of array (dir)
  • count the amount of files (for loop)
  • pick a random number between 0 and the amount of files (%RANDOM%*amount_of_files/32768)
  • output the matching filename (in the array) to the variable you want

Hope this pushes you in the right direction :-)

sources:
http://ss64.com/nt/syntax-random.html
How to loop through files matching wildcard in batch file
Create list or arrays in Windows Batch

Community
  • 1
  • 1
YVbakker
  • 130
  • 9
2
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f %%a IN (
 'dir /b /a-d "%sourcedir%\*.txt"^|find /i /c ".txt" '
 ) DO SET /a selection=1 + (%RANDOM% %% %%a)
FOR /f "tokens=1*delims=:" %%a IN (
 'dir /b /a-d "%sourcedir%\*.txt"^|findstr /n /i ".txt" '
 ) DO IF %%a==%selection% SET filename=%%b
ECHO selected %filename%

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

find /c counts the number of files found by the dir command and the for assigns the count to %%a. selection is then assigned to 1+(randomnumber mod filecount) giving 1..filecount.

findstr /n outputs the name of each file found by the dir command, prefixed by a sequential number : %%a will be assigned the number (token before the : and %%b the filename (afther the first delimiter where delims is :)

When the line number matches selection, filename is assigned the filename found.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • there is only one problem with your code. it doesnt seem to handle files with spaces that well, by that i mean it complete refuses to run – NickyGamePlays Sep 17 '16 at 23:11
  • Runs perfectly for me, even with spaces in the filenames or with spaces in the directoryname. Please post a small sample of the filenames you are using and are giving trouble. – Magoo Sep 17 '16 at 23:18
  • the file names are actually usernames for the pc which made the work so: Private User Account.txt, Guest User Account.txt, Dave's Account.txt, Lennys Account.txt – NickyGamePlays Sep 17 '16 at 23:22
  • Works perfectly for me with those filenames. Try changing `DO SET /a selection...` to `DO echo%%a&SET /a selection...` which should report the number of .txt files found to the screen. If it reports 0, then there's something wrong with the `dir` or `find` statement. Try copying the `dir` statement (just the `dir` part - up to but not including the `^`) onto a line before the first `for`. This should list the filenames. I hope you haven't tried to retype the code - you should copy-and-paste. A single character out of place can cause strange results. – Magoo Sep 17 '16 at 23:40
  • it lists the files no proplem – NickyGamePlays Sep 17 '16 at 23:45
  • ...and what filecount did it display? – Magoo Sep 17 '16 at 23:47
  • the echo displayed 17 even though it was hard to notice becouse it closed instantly – NickyGamePlays Sep 17 '16 at 23:50
  • Add a pause statement directly before the `goto :eof`. This will hold the window open until you press `enter`. If you place a line `echo on` before the second `for/f` then you should get a trace of exactly the commands being executed. Since I can't see what you are *actually* doing with the value in `filename` I'd suggest you try surrounding `%filename%` with quotes thus: `"%filename%"` - quoting filenames ensures the string between the quotes is interpreted as a single unit. – Magoo Sep 18 '16 at 00:11
  • idk what the problem was but i closed notepad++ and started it again and now it runs.. :/ – NickyGamePlays Sep 18 '16 at 00:20