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

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 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
              • H Offline
                H 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
                      • P Offline
                        P Offline
                        primem0ver
                        wrote on 15 Apr 2014, 07:32 last edited by
                        #21

                        You will not be able to use quotes then. At least not around that directory. Try it without the quotes since there is no space there.

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

                          The problem is that I want this to work with directories that have spaces - not just that one that I was testing with there - I have a couple of directories and files that I'm testing with - with and without spaces.

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

                            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.

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

                              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. :)

                              http://www.developpez.net/forums/d1431043/autres-langages/general-visual-basic-6-vbscript/vbscript/script-pdf-creator/#post7772759

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                hskoglund
                                wrote on 15 Apr 2014, 13:17 last edited by
                                #25

                                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 :-)

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

                                  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. :-)

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    hskoglund
                                    wrote on 15 Apr 2014, 14:05 last edited by
                                    #27

                                    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.

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

                                      Great! Thanks for being old! And thanks for your time and help. :-)

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

                                        Great work, hskoglund! :)

                                        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 17 Apr 2014, 04:42 last edited by
                                          #30

                                          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();

                                          }
                                          @

                                          1 Reply Last reply
                                          0

                                          20/33

                                          15 Apr 2014, 07:30

                                          • Login

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