0

I am trying to pipe the commands and execute it, but I am not able to figure how to pipe it. I am trying to copy multiple files at once using the shell command

for %I in (source) do copy %I (destination)

QString files = "for %I in (source) do copy %I (destination)"
QProcess copy ;
copy.start(files);

I have to implement the piping to do that. for Eg.

QProcess sh;
sh.start("sh", QStringList() << "-c" << "ifconfig | grep inet");

sh.waitForFinished();
QByteArray output = sh.readAll();
sh.close();

How can I implement piping for my copy process?

Vikrant singh
  • 433
  • 1
  • 7
  • 25
  • Possible duplicate of [Piping (or command chaining) with QProcess](https://stackoverflow.com/questions/20901884/piping-or-command-chaining-with-qprocess) – Azeem Aug 11 '17 at 10:01
  • @Azeem those answers are not helping me . – Vikrant singh Aug 11 '17 at 10:03
  • Errors? Issues? – Azeem Aug 11 '17 at 10:03
  • QProcess shows no status , it just not copying . i guess i am not doing the chaining properly. – Vikrant singh Aug 11 '17 at 10:11
  • Why use an external process to copy the files rather than simply use [`QFile::copy`](http://doc.qt.io/qt-5/qfile.html#copy-1)? Just create another thread on which to invoke `QFile::copy` and use signals/slots to notify of any errors or state changes etc. – G.M. Aug 11 '17 at 11:20
  • @G.M. I have used that, even that also makes some lagging issues in my application. using shell script doesn't create any hassles. – Vikrant singh Aug 11 '17 at 13:38

1 Answers1

1

Try this example:

QProcess sh;
sh.start( "sh", { "-c", "ifconfig | grep inet" } );

if ( !sh.waitForFinished( -1 ) )
{
    qDebug() << "Error:" << sh.readAllStandardError();
    return -1;
}

const auto output = sh.readAllStandardOutput();
// ...

waitForFinished() should be called in blocking mode and it must be checked if it was successful or not.

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • i tried d->copyProcess->start( "sh" , QStringList() << "-c" << files); if(!d->copyProcess->waitForFinished(-1)) { qDebug()<< "Error is there" << d->copyProcess->readAllStandardError(); } console = Error is there "" – Vikrant singh Aug 11 '17 at 10:46
  • @ninacheek: So, the command doesn't return anything on `stderr` but there was an error. Did you try to run the command without pipe? Does that run successfully? – Azeem Aug 11 '17 at 11:54
  • @ninacheek: What is `files`? What are its values? – Azeem Aug 11 '17 at 12:05
  • files is a string which contains the list of files to be copied and the folder where it has to copy – Vikrant singh Aug 11 '17 at 13:39
  • @ninacheek: Can you try without pipe e.g. `start( "sh", { "-c", "ls" } );`? – Azeem Aug 11 '17 at 16:20