My application generates at run time a Windows BAT file (oh you wonderful Windows Batch language - if I only knew which insane person had invented it!), which then in turn calls other programs (some compiled C application, some Perl programs). I need to pass strings from my Perl application via the Batch file to these programs (using the environment is no option here). For example:

my $some_argument='abc'; # ... $batchfile=IO::File->new(">x.bat"); print $batchfile "\@echo off\nMyProg $some_argument\n"; $batchfile->close; # ... later, in a different process ... : system("x.bat"); # executes MyProg abc
That's the basic idea. Of course it is not so easy, because I don't know the content of $some_argument until at run-time, and this means I have to generate the argument in a way which is properly quoted according to Batch Language Syntax Rules.

I researched a bit how to do proper quoting in Windows batch files, and though a found a bit of information here and a bit of information there, I could not find a concise document which really describes it properly. So my first question is:

Does someone happen to know a CPAN module which implements Windows Batch Language quoting? Otherwise, does someone know the rules, so that I can implement it myself? So far, I found the following set of rules:

  1. The special characters <>|^ must be escaped by ^ (for example, we have to convert 'a^b|c' into 'a^^b^|c')
  2. A double quote at the beginning or at the end of the argument must be escaped by \ (for example, we have to convert '"ab"' to '\\"ab"\\')
  3. If the argument contains spaces, it must be enclosed by double quotes, and in practice, it does not hurt to enclose the argument in double quotes always (for example, we have to convert 'a b' to '"a b"')
  4. A double quote, which is followed by a space, must get a backslash in front (for example, we have to convert 'a" b' to 'a\" b')
  5. If the argument starts with \", we are out of luck (at least I have not found yet a way how to encode the string '\\"foo' properly for my batchfile
Is this list complete or do I miss something?

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Windows quoting by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.