Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Load Random File from Folder location Question  (Read 5000 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Load Random File from Folder location Question
« on: March 25, 2017, 08:45:26 AM »
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.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Load Random File from Folder location Question
« Reply #1 on: March 25, 2017, 01:43:33 PM »
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-7

Salmon Trout

  • Guest
Re: Load Random File from Folder location Question
« Reply #2 on: March 25, 2017, 03:30:57 PM »
Elementary, 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.



BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Load Random File from Folder location Question
« Reply #3 on: March 25, 2017, 04:22:26 PM »
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);
}
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Load Random File from Folder location Question
« Reply #4 on: March 26, 2017, 08:29:39 PM »
Cool.... Thanks BC for showing how to do this in C++ without use of system(); calls.   8)

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Load Random File from Folder location Question
« Reply #5 on: March 26, 2017, 09:26:38 PM »
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.
I was trying to dereference Null Pointers before it was cool.