5

I can pass a double quote, and a larger than sign to any command in several ways: '"', "\"", ">"

But when i try to pass them together

C:\>echo "\">"
The system cannot find the path specified.

Same with "\"\>". I could make it work with single quotes around, but since i already have so much going on with dealing with quotes i'd like to keep it all inside double quotes.

Is there any way to escape that?

I'm on windows7 but i believe this is some backward compatibility 'feature', so not sure this info is relevant.

Edit 1:

I tought Endoro had the right answer... but it's not that simple. CMD treats ^> differently depending if there's a escaped double quote in the string. Anyone have any idea why?! or a different escaping method?

C:\>sh echo "\"^>"
">

C:\>sh echo "a^>"
a^>

C:\>echo "\"^>"
"\">"

C:\>echo "a^>"
"a^>"

Edit 2: here are the tests cases for what Monacraft suggested, using ^ before the quotes that go around the string

C:\>echo ^"a/>"
The system cannot find the path specified.
(we still need to escape that > symbol)

C:\>echo ^"a/^>"
"a/>"
(work fine without \" in the string)

C:\>echo ^"\"/^>"
"\"/^>"
(add a single \" and the ^> escaping stop to works)

C:\>echo ^""/^>"
""/^>"
(ok, using ^ before the string means i dont have to escape the " anymore)

C:\>echo ^"^\"/^>"
"\"/^>"
(but what if i have an actual \" in my input that i have to escape... would ^\ prevent this from happening? nope)
gcb
  • 13,901
  • 7
  • 67
  • 92
  • @Endoro sorry, i really thought it was the right answer until i added more tests and saw what i mention on the edit. I even started to write a new question, but i was pretty much the same question as before. I didn't take it light as well... sorry if it offended. – gcb Aug 28 '13 at 19:27
  • 1
    [Escaping characters like ", <, >, >>, or | in the arguments to a batch file](https://stackoverflow.com/q/32414436/995714), [Batch character escaping](https://stackoverflow.com/q/6828751/995714), [Escape percent in bat file](https://stackoverflow.com/q/13551829/995714), [Escape special character in a windows batch](https://stackoverflow.com/q/32817929/995714), [Special Characters in Batch File](https://stackoverflow.com/q/37333620/995714) – phuclv Nov 18 '18 at 14:53

1 Answers1

3

OK, I can't give you a full explanation, but if you put an escape character before the double quotes it works:

C:\>echo "a^>"
"a^>"

C:\>echo ^"a^>"
"a>"

I think by putting a ^ before the string, your telling cmd not to treat a ^ inside the string as part of the actual string. Which is why:

C:\>echo "text^>" ^"text^>"
"text^>" "text>"

Does that. However, I can't give you a full explanation, but at least that solves your problem.

Edit 2:

Ok, for edit 2 All I can say is that you don't need to escape anything inside the string!

C:\>echo ^"\"/>"
"\"/>"

Also found this website which explained that to escape a \ all you need is \\. Click here for more Information. For " simply double up the quotes ("").

Community
  • 1
  • 1
Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • this made it even more confusing :) adding `\"` still breaks it. Will add the test cases to the question. – gcb Aug 29 '13 at 21:35
  • awesome link! i was trying to find something like this in msdn with no luck! Thanks – gcb Aug 29 '13 at 23:20
  • About not escaping, remember that i must use the same pattern for any input the user trhows at me. So no escaping for `\"` means i will get an error when there is no `\"` but a `>`. See the 1st example on my edit2. – gcb Aug 29 '13 at 23:26
  • but then it does not work with your 1st example `echo ^"\"/^>"` :) there's no way to programatically encode strings that you don't know the content. unless you write a parser. which is what i did as a work around, but i want to remove that. I think the link you send is the solution. i will just have to encode `"` as `""`... let's see. will work on that again in a while. – gcb Aug 30 '13 at 00:45
  • Ok, if you have more questions we can move this to a StackChat conversation. I'll update you if I find anyother information. – Monacraft Aug 30 '13 at 03:02
  • Accepting this as I guess this is the best it can get. lesson: do not try to pass anything other than simple ascii strings in CMD. – gcb Mar 10 '14 at 22:44