My electronic portrait frame displays pictures in a slide show. The pictures (.jpg files) are displayed in the order they were written to the usb drive (memory stick). I need a C++ program to reorder files from one directory to another in a random fashion. Then I can create display the pictures randomly on my electronic portrait frame after transfering the files from the newly re-ordered directory.
I need a C++ program to re-order files randomly?
Try this on ANY OS:
// \:^)
#include %26lt;cstdio%26gt;
#include %26lt;cstdlib%26gt;
#ifdef WIN32
#include %26lt;windows.h%26gt;
#else
#include %26lt;dlfcn.h%26gt; // for dlopen(), dlclose(), dlsym() ...
#include %26lt;dirent.h%26gt; // for C++ don't use version in /usr/include/sys
#include %26lt;sys/stat.h%26gt; // for stat structure def'n
#include %26lt;unistd.h%26gt; // for chmod
#include %26lt;errno.h%26gt; // for error processing
#endif
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
#include %26lt;vector%26gt;
#include %26lt;algorithm%26gt;
using namespace std;
typedef vector%26lt;string%26gt; FileList;
FileList filelist;
void CopyFiles(string from_dir, string to_dir)
{
FileList::iterator iter = filelist.begin();
FileList::iterator stop = filelist.end();
// Create destination directory if needed
#ifdef WIN32
if (GetFileAttributes(to_dir.c_str()) == ((DWORD) -1))
{
string cmd("mkdir ");
cmd.append(to_dir);
system(cmd.c_str());
}
#else
struct stat buffer;
if (stat(to_dir.c_str(), %26amp;buffer) == -1)
{
string cmd("mkdir ");
cmd.append(to_dir);
system(cmd.c_str());
}
#endif
while(iter != stop)
{
string cmd("cp ");
cmd += from_dir;
cmd += "/";
cmd += *iter;
cmd += " ";
cmd += to_dir;
system(cmd.c_str());
iter++;
}
}
#ifdef WIN32
// Find all candidate files in directory
void FindFiles(const string %26amp;dir)
{
// VISUAL C++ 2005 (V8) Note:
// Be certain to refuse inheritance of UNICODE %26amp; _UNICODE prerocessor
// definitions (in Project Properties-%26gt;C/C++-%26gt;Preprocessor Definitions;
// otherwise Microsoft LPCSTR and other alhpbet soup types will
// want to mix and match Wide (Unicode) and ANSI (ASCII) characters
BOOL done;
HANDLE list_handle;
TCHAR dir_name[MAX_PATH+1];
WIN32_FIND_DATA file_data;
// Get the proper directory path
sprintf(dir_name, "%s\\*", dir.c_str());
// Get the first file listed in the directory
list_handle = FindFirstFile((LPCSTR)dir_name, %26amp;file_data);
// Check for empty or invalid directory
if (list_handle == INVALID_HANDLE_VALUE)
{
printf("No files found in directory\n");
}
else
{
// Traverse through the directory examine each item
done = FALSE;
while(!done)
{
// Check that the listed object is not a directory
if (!(file_data.dwFileAttributes %26amp; FILE_ATTRIBUTE_DIRECTORY))
{
string filename((char *)file_data.cFileName);
filelist.push_back(filename);
}
// Get next item listed in the directory
if (!FindNextFile(list_handle, %26amp;file_data))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
done = TRUE;
}
}
}
}
FindClose(list_handle);
}
#else
// Find all candidate files in directory
void FindFiles(const string %26amp;dir)
{
DIR *dp = 0;
struct dirent *dir_entry = 0;
struct stat stat_info;
// Open the plugin directory
if ((dp = opendir(dir.c_str())) == 0)
{
fprintf(stderr,"cannot open directory: %s\n", dir.c_str());
return;
}
chdir(dir.c_str());
string dirname(dir);
dirname += "/";
// Read each item in the directory and check for files
while((dir_entry = readdir(dp)) != 0)
{
lstat(dir_entry-%26gt;d_name, %26amp;stat_info);
// Check that the listed object is not a directory
if (! S_ISDIR(stat_info.st_mode))
{
string filename(dir_entry-%26gt;d_name);
filelist.push_back(filename);
}
}
chdir("..");
closedir(dp);
}
#endif
int main(int argc, char *argv[])
{
if (argc != 3)
{
cout %26lt;%26lt; "Usage: " %26lt;%26lt; argv[0] %26lt;%26lt; " %26lt;existing folder%26gt; %26lt;new folder%26gt;\n";
}
else
{
FindFiles(argv[1]);
random_shuffle(filelist.begin(), filelist.end());
CopyFiles(argv[1], argv[2]);
}
return 0;
}
Reply:I am an embedded C++ programmer but I still do some PC apps. I am kind of new with Visual C++. Mostly I write console apps to drive external devices, via USB.
There is a device called fso. It can create directories, copy files, and so on. It is usable in C++ as well as VB script, and other platforms. I don't know the details, on how to use it in Visual C++. The link below should help you.
Good luck
flower beds
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment