Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: FOR command starting with double quotes.  (Read 4821 times)

0 Members and 1 Guest are viewing this topic.

aarondiers

    Topic Starter


    Greenhorn

    Thanked: 1
    • Experience: Expert
    • OS: Unknown
    FOR command starting with double quotes.
    « on: October 11, 2011, 03:53:22 PM »
    I have a simple batch command that wants to capture a program's output.  It works fine when used directly:

    Code: [Select]
    FOR /F %A IN ('someprogram.exe "argument 1" "argument 2" "argument 3"') DO ECHO %A
    However, I'd like to include an absolute path to someprogram.exe, which means using a quoted path.  This doesn't work:

    Code: [Select]
    FOR /F %A IN ('"C:\Program Files\someprogram.exe" "argument 1" "argument 2" "argument 3"') DO ECHO %A
    The result is:

    Code: [Select]
    The filename, directory name, or volume label syntax is incorrect.
    I've tried this with usebackq, but that produces analogous results.  I've also tried escaping the double quote with a caret (^).  This works fine for the arguments, but doesn't work when used around the program name.  I've also tried running it through a CMD /C but that also doesn't like a starting quote.  Does anyone here know the answer to this problem?

    Raven19528



      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: FOR command starting with double quotes.
      « Reply #1 on: October 11, 2011, 07:05:06 PM »
      Your best bet in a situation like this is setting the absolute path to a variable prior to the for command and using the variable in the for line.

      Code: [Select]
      set someprogram=c:\Program Files\someprogram.exe
      ...
      FOR /F %A IN ('%someprogram% "argument 1" "argument 2" "argument 3"') DO ECHO %A
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare

      aarondiers

        Topic Starter


        Greenhorn

        Thanked: 1
        • Experience: Expert
        • OS: Unknown
        Re: FOR command starting with double quotes.
        « Reply #2 on: October 12, 2011, 07:39:52 AM »
        Thanks, but that doesn't help, either.  I get the same results whether I use variables or not.  This is on Windows 7.

        Raven19528



          Hopeful
        • Thanked: 30
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          Re: FOR command starting with double quotes.
          « Reply #3 on: October 12, 2011, 09:20:56 AM »
          Now I know some or all of this you have probably checked out, but I've been taught not to assume anything. Have you checked the file path and made sure that the program (not a shortcut, but the actual program) is in the file path location you are setting? Are you using the correct slash (\) in the file path (it does happen, more often than most would like to admit)? Are you sure you are typing the correct extension?

          If all of these issues check out, then I would propose that you simply do a change directory command and get to the directory you are wanting then using your original line.

          I tested out my proposal below, and I was actually able to accomplish what you are wanting to do here. This was what I typed in to the command prompt, and the output I received.

          Code: [Select]
          c:\>set prog=c:\users\user\desktop\batch\Passing variables\arithmetic2.bat
          c:\>for /f %A in ('"%prog%" 5 4') do echo %A

          c:\>echo 9
          9

          I am also using a Windows 7 command prompt so my suggestion would be to either use the variable within the quotes as shown above, or to thoroughly check your other areas and make certain that all of the above issues are indeed addressed.

          The other possibility is that this is the output from the program you are running. Try running the program with variables through the command prompt and see what output you get. At this point, it is difficult to troubleshoot the problem because it isn't apparant where the problem is.
          "All things that are
          Are with more spirit chased than enjoy'd" -Shakespeare

          aarondiers

            Topic Starter


            Greenhorn

            Thanked: 1
            • Experience: Expert
            • OS: Unknown
            Re: FOR command starting with double quotes.
            « Reply #4 on: October 12, 2011, 10:57:23 AM »
            Thanks, the command I am trying to execute works just fine when run outside of the FOR statement.

            I do see a slight difference that seems to be significant.  I can get your line to work:

            Code: [Select]
            for /f %A in ('"%prog%" 5 4') do echo %A
            However, several of the arguments to my program also need to be quoted.  Does this work for you?

            Code: [Select]
            for /f %A in ('"%prog%" "5" "4"') do echo %A

            Raven19528



              Hopeful
            • Thanked: 30
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Re: FOR command starting with double quotes.
              « Reply #5 on: October 12, 2011, 12:29:15 PM »
              That did not work, however the error I was getting was different from yours. It did spark a curiousity in me though, and I tried the following with success:

              Code: [Select]
              set prog="c:\users\user\desktop\batch\passing variables\arithmetic2.bat"
              set arg1="5"
              set arg2="4"

              for /f %A in ('"%prog% %arg1% %arg2%"') do echo %A

              c:\>echo 9
              9

              For some reason, the cmd prompt would not pick up the first double quote, and would therefore not run the program. I would imagine that if there are no spaces in the file path to your program, then it ends up running but without all of the parameters being passed to it, thereby generating the error you are getting. By essentially setting up a double double quote, the cmd prompt sees what we are wanting it to see and passes the correct parameters. I do not know the why on this one, but hopefully the how will be enough to get done what you want to get done.
              "All things that are
              Are with more spirit chased than enjoy'd" -Shakespeare

              aarondiers

                Topic Starter


                Greenhorn

                Thanked: 1
                • Experience: Expert
                • OS: Unknown
                Re: FOR command starting with double quotes.
                « Reply #6 on: October 12, 2011, 12:54:20 PM »
                Thanks, that works, and I'll add one more caveat for anyone who may read this in the future, hoping it will save some headaches.  If you're on a 64-bit windows machine, then there are two folders for program files:

                C:\Program Files
                C:\Program Files (x86)

                If you're trying to do this kind of FOR command, then you also need to explicitly escape the closing parentheses with carets, as well as do everything else with double quotes from the example above.

                Code: [Select]
                SET prog="C:\Program Files (x86^)\Folder\Prog.exe"
                SET arg1="C:\Program Files (x86^)\Folder\filename.dll"
                SET arg2="Hello World"
                FOR /F %A IN ('"%prog% %arg1% %arg2%"') do echo %A

                What a mess!
                Also, for anybody who'd like a shortcut for escaping the parenthesis with the caret, try this:

                Code: [Select]
                SET variable=%variable:)=^)%
                Thanks again for all of the help!

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: FOR command starting with double quotes.
                « Reply #7 on: October 12, 2011, 01:07:25 PM »
                The x86 folder is there so 64bit Windows can run native 32bit apps...
                " Anyone who goes to a psychiatrist should have his head examined. "

                Raven19528



                  Hopeful
                • Thanked: 30
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Windows 7
                  Re: FOR command starting with double quotes.
                  « Reply #8 on: October 13, 2011, 10:43:34 AM »
                  Also, for anybody who'd like a shortcut for escaping the parenthesis with the caret, try this:

                  Code: [Select]
                  SET variable=%variable:)=^)%

                  And in case you are wanting to do that through an entire file:

                  Code: [Select]
                  @echo off
                  set /p file=What file do you need to caretize?
                  for /f "delims=" %%A in (%file%) do (
                    set line=%%A
                    call set line=%line:)=^)%
                    echo %line%>>output.txt
                  )
                  ren %file% %file%.old
                  ren output.txt %file%

                  Make sure the file to be changed is in the same directory as the batch file. Otherwise you would have to adjust the code a little bit/
                  "All things that are
                  Are with more spirit chased than enjoy'd" -Shakespeare