Problem passing a directory with a space in it as an argument
-
nevermind i was wrong about that anyway. apparently quotes in the middle of a text block is still interpreted as one argument even if spaces exist.
-
Hi everyone. Bad news and good news... The bad news is that I've given up trying to do this with Qt because it's driving me crazy. The good news is that someone's already done this in VBScript. :)
-
Hi again, couldn't resist another attempt at this problem :-)
first: about those yen signs, it's because of the japanese codepage used by windows (where \ is replaced by the yen sign)
then: I reread your problem description, and it's not about backslashes or yen signs but spaces in directories/paths. And as you say, invoking PDF Creator from the command line. in that case when you quote the path a space works fine and dandy. But not from Qt :-(
So I thought, why not go lowtech on this problem and launch PDF Creator using a .cmd file (for 64-bit Win7 it's better to use .cmd instead of .bat files).
I tested with a cmd file (C:\Temp\StartPF.cmd) that contains two lines:
@"C:\Program Files (x86)\PDFCreator\PDFCreator.exe" /NOSTART /PF"%1 %2 %3 %4 %5 %6 %7 %8 %9
"C:\Program Files\PDFCreator\PDFCreator.exe" /NOSTART /PF"%1 %2 %3 %4 %5 %6 %7 %8 %9@(One of the two lines should work regardless if you're on 32 bit or 64 bits Win7.)
Then I modified your program again:
@ program = "cmd.exe";QStringList arguments; arguments << "/c C:\\temp\\StartPF.cmd " + r + "\\*.pub";@
Note in the .cmd file there's no trailing quote, it's because QProcess inserts quotes anyway around each separate argument. (Also note I add everything together into just one argument in the string list, to minimize the extra cooking by QProcess.)
Anyway, the low-tech idea here is to use that sequence of %1 %2 %3 etc. to bake paths together to it's full glory again even with spaces inside. As long as the # of different spaces are 8 or less :-)
-
hskoglund,
You're a star! It works perfectly. :-)
Now, can you tell me what you did there? You said you've gone low-tech there, but unfortunately, I've only ever cobbled together some simple .bat files in the past and I've never even used a .cmd file before.
Thanks a lot. :-)
-
Good! (when I saw you posted couple of seconds before me, I hoped my post would save you from the brink of insanity.)
About .bat/.cmd files, that's one advantage of being old age, they're primitive tools for sure but not unknown territory. Anyway a .cmd file is nothing more than a renamed .bat file, because sometimes .bat files don't start in 64-bit Windows, but .cmd works fine.
The low-tech? It's just that sequence %1 %2 %3 %4 %5 ... that works as a "catch-all", i.e. when QProcess launches the .cmd file, an argument with spaces inside gets split up into separate arguments, but %1 %2 %3 %4 %5 ... just pastes them together again.
-
Great work, hskoglund! :)
-
Hello again,
I'm not sure if I should start a new post here, but I have another small problem with this script.
I've posted the whole file below. The problem is with opening a directory in Explorer after the work (PDF printing) is done. I didn't have this problem before because I'm building and testing on two Windows 7 machines. Now I've come to deploy this on an XP machine, I've found that the simple code I had to do this isn't working. I tried it in a cmd window and it didn't work. I found that this works:
@start C:\temp@
...but this doesn't:
@start E:\pdfs@
With a bit more investigation, I found that this works:
@%SystemRoot%\explorer.exe /e, E:\pdfs@
... but when I put that into my Qt code, it doesn't work. Please take a look near the end of the code below and see if you can see where I might be going wrong.Thanks a lot. I appreciate any help.
@#include "mainwindow.h"
#include "ui_mainwindow.h"#include "QFileDialog"
#include "QProcess"
#include "QFile"
#include "QDebug"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setWindowTitle("Batch Publisher to PDF");
ui->setupUi(this);}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);QString r; r = dir.replace("/", "\\"); QString program; program = "cmd.exe"; QStringList arguments; arguments << "/c C:\\sb\\StartPF.cmd " + r + "\\*.pub"; QProcess *myProcess = new QProcess(qApp); myProcess->start(program,arguments); qDebug() << arguments; myProcess->waitForFinished(); QProcess *process = new QProcess(qApp); QString pdf; QString pdfA; QString pdfB; QString pdfC; pdfA = "C:\\Users\\Steve\\Documents\\PDFs\\"; pdfB = "E:\\pdfs"; // This is the one in the XP machine pdfC = "C:\\Users\\Steve\\Dropbox\\pdfs\\"; QDir dDirA(pdfA); QDir dDirB(pdfB); if (dDirA.exists()) { pdf = pdfA; } else if (dDirB.exists()) { pdf = pdfB; } else { pdf = pdfC; } if (pdf==pdfB) { // if XP process->start("%SystemRoot%\\explorer.exe /e, "+pdf); process->waitForFinished(); }
else {
process->start("explorer ""+pdf+"");
process->waitForFinished();
}
qApp->exit();}
@ -
Hi, re. that XP problem, probably process->start() does not interpret/expand that environment variable SystemRoot into C:\Windows. So when fiddling with Explorer, start etc. it's usually better to launch them via "cmd.exe /c" (as used to run StartPF.cmd).
So try changing to
@process->start("cmd.exe","/c %SystemRoot%\explorer.exe /e, " + pdf);@