1

I am trying to securely clear out a directory using SDelete. I know that this is used from the Command line, but how I would I go about automatically clearing the directory from my C++ code, also using Qt if this has a built any built in functions. I could not find anything with searching and this is my first time doing something like this. Any help would be greatly appreciated, thanks.

jbz6587
  • 45
  • 1
  • 6
  • Do you want to invoke the SDelete command from your application? Or do you want to do the same thing that SDelete does? Your question isn't clear. – Darrin Cullop Apr 30 '19 at 20:17
  • @DarrinCullop I do want to invoke the SDelete command from the application, apologies. – jbz6587 Apr 30 '19 at 20:32

1 Answers1

1

It is good that you're not trying to re-create the functionality of SDelete. It would be a LOT of work to do as good as a job as what SDelete does. Invoking the existing application is a wise choice.

Now, on to your question... If you want to use QT, then what you need is something like this:

QString path = QString("sdelete", QStringList() << "Bogus.txt");
QProcess sdelete;

sdelete.start( path );
sdelete.waitForFinished();

That will start the process sdelete with the parameter Bogus.txt and then wait until the application is finished.


More Info: https://doc.qt.io/archives/qt-4.8/qprocess.html#start

Edit from OP : I found that using the following worked for me with the argument being passed in being a QString.

QProcess::execute("sdelete -s path");
jbz6587
  • 45
  • 1
  • 6
Darrin Cullop
  • 1,170
  • 8
  • 14
  • 1
    Don't build strings to launch programs - you get immediately into the mess of handling spaces and escapes in program arguments. Use [the correct overload of `QProcess::start` taking a list of arguments](https://doc.qt.io/qt-5/qprocess.html#start). – Matteo Italia May 01 '19 at 00:03
  • 1
    If you think my answer could be improved, you should edit it and make it better, or submit your own. Downvoting an answer that will work fine is rude. – Darrin Cullop May 01 '19 at 00:09
  • 1
    I updated my answer according to @MatteoItalia's suggestion about providing parameters. The documents say they're equivalent, but I can see it getting messy with a lot of parameters. – Darrin Cullop May 01 '19 at 00:12
  • 1
    I'm downvoting an answer that *doesn't work* or risks doing a mess in edge cases, namely when the file name contains a space. Sorry for your sensibility, but there's such a thing as being wrong, which is especially bad when we are talking about deleting files. There's more than enough string interpolation junk on the internet that breaks because it either doesn't escape stuff correctly, or uses the wrong tool for the job (string interpolation instead of forwarding arguments as a list). – Matteo Italia May 01 '19 at 00:16
  • 1
    This is now even wronger, and doesn't make sense even just on the C++ level. Read the docs and actually test your code. – Matteo Italia May 01 '19 at 00:19
  • 1
    @jbz The answer is lifted from the documentation and will work just fine. – Darrin Cullop May 01 '19 at 00:25
  • 1
    There's no such a thing as a `QString` constructor taking a string literal and a `QStringList`. This won't even compile (and even if it did, it would have the same problems as the other). You have to call the other overload of `QProcess::start` giving it the arguments as a list, so that they can be escaped correctly when building the command line for the new process (on Win32) or straight forwarded as a list (on POSIX). You put an admirable effort in actively misunderstanding what I'm trying to explain. – Matteo Italia May 01 '19 at 12:01
  • 1
    Thank you for your responses, both were very helpful. I did not use a list, but a QString of the command with the path together. I had trouble with the QProcess.start() so I ended up using the QProcess::execute(). Thanks again! – jbz6587 May 02 '19 at 17:38
  • 1
    As long as you got it working, that's the only thing that matters. :) – Darrin Cullop May 02 '19 at 18:21