1.

Solve : Load Random File from Folder location Question?

Answer»

Was wondering if there was an easier way vs naming files 1, 2, 3, 4, 5, 6, 7 .... to launch an executable with random file appended to it within " " to when running an EXE have it pick from any number of many files at random. I right now can do this all in C++ with file names that are numbers. With C++ I can concatenate the random number output generated to .mp3 for example and then perform a system call to the player.exe "4.mp3" for example to play the 4th file NAMED 4.mp3. But I'd like the files to all keep their original file names without having to rename all files to numbers.mp3 since sometimes I want to manually go in and launch a file to listen to and navigating in a folder with numbers without a list of what is what it is all a mess of unknown.

I was wondering rethinking all of this if this could be done in Batch maybe where DIR can be used to append file name / extension to a text file and then at random read in one of the files listed in the DIR output, calling to say line #8.  So it launches whatever file name is the 8th down on the list. *Only the music files for example will be located in this folder and so there is no risk of calling to a non supported file type by the player.

iTunes isnt a solution for a random shuffle of music because this will be for more than just MP3 files, but also for video file formats when I want to watch something at random from say folders that are CATEGORIZED so say i want to watch an action movie but cant make up my mind, I run this and it picks from the list at  random and I watch it, or if i am not in the mood for whatever movie, I can run it again and it picks the next movie at random.Finally found what I think might be the solution... found this under search words of "opening a random file from windows", even though its really a batch script method ... NT based batch.

https://superuser.com/questions/872048/opening-random-file-from-folder-and-subfolders-with-batch-script-windows-7Elementary, surely? Capture the filenames, put them in an array (or a batch equivalent), generate a random number between array lower limit and array upper limit, use that as array index, read out the file name.


Yes that was my FIRST thought. Choosing one item out of a set like this is fairly straightforward, launching a file with the associated program is fairly easy as well for the most part.

Code: [Select]String sMask = "*.txt";
String sFolder = "D:\\";
var AllFiles = new DirectoryInfo(sFolder).GetFiles(sMask);
Process.Start(AllFiles[new Random().Next(AllFiles.Length)].FullName);

Also for funzies, a C++ version. Windows only. (my use of rand() results in non-uniform distributions) Hard-coded directory and file mask (D:\ and *.txt files).

Code: [Select]#include "stdafx.h"
#include <vector>
#include <string>
#include <algorithm>
#include <Windows.h>
#include <random>
#include <time.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

WIN32_FIND_DATA finddata;

vector<wstring> files;
ZeroMemory(&finddata, sizeof(finddata));
HANDLE hfind = FindFirstFile(L"D:\\*.txt", &finddata);

while (NULL != hfind)
{
wstring smember(finddata.cFileName);
files.push_back(smember);
BOOL result = FindNextFile(hfind, &finddata);
if (!result)
{
FindClose(hfind);
hfind = NULL;
}
}

srand(time(NULL));
int chosen = rand() % files.size();
SHELLEXECUTEINFO inf;
ZeroMemory(&inf, sizeof(inf));
inf.cbSize = sizeof(inf);
inf.lpDirectory = L"D:\\";
inf.lpFile = (files[chosen].c_str());
inf.lpVerb = L"Open";
ShellExecuteEx(&inf);
}
Cool.... Thanks BC for showing how to do this in C++ without use of system(); calls.   It's interesting how much longer the C++ version is compared to the C# version (which admittedly WOULD need a few more lines to be a full program).

I tried to convert it to a powershell version but something came up so I just posted what I had at the time.



Discussion

No Comment Found