Reputation: 443
I have a QStringList
named filesToCopy
which contains the files name to copy.
I want to make this output:
for %I in ("(", "C:\Users\Nina\Documents\A.mp4", "C:\Users\Nina\Documents\A.srt", "C:\Users\Nina\Documents\A.txt", ")", "do copy %I", "C:\Users\Nins\Desktop\z")
to look like this:
for %I in ("C:\Users\Nina\Documents\A.mp4" "C:\Users\Nina\Documents\A.srt" "C:\Users\Nina\Documents\A.txt") do copy %I "C:\Users\Nina\Desktop\z"
This is my code:
d->copyProcess = new QProcess(this);
QStringList copyProcessParameters;
Q_FOREACH(QString fileName, fileNames)
{
d->totalFileSize += this->getSize(fileName);
d->filesToCopy.append(fileName);
}
d->filesToCopy.append(")");
d->filesToCopy.prepend("(");
copyProcessParameters.append(d->filesToCopy);
copyProcessParameters.append("do copy %I");
copyProcessParameters.append(destinationDir);
copyProcessParameters.replaceInStrings("/","\\");
qDebug()<<"for %I in" << copyProcessParameters;
d->copyProcess->start("for %I in", copyProcessParameters);
Upvotes: 1
Views: 815
Reputation: 1965
As @Azeem mentioned, you can use join()
method:
auto l_files = fileNames.join(", ");
Also note that macro Q_FOREACH
is currently (since Qt 5.7) discouraged and will be removed in a future version of Qt. You should use range-based loop from C++ standard:
for(auto& fileName : fileNames)
{
doSomething(fileName);
}
If you want to iterate over const references you can use qAsConst()
.
Upvotes: 0
Reputation: 14587
Use QStringList::join() to create the list separated by a space.
And, to make things simple, you can use QString::arg() or its overloads to create the desired string with replacements. That would be more straightforward to use and readable instead of a lot of prepend()
and/or append()
calls.
Here's an example:
const QString format { R"(for %I in (%1) do copy %I %2)" };
const auto command = format.arg( filesList, destinationDir );
Upvotes: 1