0

I am trying to copy multiple files to a destination folder in Qt 4.8 using QProcess named copyProcess. The thing is when I use this command, nothing happens, in cmd, it shows invalid syntax. The value which I get from the StringList is this

for %I in (", "C:/Users/xyz.mp4","C:/Users/V46.srt","C:/Users/FP.txt",")","do copy %I", "C:\Users\Nina\Desktop\New folder (9)")

        d->copyProcess =  new QProcess(this) ;
        QStringList copyProcessParameters;
       // copyProcessParameters.append("-rf");
        Q_FOREACH(QString fileName, fileNames)
        {
            d->totalFileSize += this->getSize(fileName);
            d->filesToCopy.append(fileName);
        }
        copyProcessParameters.append(d->filesToCopy);
        copyProcessParameters.append("do copy %I");
        copyProcessParameters.append(destinationDir);
        d->copyProcess->start("for %I in", copyProcessParameters);

So is there a way I can use the batch script in qt that way that it can copy multiple files at once ?

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Vikrant singh
  • 433
  • 1
  • 7
  • 25
  • 1
    I am not familiar with QT but I bet it has a way to copy files itself without asking the shell to do it ... – Alex K. Aug 08 '17 at 12:27
  • @nina cheek Why Qprocess ? you can use QFile. easy to use. – Farhad Aug 08 '17 at 12:27
  • @AlexK. Of course it have that https://stackoverflow.com/questions/19928216/qt-copy-a-file-from-one-directory-to-another – litelite Aug 08 '17 at 12:27
  • @ninacheek Why would you need it to be asynchronous? And if you really need it to be you could start a thread – litelite Aug 08 '17 at 12:28
  • the whole other UI hangs if I try to copy, anyway, yeah I can use threads, but I wanted to use the shell, as the memory leak was low and load was less. – Vikrant singh Aug 08 '17 at 12:30
  • 3
    If you do it correctly, you should have 0 memory leak. – litelite Aug 08 '17 at 12:31
  • @ninacheek I agree with litelite you can handle it properly. – Farhad Aug 08 '17 at 12:32
  • okay , thanks , but now whole of my work is related to QProcess and its exit code , so moving to threads would take a lot of revamping. – Vikrant singh Aug 08 '17 at 12:32
  • @ninacheek it would be better practice to use thread rather than the shell (because the shell can be dangerous to use sometime). As a bonus, with the thread you could monitor the progress so you could add a cancel button an a progress bar. – litelite Aug 08 '17 at 12:33
  • That's a very silly way! An alternative create a batch script and call it just once! – Elvis Dukaj Aug 08 '17 at 12:33
  • Also, if you really want to do it that way. your syntax error seems to be caused by some misbalanced _"_ – litelite Aug 08 '17 at 12:37

1 Answers1

0

Using QT might give you more flexibility. But if you wanted concurrent copies, you could use START. Of course, change the destination of NUL to your directory.

for %I in ("C:\Users\xyz.mp4" "C:\Users\V46.srt" "C:\Users\FP.txt") do (start /MIN cmd.exe /C COPY "%~I" NUL)

Also, if this goes into a .bat script, double the % characters on the variable.

lit
  • 14,456
  • 10
  • 65
  • 119