Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

[OT] Invoking Perl via batchfile: ampersand in PATH [Solved]

by Athanasius (Chancellor)
on Nov 02, 2015 at 09:01 UTC ( #1146683=perlquestion: print w/replies, xml ) Need Help??
Athanasius has asked for the wisdom of the Perl Monks concerning the following question:

I run a portable edition of Strawberry Perl (under Windows 8.1) invoked by the batch file portableshell.bat which contains the following line:

set PATH=%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c\bin;%PATH +%

This has been working well. However, after updating MySQL and installing MySQL Workbench, I now have a directory:

C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5

which has also been added to the PATH environment variable. Now, when I open a normal Windows command prompt, the PATH is set correctly; but when I run portableshell.bat the ampersand in the directory name is interpreted as a “special character” and the path is truncated.

Google search has led me to the following suggested fixes:

  • Escape the ampersand with a caret: \MySQL Fabric 1.5 ^% MySQL Utilities 1.5
  • Quote the directory name in the PATH variable
  • Quote the assignment in the batch file: set "PATH=...;%PATH%"

I’ve tried these in various permutations, but so far nothing works. :-(

Yes, I could rename the directory, but I’m afraid this will break, e.g., MySQL Notifier.

So, is there an equivalent of quotemeta for Windows batch files? Or another workaround?

Thanks,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re: [OT] Invoking Perl via batchfile: ampersand in PATH
by Discipulus (Monsignor) on Nov 02, 2015 at 09:32 UTC
    Hello Athanasius,

    apart from my allergy to any non ascii chars in everything in any os, if i remember correctly Strawberry told me something like:"Strawberry Perl cannot be installed in a directory with spaces or non-ASCII characters" and probably the space is worst than ampersand. I personally would prefer to reinstall the program in a normalized path..
    Anyway i think you can modify the portableshell.bat to include your malefical path as follow
    set _TEST="c:\path\dir & test" set PATH=%_TEST%;%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c +\bin;%PATH%
    then you can use it as you wish, after launching the batch:
    c:\strawberry>set ... Path="c:\ulisse\dir & test";C:\ulisse\strawberry\perl\site\bin;C:\ulis +se\strawberry\perl\bin; ... _TEST="c:\path\dir & test" c:\strawberry>cd %_TEST% c:\path\dir & test>perl -e "print $ENV{PATH}" c:\path\dir & test;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin; +C:\strawberry\c\bin; .....
    UPDATE: ahh.. you can also use junction (from sysinternals) as mentioned in the thread Installing CPAN on a Windows system or native command to achieve the same result: see here

    HtH

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hello Discipulus,

      Thanks for the reply. Unfortunately, it doesn’t help. :-( Adding %_TEST% in this way does succeed in getting the full directory name into the PATH variable; but the later %PATH% in the assignment still chokes on the ampersand, as before.

      However, adding double quotes around the assignment does work, after all:

      set "PATH=%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c\bin;%PAT +H%"

      (I made a mistake when testing.)

      Junction looks interesting. I’ll have to look into it.

      Thanks,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Invoking Perl via batchfile: ampersand in PATH ( this.bat )
by Anonymous Monk on Nov 02, 2015 at 09:43 UTC

    So, is there an equivalent of quotemeta for Windows batch files? Or another workaround?

    make a hard link with a shorter name without spaces and amperstands

    or do the best and most closest approximation of quotemeta, see this.bat, this2.bat ... & is particularly stupid but doable

    this3.bat

    $ "rock&roll\this3.bat" "D:\rock&roll\this3.bat" escaped-thisfile "D:\rock^&roll\this3.bat" $ D:\rock^&roll\this3.bat "D:\rock&roll\this3.bat" escaped-thisfile "D:\rock^&roll\this3.bat"

    Its triple interpolation, or eval interpolation , or ... whatever, its batguano :P

    See also the following, maybe improve to make hard links for &nonsense vienv - Edit local environment variables on Win32, CleanPath , SetEnviron.bat

      Thanks! Not sure if it’s what you had in mind, but your advice:

      make a hard link with a shorter name without spaces and amperstands

      gave me the clue I needed: I replaced the directory name with its DOS short name (8.3 format) obtained via dir /x. This is a better solution, as it means I only have to make a single change to the environment variable definition, instead of updating a separate batch file for each Strawberry Perl portable edition.

      Thanks again,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: [OT] Invoking Perl via batchfile: ampersand in PATH
by syphilis (Chancellor) on Nov 02, 2015 at 09:51 UTC
    I think you just need to replace (in potableshell.bat's "set PATH=..." line) %PATH% with "%PATH%".

    Cheers,
    Rob

      Hello syphilis,

      Thanks. Actually putting double quotes around just %PATH% works, but produces a different warning:

      'cmd' is not recognized as an internal or external command, operable program or batch file.

      I have no idea why. But putting double quotes around the whole assignment works with no warning! See my reply to Discipulus, above.

      Thanks,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1146683]
Approved by Discipulus
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-05-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?