Может, кому пригодится
Написано-отлажено под NetBeans
Есть веб-камеры, скидывают все картинки по фтп в одну папку.
Несколько кадров в секунду. Имена файлов камерой формируются исходя из её времени, не факт, что оно выставлено.
Процедура сортирует папку inDir в подпапки outDir по месяцам, числам и десяткам минут, исходя из времени создания файла на сервере.
outDir/Jan/18/21-00/картинка
#include <cstdlib>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
int sortDir( char *inDir, char *outDir)
{
char *bu, *bb, *inF, *outF, *buf, *timefnam ;
bu = new char[200] ; bb = new char[220] ; inF = new char[200] ; outF = new char[200] ; buf = new char[0x10000] ;
timefnam = new char[200] ;
struct stat stB, jN ;
timespec fTm ;
time_t t ;
int i, j ;
int fcount = 0 ;
timeval tv ;
FILE *fi, *fo ;
DIR *dp;
struct dirent *dent;
time_t rawtime ;
long l = time(&t) ;
dp = opendir(inDir) ; // open directory to sort
while(dent = readdir(dp))
{
if ( dent->d_type == DT_REG ) // if is file
{
sprintf(buf, "%s/%s", inDir, dent->d_name) ; // path/filename
j = stat(buf, &jN) ; // get information about file
fTm = jN.st_mtim ;
rawtime = fTm.tv_sec ;
strcpy(timefnam, ctime(&rawtime)) ; // compose time field for new filename
j = strlen(timefnam) ;
timefnam[j-1] = 0 ;
sprintf(bu,"%s/%c%c%c", outDir, timefnam[4], timefnam[5], timefnam[6]) ; // dir name for Month
if ( stat(bu, &stB) )
mkdir(bu, 0777) ; // create if does not exist
if (timefnam[8] == ' ')
timefnam[8] = '0' ;
sprintf(bb,"/%c%c", timefnam[8], timefnam[9]) ; // dir for Date
strcat(bu, bb) ; // append Date to path
if( stat(bu, &stB) )
mkdir(bu, 0777) ; // create if does not exist
sprintf(bb, "/%c%c--%c0", timefnam[11], timefnam[12], timefnam[14]) ; // Hour + 10 minutes
strcat(bu, bb) ; // append
if ( stat(bu, &stB) )
mkdir(bu, 0777) ; // create if does not exist
gettimeofday(&tv, NULL) ;
sprintf(outF, "%s/%s %02d.JPG", bu, timefnam, (int)(tv.tv_usec/10000)) ; // append seconds
sprintf(inF, "%s/%s", inDir, dent->d_name) ; // compose path + filename from readdir
i = chmod(inF, 0777) ;
j = rename(inF, outF) ;
if ( !j )
fcount++ ;
else
return (-fcount) ;
chmod(outF, 0777) ;
}
}
closedir(dp);
// j = remove(inDir) ;
delete [] bu, delete [] bb, delete [] inF, delete [] outF, delete [] buf, delete [] timefnam ;
return (fcount) ;
}