Skip to content

Windows Batch code generation improvements #91

Description

@andry81

Expression:

c = [1, 2, "str", true, false];
set /a c[0]=1
set /a c[1]=2
set "c[2]=str"
set /a c[3]=1
set /a c[4]=0

Command:

Note

  1. Supports control characters except double quotes: for /F ... in ("...with control characters except a double quote character...") do ...%%i or for /F "usebackq ..." ... in ('"...with control characters except a double quote character..."') do ...%%~i
  2. Supports an empty value: for /F "usebackq ..." ... in ('""') do ... %%~i
  3. Supports print of ' character as is: for /F "usebackq ..." ... in ('"...'....'..."') do ... %%~i
  4. eol^= for the empty EOL (Empty EOL in `for /F` statement andry81/contools#5)
  5. <nul closes the stdin handle to quit the set /P command immediately after a line print without the trailing line return (instead of echo | set /P ... which creates a child cmd.exe process): <nul set /P =...
  6. In case of enabled variable expansion by default the forwarding setlocal DISABLEDELAYEDEXPANSION and trailing endlocal prevents lines from the early delayed expansion in both for F statement and in %%~i expression: setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq ..." ... in ('"...!...!..."') do ...%%~i) & endlocal, when setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq ..." ... in ('"...!...!..."') do endlocal & ...%%~i prevents only in for /F statement
  7. ^! in for /F statement suppresses the delayed expansion in %%~i after the endlocal (`!` character can be escaped while `ENABLEDELAYEDEXPANSION` is used andry81/contools#33): setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq ..." ... in ('"...^!...^!..."') do endlocal & ...%%~i
  8. Double quotes in set "...=%%~i" expression does ignore trailing white space character(s) after the second double quote and so can be chained by the & operator
  9. A closed parentheses in echo.%%~i) avoids print of a trailing white space character(s) after the closed parentheses

Caution

All values in for /F statement must be already without a double quote in between of '" and "': for /F "usebackq ..." ... in ('"...with control characters except a double quote character..."') do ...%%~i

Note

To workaround double quotes in variables you can simple remove (or even replace) them:

@echo off
set A_="123" "123"=
if defined A_ set "A_=%A_:"=%"
set A_

Batsh code:

println("");
println("''");
println("Hello&world!");
print("");
print("Hello");
print("&world");
println("");

A = "Raw Value"
B = "Raw Value !A!"

println("!A!");
println("!B!");

println(A);
println(B);

Windows Batch equivalent script:

@echo off

setlocal ENABLEDELAYEDEXPANSION

setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('""') do echo;%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"''"') do echo;%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"Hello&world!"') do echo;%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('""') do <nul set /P =%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"Hello"') do <nul set /P =%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"&world!"') do <nul set /P =%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('""') do echo;%%~i) & endlocal

setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq tokens=* delims="eol^= %%i in ('"Raw Value"') do endlocal & set "A=%%~i" 
setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq tokens=* delims="eol^= %%i in ('"Raw Value ^!A^!"') do endlocal & set "B=%%~i"

setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"!A!"') do echo;%%~i) & endlocal
setlocal DISABLEDELAYEDEXPANSION & (for /F "usebackq tokens=* delims="eol^= %%i in ('"!B!"') do echo;%%~i) & endlocal

setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq tokens=* delims="eol^= %%i in ('"!A!"') do endlocal & echo;%%~i
setlocal DISABLEDELAYEDEXPANSION & for /F "usebackq tokens=* delims="eol^= %%i in ('"!B!"') do endlocal & echo;%%~i

Result:

 
''
Hello&world!
Hello&world!
!A!
!B!
Raw Value
Raw Value !A!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions