Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem passing a directory with a space in it as an argument
QtWS25 Last Chance

Problem passing a directory with a space in it as an argument

Scheduled Pinned Locked Moved General and Desktop
33 Posts 5 Posters 17.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BonRouge
    wrote on 12 Apr 2014, 05:58 last edited by
    #1

    I was making a simple little tool as a front-end to PDF Creator to batch print a load of Publisher files...

    I know the code is not very good, in the sense that I'm hard-coding some file paths in there, but this is just to work on three computers in my office, so that doesn't matter. I just wanted to make this small job (batch printing Publisher -> PDF) easier for a couple of people in my office.

    You choose a directory and then the PDFs are made. After that, the directory where PDF Creator auto-saves files opens and the application closes. However, when the directory name has a space in it (and possibly also when there's a minus sign in it (I think)), the files are not processed. I've tried to escape the space somehow, but I haven't managed to do it.

    Here's my 'mainwindow.cpp' file.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include "QFileDialog"
    #include "QProcess"
    #include "QFile"

    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;
    QString pA;
    QString pB;
    
    pA = "C:\\Program Files (x86)\\PDFCreator\\PDFCreator.exe";
    pB = "C:\\Program Files\\PDFCreator\\PDFCreator.exe";
    
    QFile dFile(pB);
    
    if (dFile.exists()) {
        program = pB;
    }
    else {
        program = pA;
    }
    QStringList arguments;
    arguments << "/NOSTART" << "/PF"+r+"\\*.pub";
    QProcess *myProcess = new QProcess(qApp);
    myProcess->start(program, arguments);
    myProcess->waitForFinished();
    
    
    QProcess *process = new QProcess(qApp);
    
    QString pdf;
    QString pdfA;
    QString pdfB;
    
    pdfA = "C:\\Users\\Steve\\Documents\\PDFs\\";
    pdfB = "E:\\pdfs\\";
    
    QDir dDir(pdfA);
    if (dDir.exists()) {
        pdf = pdfA;
    }
    else {
        pdf = pdfB;
    }
    
    process->start("explorer \""+pdf+"");
    process->waitForFinished();
    
    qApp->exit();
    

    }
    @

    I'm using Qt 4.7. I tried this with a new installation of Qt 5, but I had a nightmare with deployment which I didn't have on my old computer with Qt 4.7 installed.

    Thanks if anyone can see where I'm going wrong here.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 12 Apr 2014, 06:30 last edited by
      #2

      Arguments with spaces need to be quoted when passed to the shell. Either do that or, preferably, use the version of QProcess::start() that takes the argument list as a QStringList.

      In line 77 you put a literal double quote in your string before the file name, but do not put one after the file name.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BonRouge
        wrote on 12 Apr 2014, 08:01 last edited by
        #3

        Ah! That might be it - what you said about line 77. I'll check that later. Thanks a lot. I'll let you know how it goes.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BonRouge
          wrote on 13 Apr 2014, 00:24 last edited by
          #4

          Hi. I realised that that wasn't the problem (about line 77). That's the bit that opens the PDF directory, and that's been working fine (although I did need to fix that too.) The problem seems to be in the arguments being passed to PDFCreator. When I do this straight from the command line, it works fine...

          Thanks for any more help.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 13 Apr 2014, 05:37 last edited by
            #5

            Line 54 probably should read:
            @
            arguments << "/NOSTART" << "/PF" << r+"\*.pub";
            @

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BonRouge
              wrote on 13 Apr 2014, 16:33 last edited by
              #6

              Chris,

              Unfortunately, that didn't do it - in fact, that broke it. Before, in a directory without a space in it, it worked OK, but now - with that line - it doesn't.

              Thanks for the input and any further ideas.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BonRouge
                wrote on 14 Apr 2014, 00:45 last edited by
                #7

                Hello again.

                I'm getting really frustrated. Any help would be great. The command line for batch printing PDFs normally looks something like this for me:

                @PDFCreator.exe /PF"C:\Users\Steve\Documents\arf*.pub"@

                This works fine. If I have a space in that, it still works fine....

                @PDFCreator.exe /PF"C:\Users\Steve\Documents\arf 2*.pub"@

                The PDF Creator page about the command line options stresses that there should be no space between "/PF" and the file path, so I'm passing that as one argument in my code up there. (And, as mentioned above, sending "/PF" and the file as separate arguments doesn't work.)

                In my Qt thing, when I try to escape the quotes so that I can keep that whitespace in the file name or path, PDF Creator gives me this when there's no space:

                bq. The file can not be found!
                "C:\Users\Steve\Documents\arf*.pub"

                When there is a space, it gives me nothing - it just doesn't work and my Qt thing goes to the next step.

                So... any idea what I can do?

                Thanks again.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 14 Apr 2014, 02:00 last edited by
                  #8

                  Hi,

                  QProcess has a strange requirement regarding quotation marks. Its "documentation":http://qt-project.org/doc/qt-5/qprocess.html#start-3 says, "quotes need to be both escaped and quoted" (see the documentation for an example).

                  This is unintuitive behaviour, and the Qt devs are planning to change it.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BonRouge
                    wrote on 14 Apr 2014, 04:33 last edited by
                    #9

                    Thanks for that.

                    Now, I have this:

                    @ QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                    "/home",
                    QFileDialog::ShowDirsOnly
                    | QFileDialog::DontResolveSymlinks);
                    QString r;
                    r = dir.replace("/", "\");

                    QStringList arguments;
                    arguments << "/NOSTART" << "/PF""""+r+"\*.pub"""";
                    QProcess *myProcess = new QProcess(qApp);
                    myProcess->start(program, arguments);

                    @

                    ... and PDF Creator gives me this:

                    bq. The file can not be found!
                    ""\“C:\Users\Steve\Documents\arf*.pub\”""

                    Any more help would be greatly appreciated. Alternatively, do you know another simple way to do this? All I want to do is select a directory and pass that directory as part of an argument to PDF Creator. That seems simple, and I thought this 'project' would be easy to do. If there's another simple way to do this, I'd love to hear it.

                    Thanks.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 14 Apr 2014, 05:32 last edited by
                      #10

                      I'm not able to test at the moment, but what do you get if you write:

                      @
                      myProcess->start("C:\Program Files\PDFCreator\PDFCreator.exe /NOSTART /PF"""C:\Users\Steve\Documents\arf*.pub"""");
                      @

                      As a last resort, you could also try...
                      @
                      system("C:\Program Files\PDFCreator\PDFCreator.exe /NOSTART /PF"C:\Users\Steve\Documents\arf*.pub"");
                      @
                      ...but the general consensus is to avoid system() if at all possible because it can be a security risk. system() might also upset your antivirus.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        BonRouge
                        wrote on 14 Apr 2014, 14:03 last edited by
                        #11

                        JKSH,

                        With this:
                        @ myProcess->start("C:\Program Files (x86)\PDFCreator\PDFCreator.exe /NOSTART /PF"""C:\Users\Steve\Documents\arf\*.pub"""");@

                        I get nothing.

                        With this:
                        @system("C:\Program Files (x86)\PDFCreator\PDFCreator.exe /NOSTART /PF"C:\Users\Steve\Documents\arf\*.pub"");@

                        I get this:

                        @'C:\Program' は、内部コマンドまたは外部コマンド、
                        操作可能なプログラムまたはバッチ ファイルとして認識されていません。
                        @

                        ... which means it's no good (my OS is Japanese).

                        I tried playing around with your ideas - escaping quotes and stuff, but I got nowhere.

                        Thanks for the interest and any further help.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JKSH
                          Moderators
                          wrote on 14 Apr 2014, 15:12 last edited by
                          #12

                          Oops, you need to wrap the path to PDFCreator.exe in quotes too, because there are spaces in the "Program Files" part.

                          Try these:

                          @
                          myProcess->start(""""C:\Program Files\PDFCreator\PDFCreator.exe""" /NOSTART /PF"""C:\Users\Steve\Documents\arf*.pub"""");
                          @

                          @
                          system(""C:\Program Files\PDFCreator\PDFCreator.exe" /NOSTART /PF"C:\Users\Steve\Documents\arf*.pub"");
                          @

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            BonRouge
                            wrote on 14 Apr 2014, 15:24 last edited by
                            #13

                            Hello again.

                            @ myProcess->start(""""C:\Program Files (x86)\PDFCreator\PDFCreator.exe""" /NOSTART /PF"""C:\Users\Steve\Documents\arf*.pub"""");
                            @

                            This gave me an error (unknown escape sequence '*'), so I added an extra backslash there and then I just got nothing.

                            @system(""C:\Program Files (x86)\PDFCreator\PDFCreator.exe" /NOSTART /PF"C:\Users\Steve\Documents\arf\*.pub"");@

                            This gave me the same error as above (the Japanese one).

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              JKSH
                              Moderators
                              wrote on 14 Apr 2014, 16:44 last edited by
                              #14

                              Huh, it looks like system() is broken on Windows.

                              @
                              // This works
                              system(""C:\Program Files\7-zip\7z.exe" a -tzip C:\Test\output.zip C:\Test\*.txt");
                              @

                              @
                              // This gives me:
                              // 'C:\Program' is not recognized as an internal or external command,
                              // operable program or batch file.
                              system(""C:\Program Files\7-zip\7z.exe" a -tzip "C:\Test\output.zip" C:\Test\*.txt");
                              @

                              @
                              // This works, surprisingly!
                              QProcess p;
                              p.start(""C:\Program Files\7-zip\7z.exe" a -tzip "C:\Test\output.zip" C:\Test\*.txt");
                              @

                              So, perhaps you should try this?:
                              @
                              myProcess->start(""C:\Program Files (x86)\PDFCreator\PDFCreator.exe" /NOSTART /PF"C:\Users\Steve\Documents\arf\*.pub"");
                              @

                              (P.S. This is with Qt 5.3.0 beta. I don't know what it's like on Qt 4.7)

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                BonRouge
                                wrote on 14 Apr 2014, 23:06 last edited by
                                #15

                                Thanks again, but that kind of takes me back to where I was: the line works when there's no space in the directory name, but not when there is a space. (And I tried it on Qt 5).

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  JKSH
                                  Moderators
                                  wrote on 15 Apr 2014, 00:21 last edited by
                                  #16

                                  I'm out of ideas, I'm afraid.

                                  Try subscribing to the "Interest mailing list":http://lists.qt-project.org/mailman/listinfo/interest and asking there. Someone had a similar problem not long ago, which was resolved on that list: http://comments.gmane.org/gmane.comp.lib.qt.user/11719

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  1 Reply Last reply
                                  0
                                  • hskoglundH Offline
                                    hskoglundH Offline
                                    hskoglund
                                    wrote on 15 Apr 2014, 01:12 last edited by
                                    #17

                                    Hi, just tested, when I changed your lines:
                                    @ QStringList arguments;
                                    arguments << "/NOSTART" << "/PF"+r+"\*.pub";@

                                    to
                                    @ QStringList arguments;
                                    QString sQuote = """;
                                    arguments << "/NOSTART" << "/PF"+ sQuote + r + "\*.pub" + sQuote;
                                    @

                                    I got a PDF file (but my system is English). Anyway I think it should work, just a matter of fixing those d*mn quotes..

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      BonRouge
                                      wrote on 15 Apr 2014, 03:18 last edited by
                                      #18

                                      hskoglund,

                                      Thanks for trying, but with that, with a directory that worked before, I get this from PDFCreator:

                                      bq. The file can not be found!
                                      \“C:\Users\Steve\Documents\arf*.pub\”

                                      I don't know if the language of the system is relevant, but interestingly, I often find in things like this, where you would normally see a '', I Windows shows a yen sign. I'm not sure why that is or if it means the code is treated any differently (maybe I should look this up) but I don't usually have a problem with it - I type a blackslash and I see a backslash in my code and it escapes things and whatever it needs to do, but the little message windows that give errors like the one I've shown above actually show yen signs. This is not specific to what I'm doing here - it often happens, and it's never been a problem - I just see it as a quirk of the Japanese OS.

                                      [edit]
                                      Here's something related to the backslash/yen thing: http://www.pcreview.co.uk/forums/japanese-fonts-changing-backslash-yen-symbol-t452657.html
                                      [/edit]

                                      1 Reply Last reply
                                      0
                                      • P Offline
                                        P Offline
                                        primem0ver
                                        wrote on 15 Apr 2014, 07:24 last edited by
                                        #19

                                        [quote author="BonRouge" date="1397489044"]Hello again.

                                        @ myProcess->start(""""C:\Program Files (x86)\PDFCreator\PDFCreator.exe""" /NOSTART /PF"""C:\Users\Steve\Documents\arf*.pub"""");
                                        @

                                        This gave me an error (unknown escape sequence '*'), so I added an extra backslash there and then I just got nothing.

                                        @system(""C:\Program Files (x86)\PDFCreator\PDFCreator.exe" /NOSTART /PF"C:\Users\Steve\Documents\arf\*.pub"");@

                                        This gave me the same error as above (the Japanese one).
                                        [/quote]

                                        I really know nothing about how the Qt Process object works as I am new to Qt. However, the system option is formatted incorrectly. You must have a space between /PF and "C:\Users\Steve\Documents\arf\*.pub""

                                        It should read:
                                        @system(""C:\Program Files (x86)\PDFCreator\PDFCreator.exe" /NOSTART /PF "C:\Users\Steve\Documents\arf\*.pub"");@

                                        1 Reply Last reply
                                        0
                                        • B Offline
                                          B Offline
                                          BonRouge
                                          wrote on 15 Apr 2014, 07:30 last edited by
                                          #20

                                          prImem0ver,

                                          Thanks for the input, but this is from "the PDF Creator site":http://www.pdfforge.org/content/command-line-parameters:

                                          /PF<filename>
                                          Print a file with the standard program linking with the extension of the file. In general, this option is useful in connection with Auto-Save mode. It is not possible to use this parameter in conjunction with the /OF parameter. There is NO space between the parameter and the file name.

                                          (The emphasis is on the site, not added by me.)

                                          1 Reply Last reply
                                          0

                                          6/33

                                          13 Apr 2014, 16:33

                                          topic:navigator.unread, 27
                                          • Login

                                          • Login or register to search.
                                          6 out of 33
                                          • First post
                                            6/33
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved