I have the following code to copy files to subfolders. But always the first 20 pictures are copied instead of 1-20 into the first folder, 21-40 to the second folder and 41-60 to the third folder:
QDir dir(ui->lineEdit->text());
QList<QString> fileNameList;
QFileInfoList files = dir.entryInfoList();
foreach(const QFileInfo &fi, files) {
if(!fi.isDir()) {
if (fi.fileName().endsWith(".JPG")) {
fileNameList.append(fi.fileName());
}
}
}
int parts = (int) (fileNameList.size()) / ui->spinBox->value();
qDebug() << "parts=" << parts;
for (int i = 0; i < parts; i++) {
QDir().mkdir(ui->lineEdit->text() + QString("/part%1").arg(i + 1));
for (int l = 0; l < ui->spinBox->value(); l++) {
QFile::copy(ui->lineEdit->text() + "/" + fileNameList.at(l), ui->lineEdit->text() + QString("/part%1").arg(i + 1) + "/" + fileNameList.at(l));
}
}
The problem is with the indexing of fileNameList
. You are always indexing it from 0
to l
Which causes to always copy the first files. It should be like :
int index = i*ui->spinBox->value() + l;
QFile::copy(ui->lineEdit->text() + "/" + fileNameList.at(index), ui->lineEdit->text() + QString("/part%1").arg(i + 1) + "/" + fileNameList.at(index));