“漂亮的印刷品” windows %PATH% 变量 - 如何在“;”上拆分在 CMD 外壳中

发布于 2024-10-27 07:25:06 字数 615 浏览 3 评论 0原文

我想在 Windows CMD 提示符下运行一个简单的一行来打印我的 %PATH% 变量,每行一个条目。

我尝试了这个: for /f "delims=;" %a in ("%path%") do echo %a 但这仅打印第一个条目:

Z:\>for /f "delims=;" %a in ("%path%") do echo %a

Z:\>echo c:\python25\.
c:\python25\.

另外,正如您从上面的输出中看到的,这也打印了 echo %a > 命令以及输出。有什么办法可以阻止这种情况吗?

如果我尝试类似的命令,我会得到所有条目,但仍然会得到 echo %a 输出垃圾邮件结果。我不明白为什么以下内容会打印所有条目,但我对 %PATH% 的尝试却没有。我怀疑我不明白 /F 开关。

Z:\>for %a in (1 2 3) do echo %a

Z:\>echo 1
1

Z:\>echo 2
2

Z:\>echo 3
3

I want to run a simple one-liner in the Windows CMD prompt to print my %PATH% variable, one entry per line.

I tried this: for /f "delims=;" %a in ("%path%") do echo %a but this only prints the first entry:

Z:\>for /f "delims=;" %a in ("%path%") do echo %a

Z:\>echo c:\python25\.
c:\python25\.

Also as you can see from the output above, this is also printing the echo %a command as well as the output. Is there any way to stop this?

If I try a similar command, I get all the entries, but still get the echo %a output spamming the results. I don't understand why the following prints all entries, but my attempt on %PATH% doesn't. I suspect I don't understand the /F switch.

Z:\>for %a in (1 2 3) do echo %a

Z:\>echo 1
1

Z:\>echo 2
2

Z:\>echo 3
3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

甜尕妞 2024-11-03 07:25:06

简单的方法是使用

for %a in ("%path:;=";"%") do @echo %~a

This Works for all without ; in the path and without " around a single element
测试路径=C:\qt\4.6.3\bin;C:\Program Files;C:\documents &设置

但是“始终”的解决方案有点复杂
编辑:现在是一个工作变体

@echo off
setlocal DisableDelayedExpansion
set "var=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"

set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"

set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intended
set var=%var:""="%
set "var=%var:"=""%"

set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"

setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
    endlocal
    echo %%~a
    setlocal EnableDelayedExpansion
)

我在那里做了什么?
我试图解决主要问题:引号内的分号应该被忽略,并且只有 正常 分号应该替换为 ";"

我使用了批处理解释器本身为我解决这个问题。

  • 首先,我必须使字符串安全,转义所有特殊字符。
  • 然后所有 ; 都替换为 ^;^;
  • 然后技巧从行开始
    set var=%var:"=""%" (缺少的引号是关键!)。
    其扩展方式使得所有转义字符都将失去其转义插入符:
    var=foo &; bar;;baz<>gak;;“半^;^;冒号^;^;^&嵌入”;;又是foo!;;...
    但仅限于引号之外,因此现在引号 ;; 之外的分号和 ^;^; 内部的分号之间存在差异。
    这就是关键。

The simple way is to use

for %a in ("%path:;=";"%") do @echo %~a

This works for all without ; in the path and without " around a single element
Tested with path=C:\qt\4.6.3\bin;C:\Program Files;C:\documents & Settings

But a "always" solution is a bit complicated
EDIT: Now a working variant

@echo off
setlocal DisableDelayedExpansion
set "var=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"

set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"

set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intended
set var=%var:""="%
set "var=%var:"=""%"

set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"

setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
    endlocal
    echo %%~a
    setlocal EnableDelayedExpansion
)

What did I do there?
I tried to solve the main problem: that the semicolons inside of quotes should be ignored, and only the normal semicolons should be replaced with ";"

I used the batch interpreter itself to solve this for me.

  • First I have to make the string safe, escaping all special characters.
  • Then all ; are replaced with ^;^;
  • and then the trick begins with the line
    set var=%var:"=""%" (The missing quote is the key!).
    This expands in a way such that all escaped characters will lose their escape caret:
    var=foo & bar;;baz<>gak;;"semi^;^;colons^;^;^&embedded";;foo again!;;...
    But only outside of the quotes, so now there is a difference between semicolons outside of quotes ;; and inside ^;^;.
    Thats the key.
坏尐絯 2024-11-03 07:25:06

一个简单的一行来美化打印 PATH 环境变量:

ECHO.%PATH:;= & ECHO.%

如果您的 PATH 等于 A;B;C 上面的字符串替换将会改变这到 ECHO.A &回声.B& ECHO.C 并一次性执行它。句号可防止出现“ECHO is on”消息。

A simple one liner to prettying printing the PATH environment variable:

ECHO.%PATH:;= & ECHO.%

If your PATH was equal to A;B;C the above string substitution will change this to ECHO.A & ECHO.B & ECHO.C and execute it all in one go. The full stop prevents the "ECHO is on" messages from appearing.

难如初 2024-11-03 07:25:06

Stephan Quan 非常聪明的单行解决方案的更新:我遇到的问题是尾随分号 - (可能还有两个连续的分号,即空路径元素)会导致出现“ECHO is on”消息。我通过在第二个 ECHO 语句之后立即插入一个句点(这是抑制 ECHO is on/off 消息的语法)解决了这个问题。但是,这会导致额外的空行:

ECHO %PATH:;= & ECHO.%

An update to Stephan Quan's very clever one-liner solution: The problem I encountered was that a trailing semi-colon - (and maybe two successive semi-colons, i.e. empty path element) would cause the message "ECHO is on" to appear. I solved this by inserting a period immediately after the second ECHO statement (which is the syntax to suppress ECHO is on/off messages). However, it will result in an extra empty line:

ECHO %PATH:;= & ECHO.%
时光与爱终年不遇 2024-11-03 07:25:06

我对 Jeb 聪明的“始终”解决方案进行了一些小改进。目前jeb的解决方案存在以下问题:

  1. 如果前导路径用引号引起来,那么第一个输出
    以 "" 开头
  2. 如果尾随路径用引号括起来,则最后的输出
    以“”结尾
  3. 如果任何路径包含无害但无功能的连续“”,
    那么输出保留“”
  4. If var contains Continuous ;;分隔符然后输出 ECHO is off

此解决方案修复了小问题,并且减少了 2 个替换。此外,我还消除了循环内不必要的重复启用/禁用延迟扩展。 (编辑于2011-10-30简化了ENDLOCAL逻辑)

@echo off
setlocal DisableDelayedExpansion
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
set var=%var:""="%
set "var=%var:"=""Q%"
set "var=%var:;;="S"S%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
setlocal EnableDelayedExpansion
set "var=!var:"Q=!"
for %%a in ("!var:"S"S=";"!") do (
  if "!!"=="" endlocal
  if %%a neq "" echo %%~a
)

如果您想看到连续 ;; 产生的每个空路径都有一个空行分隔符,那么 FOR 循环的最后一行可以简单地读取 echo(%%~a )。

或者,使用以下方法将空路径显示为“”可能会更明显:
if %%a=="" (echo "") else echo %%~a

各种空路径修复也适用于 jeb 的简单解决方案。

更新:这是一个使用 JREPL.BAT 的简单单行代码

您可以使用我的 JREPL.BAT 正则表达式文本处理实用程序实现简单、非常强大的解决方案。 JREPL.BAT 是纯脚本(混合 JScript/批处理),可以在 XP 及以上版本的任何 Windows 计算机上本机运行。

jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path

I have minor improvements to jeb's clever "always" solution. Currently jeb's solution has the following issues:

  1. If the leading path is enclosed in quotes, then the first output
    starts with ""
  2. If the trailing path is enclosed in quotes, then the last output
    ends with ""
  3. If any path contains harmless but non-functional consecutive "",
    then the output preserves the ""
  4. If var contains consecutive ;; delimiters then outputs ECHO is off

This solution fixes the minor issues, plus it uses 2 fewer substitutions. Also I eliminated the unnecessary repeated enabling/disabling delayed expansion within the loop. (Edit on 2011-10-30 simplified the ENDLOCAL logic)

@echo off
setlocal DisableDelayedExpansion
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
set var=%var:""="%
set "var=%var:"=""Q%"
set "var=%var:;;="S"S%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
setlocal EnableDelayedExpansion
set "var=!var:"Q=!"
for %%a in ("!var:"S"S=";"!") do (
  if "!!"=="" endlocal
  if %%a neq "" echo %%~a
)

If you want to see a blank line for each empty path resulting from consecutive ;; delimiters, then the last line of the FOR loop can simply read echo(%%~a instead.

Or perhaps it would be more obvious to display empty paths as "" using:
if %%a=="" (echo "") else echo %%~a

The various empty path fixes work for jeb's simple solution as well.

UPDATE: Here is a simple one-liner using JREPL.BAT

You can use my JREPL.BAT regular expression text processing utility to achieve a simple, very robust solution. JREPL.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path
如果没有你 2024-11-03 07:25:06

Stephen Quan 的答案更短更好,但这里有一个 Python 解决方案:

python -c "import os; print(os.environ['PATH'].replace(';', '\n'));"

; 分号转换为 < code>\n 换行符。

Stephen Quan's answer is shorter and better, but here's a Python solution:

python -c "import os; print(os.environ['PATH'].replace(';', '\n'));"

Turning ; semicolons into \n newlines.

浪荡不羁 2024-11-03 07:25:06

这可以在 Windows 上使用 Git Bash 在 cmd 窗口中运行:

echo -e ${PATH//:/\\n}

您还可以在 .bash_profile 中创建一个方便的别名:

alias showpath='echo -e ${PATH//:/\\n}'

This works in cmd window using Git Bash on Windows:

echo -e ${PATH//:/\\n}

You can also make a handy alias in your .bash_profile:

alias showpath='echo -e ${PATH//:/\\n}'

溺ぐ爱和你が 2024-11-03 07:25:06

@ROMANIA_engineer 在评论中提出了 PowerShell 解决方案。由于问题要求在 CMD shell 中工作的命令,因此这里有一种从 OP 所需环境中使用优雅代码的方法:

powershell -Command ($env:Path).split(';')powershell -Command ($env :

为了使其更具可读性,您可以添加排序:

Path).split(';') |排序

信用:https://stackoverflow.com/a/34920014/704808

@ROMANIA_engineer proposed a PowerShell solution in a comment. Since the question asks for a command that works in the CMD shell, here is a way to use that elegant code from the OP's desired environment:

powershell -Command ($env:Path).split(';')

To make it still more readable, you can add sorting:

powershell -Command ($env:Path).split(';') | sort

Credit: https://stackoverflow.com/a/34920014/704808

碍人泪离人颜 2024-11-03 07:25:06

我知道这已经很旧了,但是FWIW;我总是因为某种原因想要这个。前段时间,我给自己写了一个脚本来做到这一点。我对其进行了一些润色并将其发布到我的博客上。

请随意使用它。

它称为 epath,该文件位于 inzi.com。它被编译为 EXE 以便于使用(使用 vbsedit): 此处

您可以在那里下载 exe。如果您希望将其作为 vbs 脚本,请查看该脚本的源代码。

    scriptname = Wscript.ScriptName 'objFSO.GetFileName(WScript.FullName)

    Function BubbleSort(arrData,strSort)
    'borrowed from here: http://vbscripter.blogspot.com/2008/03/q-how-do-i-sort-data-in-array.html

    'Input: arrData = Array of data.  Text or numbers.
    'Input: strSort = Sort direction (ASC or ascending or DESC for descending)
    'Output: Array
    'Notes: Text comparison is CASE SENSITIVE
    '        strSort is checked for a match to ASC or DESC or else it defaults to Asc


        strSort = Trim(UCase(strSort))
        If Not strSort = "ASC" And Not strSort = "DESC" Then
            strSort = "ASC"
        End If 

        For i = LBound(arrData) to UBound(arrData)
          For j = LBound(arrData) to UBound(arrData)
            If j <> UBound(arrData) Then
                If strSort = "ASC" Then
                  If UCase(arrData(j)) > UCase(arrData(j + 1)) Then
                     TempValue = arrData(j + 1)
                     arrData(j + 1) = arrData(j)
                     arrData(j) = TempValue
                  End If
                End If

                If strSort = "DESC" Then
                    If UCase(arrData(j)) < UCase(arrData(j + 1)) Then
                        TempValue = arrData(j + 1)
                        arrData(j + 1) = arrData(j)
                        arrData(j) = TempValue
                     End If        
                End If 
            End If
          Next
        Next

        BubbleSort = arrData

    End Function

    If Wscript.Arguments.Count>0 Then

        Set args = Wscript.Arguments

        bInLines = False
        bInAlphabetical = False
        bReverseSort = False
        bShowHelp = False

        For Each arg In args
            Select Case arg
                Case "-l"
                    bInLines = True
                Case "-a"
                    bInAlphabetical = True
                Case "-r"
                    bReverseSort = True
                Case Else
                    bShowHelp=True
            End Select  

        Next

        If bInLines = False Then
            bShowHelp=True
        End if

        If bShowHelp Then

                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt +  scriptname  & " Displays the system path in optionally friendly formats." & vbCrLf
                    sTxt = sTxt +  "ePath is helpful when viewing the system path and easily identifying folders therein." & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "EPATH [-l] [-a] [-r]" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "Switches:" & vbCrLf
                    sTxt = sTxt + vbTab + "[-l]" + vbtab + "Show the path broken out in lines" & vbCrLf
                    sTxt = sTxt + vbtab + "[-a]" + vbTab + "Sort the path broken out in lines sorted alphabetically" & vbCrLf
                    sTxt = sTxt + vbtab + "[-r]" + vbTab + "Reverse the alphabetic sort [asc default] (ignored without -a)" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + vbTab + "Examples:" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & vbTab & "(Show %PATH% normally)" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a -r" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -? Display help (what you are seeing now)" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "More info or questions at http://inzi.com" & vbCrLf


                    Wscript.Echo sTxt

                    WScript.Quit

        Else
            Set wshShell = CreateObject( "WScript.Shell" )
            sPath = wshShell.ExpandEnvironmentStrings( "%PATH%" )
            thePath = Split(sPath,";")

            If bInAlphabetical Then
                If bReverseSort Then
                    sDirection = "DESC"
                End If

                thePath = BubbleSort(thePath, sDirection)
            End if


            For Each item In thePath
                WScript.Echo item
            Next
            Set wshShell = Nothing
        End if
    Else
        'Nothing, echo the path.

        Set wshShell = CreateObject( "WScript.Shell" )
        WScript.Echo wshShell.ExpandEnvironmentStrings( "%PATH%" )
        Set wshShell = Nothing

    End If

I know this is old, but FWIW; I always run into wanting this for some reason or another. Some time ago, I wrote myself a script to do this. I put a little polish on it and posted it on my blog.

Feel free to use it.

It's called epath, and the file is at inzi.com. It's compiled as an EXE for easy use (using vbsedit): here

You can download the exe there. Here's the source code to the script if you want it as a vbs script.

    scriptname = Wscript.ScriptName 'objFSO.GetFileName(WScript.FullName)

    Function BubbleSort(arrData,strSort)
    'borrowed from here: http://vbscripter.blogspot.com/2008/03/q-how-do-i-sort-data-in-array.html

    'Input: arrData = Array of data.  Text or numbers.
    'Input: strSort = Sort direction (ASC or ascending or DESC for descending)
    'Output: Array
    'Notes: Text comparison is CASE SENSITIVE
    '        strSort is checked for a match to ASC or DESC or else it defaults to Asc


        strSort = Trim(UCase(strSort))
        If Not strSort = "ASC" And Not strSort = "DESC" Then
            strSort = "ASC"
        End If 

        For i = LBound(arrData) to UBound(arrData)
          For j = LBound(arrData) to UBound(arrData)
            If j <> UBound(arrData) Then
                If strSort = "ASC" Then
                  If UCase(arrData(j)) > UCase(arrData(j + 1)) Then
                     TempValue = arrData(j + 1)
                     arrData(j + 1) = arrData(j)
                     arrData(j) = TempValue
                  End If
                End If

                If strSort = "DESC" Then
                    If UCase(arrData(j)) < UCase(arrData(j + 1)) Then
                        TempValue = arrData(j + 1)
                        arrData(j + 1) = arrData(j)
                        arrData(j) = TempValue
                     End If        
                End If 
            End If
          Next
        Next

        BubbleSort = arrData

    End Function

    If Wscript.Arguments.Count>0 Then

        Set args = Wscript.Arguments

        bInLines = False
        bInAlphabetical = False
        bReverseSort = False
        bShowHelp = False

        For Each arg In args
            Select Case arg
                Case "-l"
                    bInLines = True
                Case "-a"
                    bInAlphabetical = True
                Case "-r"
                    bReverseSort = True
                Case Else
                    bShowHelp=True
            End Select  

        Next

        If bInLines = False Then
            bShowHelp=True
        End if

        If bShowHelp Then

                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt +  scriptname  & " Displays the system path in optionally friendly formats." & vbCrLf
                    sTxt = sTxt +  "ePath is helpful when viewing the system path and easily identifying folders therein." & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "EPATH [-l] [-a] [-r]" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "Switches:" & vbCrLf
                    sTxt = sTxt + vbTab + "[-l]" + vbtab + "Show the path broken out in lines" & vbCrLf
                    sTxt = sTxt + vbtab + "[-a]" + vbTab + "Sort the path broken out in lines sorted alphabetically" & vbCrLf
                    sTxt = sTxt + vbtab + "[-r]" + vbTab + "Reverse the alphabetic sort [asc default] (ignored without -a)" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + vbTab + "Examples:" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & vbTab & "(Show %PATH% normally)" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -l -a -r" & vbCrLf
                    sTxt = sTxt +  vbTab + vbTab + scriptname  & " -? Display help (what you are seeing now)" & vbCrLf
                    sTxt = sTxt + "" & vbCrLf
                    sTxt = sTxt + "More info or questions at http://inzi.com" & vbCrLf


                    Wscript.Echo sTxt

                    WScript.Quit

        Else
            Set wshShell = CreateObject( "WScript.Shell" )
            sPath = wshShell.ExpandEnvironmentStrings( "%PATH%" )
            thePath = Split(sPath,";")

            If bInAlphabetical Then
                If bReverseSort Then
                    sDirection = "DESC"
                End If

                thePath = BubbleSort(thePath, sDirection)
            End if


            For Each item In thePath
                WScript.Echo item
            Next
            Set wshShell = Nothing
        End if
    Else
        'Nothing, echo the path.

        Set wshShell = CreateObject( "WScript.Shell" )
        WScript.Echo wshShell.ExpandEnvironmentStrings( "%PATH%" )
        Set wshShell = Nothing

    End If
风为裳 2024-11-03 07:25:06

原始版本

这是一种仅执行多个字符替换的方法,它可以正确处理以分号分隔的可选引用项目的列表,这些项目在引用时甚至可能自己包含分号。空列表项也能正确处理。成功的关键是能够区分引号内的分号(属于列表项的一部分)和引号内的分号(构成分隔符):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here (sample taken from https://stackoverflow.com/a/5472168):
set "BUFFER=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"
rem set "BUFFER="caret ^^";"bang !";"caret ^^ bang !""

set BUFFER & echo/

setlocal EnableDelayedExpansion
rem // Replace `^` by `^@` to avoid sequences of `^`:
set "BUFFER=!BUFFER:^=^@!"

rem // Escape special characters `^`, `&`, `<`, `>`, `|`:
set "BUFFER=!BUFFER:^=^^!"
set "BUFFER=!BUFFER:&=^&!"
set "BUFFER=!BUFFER:<=^<!"
set "BUFFER=!BUFFER:>=^>!"
set "BUFFER=!BUFFER:|=^|!"

rem // Escape `;`:
set "BUFFER=!BUFFER:;=^;!"

rem // Expand immediately, so escaping is processed for unquoted portions:
endlocal & set ^"BUFFER=%BUFFER%^"
setlocal EnableDelayedExpansion

rem // Escape `^`, `&`, `<`, `>`, `|`, hence quoted portions become double-escaped:
set "BUFFER=!BUFFER:^=^^!"
set "BUFFER=!BUFFER:&=^&!"
set "BUFFER=!BUFFER:<=^<!"
set "BUFFER=!BUFFER:>=^>!"
set "BUFFER=!BUFFER:|=^|!"

rem /* Replace `^^^` by `^` in order to resolve double-escaping;
rem    at this point, quoted `;` are represented by the sequence `^^;`: */
set "BUFFER=!BUFFER:^^^=^!"

rem // Escape `"`, so everything appears unquoted then:
set "BUFFER=!BUFFER:"=^^"!"

rem /* Expand immediately, so escaping is again processed;
rem    at this point, originally quoted `;` are represented by `^;`: */
set "BUFFER=!BUFFER:^=^^^!"
set ^"BUFFER=%BUFFER:!=^^!%^" !

rem // Remove all `"` from string:
set "BUFFER=!BUFFER:"=!^"

rem // Enclose whole string in `""` and replace each `;` by `";"`:
set ^"BUFFER="!BUFFER:;=";"!"^"

rem // Replace `^";"` (which are originally quoted `;`) by `;`:
set "BUFFER=!BUFFER:^";"=;!"

rem // Finally revert initial replacement of `^` by `^@`:
set "BUFFER=!BUFFER:^@=^!"

set BUFFER & echo/

rem // Eventually return the list items:
for %%I in (!BUFFER!) do (
    if "!!"=="" endlocal & rem // (technique taken from https://stackoverflow.com/a/7940444)
    echo(%%~I
)

endlocal
exit /B

改进版本

这是一种执行较少替换的改进方法:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here:
set "BUFFER="caret ^^";"bang !";"caret ^^; ^& bang !";caret ^;bang !;caret ^@ & bang !"

set BUFFER & echo/

setlocal EnableDelayedExpansion
rem // Replace `^` by `^@` to avoid sequences of `^`:
set "BUFFER=!BUFFER:^=^@!"

rem // Double-escape special characters `^`, `&`, `<`, `>`, `|`:
set "BUFFER=!BUFFER:^=^^^^!"
set "BUFFER=!BUFFER:&=^^^&!"
set "BUFFER=!BUFFER:<=^^^<!"
set "BUFFER=!BUFFER:>=^^^>!"
set "BUFFER=!BUFFER:|=^^^|!"

rem // Replace `;` by `^^;`:
set "BUFFER=!BUFFER:;=^^;!"

rem // Expand immediately, so escaping is processed for unquoted portions:
endlocal & set ^"BUFFER=%BUFFER%^"
setlocal EnableDelayedExpansion

rem /* Replace `^^^` by `^` in order to resolve double-escaping; at this point,
rem    quoted `;` are represented by the sequence `^^;`, unquoted ones by `^;`: */
set "BUFFER=!BUFFER:^^^=^!"

rem // Escape `"`, so everything appears unquoted then:
set "BUFFER=!BUFFER:"=^^"!"

rem /* Expand immediately, so escaping is again processed;
rem    at this point, originally quoted `;` are represented by `^;`: */
set "BUFFER=!BUFFER:^=^^^!"
set ^"BUFFER=%BUFFER:!=^^!%^" !

rem // Remove all `"` from string:
set "BUFFER=!BUFFER:"=!^"

rem // Enclose whole string in `""` and replace each `;` by `";"`:
set ^"BUFFER="!BUFFER:;=";"!"^"

rem // Replace `^";"` (which are originally quoted `;`) by `;`:
set "BUFFER=!BUFFER:^";"=;!"

rem // Finally revert initial replacement of `^` by `^@`:
set "BUFFER=!BUFFER:^@=^!"

set BUFFER & echo/

rem // Eventually return the list items:
for %%I in (!BUFFER!) do (if "!!"=="" endlocal) & echo(%%~I

endlocal
exit /B

Original Version

This is an approach that only performs several character substitutions, which can properly handle a semicolon-separated list of optionally quoted items, which, when quoted, may even contain semicolons on their own. Empty list items are also dealt with correctly. The key to success is to become able to distinguish between semicolons within quotes (which are part of a list item) and those not (which constitute the separators):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here (sample taken from https://stackoverflow.com/a/5472168):
set "BUFFER=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"
rem set "BUFFER="caret ^^";"bang !";"caret ^^ bang !""

set BUFFER & echo/

setlocal EnableDelayedExpansion
rem // Replace `^` by `^@` to avoid sequences of `^`:
set "BUFFER=!BUFFER:^=^@!"

rem // Escape special characters `^`, `&`, `<`, `>`, `|`:
set "BUFFER=!BUFFER:^=^^!"
set "BUFFER=!BUFFER:&=^&!"
set "BUFFER=!BUFFER:<=^<!"
set "BUFFER=!BUFFER:>=^>!"
set "BUFFER=!BUFFER:|=^|!"

rem // Escape `;`:
set "BUFFER=!BUFFER:;=^;!"

rem // Expand immediately, so escaping is processed for unquoted portions:
endlocal & set ^"BUFFER=%BUFFER%^"
setlocal EnableDelayedExpansion

rem // Escape `^`, `&`, `<`, `>`, `|`, hence quoted portions become double-escaped:
set "BUFFER=!BUFFER:^=^^!"
set "BUFFER=!BUFFER:&=^&!"
set "BUFFER=!BUFFER:<=^<!"
set "BUFFER=!BUFFER:>=^>!"
set "BUFFER=!BUFFER:|=^|!"

rem /* Replace `^^^` by `^` in order to resolve double-escaping;
rem    at this point, quoted `;` are represented by the sequence `^^;`: */
set "BUFFER=!BUFFER:^^^=^!"

rem // Escape `"`, so everything appears unquoted then:
set "BUFFER=!BUFFER:"=^^"!"

rem /* Expand immediately, so escaping is again processed;
rem    at this point, originally quoted `;` are represented by `^;`: */
set "BUFFER=!BUFFER:^=^^^!"
set ^"BUFFER=%BUFFER:!=^^!%^" !

rem // Remove all `"` from string:
set "BUFFER=!BUFFER:"=!^"

rem // Enclose whole string in `""` and replace each `;` by `";"`:
set ^"BUFFER="!BUFFER:;=";"!"^"

rem // Replace `^";"` (which are originally quoted `;`) by `;`:
set "BUFFER=!BUFFER:^";"=;!"

rem // Finally revert initial replacement of `^` by `^@`:
set "BUFFER=!BUFFER:^@=^!"

set BUFFER & echo/

rem // Eventually return the list items:
for %%I in (!BUFFER!) do (
    if "!!"=="" endlocal & rem // (technique taken from https://stackoverflow.com/a/7940444)
    echo(%%~I
)

endlocal
exit /B

Improved Version

Here is an improved approach that performs less substitutions:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here:
set "BUFFER="caret ^^";"bang !";"caret ^^; ^& bang !";caret ^;bang !;caret ^@ & bang !"

set BUFFER & echo/

setlocal EnableDelayedExpansion
rem // Replace `^` by `^@` to avoid sequences of `^`:
set "BUFFER=!BUFFER:^=^@!"

rem // Double-escape special characters `^`, `&`, `<`, `>`, `|`:
set "BUFFER=!BUFFER:^=^^^^!"
set "BUFFER=!BUFFER:&=^^^&!"
set "BUFFER=!BUFFER:<=^^^<!"
set "BUFFER=!BUFFER:>=^^^>!"
set "BUFFER=!BUFFER:|=^^^|!"

rem // Replace `;` by `^^;`:
set "BUFFER=!BUFFER:;=^^;!"

rem // Expand immediately, so escaping is processed for unquoted portions:
endlocal & set ^"BUFFER=%BUFFER%^"
setlocal EnableDelayedExpansion

rem /* Replace `^^^` by `^` in order to resolve double-escaping; at this point,
rem    quoted `;` are represented by the sequence `^^;`, unquoted ones by `^;`: */
set "BUFFER=!BUFFER:^^^=^!"

rem // Escape `"`, so everything appears unquoted then:
set "BUFFER=!BUFFER:"=^^"!"

rem /* Expand immediately, so escaping is again processed;
rem    at this point, originally quoted `;` are represented by `^;`: */
set "BUFFER=!BUFFER:^=^^^!"
set ^"BUFFER=%BUFFER:!=^^!%^" !

rem // Remove all `"` from string:
set "BUFFER=!BUFFER:"=!^"

rem // Enclose whole string in `""` and replace each `;` by `";"`:
set ^"BUFFER="!BUFFER:;=";"!"^"

rem // Replace `^";"` (which are originally quoted `;`) by `;`:
set "BUFFER=!BUFFER:^";"=;!"

rem // Finally revert initial replacement of `^` by `^@`:
set "BUFFER=!BUFFER:^@=^!"

set BUFFER & echo/

rem // Eventually return the list items:
for %%I in (!BUFFER!) do (if "!!"=="" endlocal) & echo(%%~I

endlocal
exit /B
荒芜了季节 2024-11-03 07:25:06

这是一个注释良好的脚本,它解析以分号分隔的项目列表,关于每个项目的可选引用,甚至允许其中包含分号的项目(引用时)。由于它使用了 goto 循环,因此它可能不是最快的方法,但它应该可以安全地防止特殊字符:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here (sample taken from https://stackoverflow.com/a/5472168):
set "BUFFER=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"

setlocal EnableDelayedExpansion
echo BUFFER: !BUFFER!
echo/
endlocal

rem // Initialise items counter and clear (pseudo-)array variable:
set /A "IDX=0" & for /F "delims==" %%V in ('set $ITEM[ 2^> nul') do set "%%V="
rem // Utilise a `goto` loop to retrieve all items:
:PARSE_LOOP
rem // Check for availability of further items:
if defined BUFFER (
    rem // Increment items counter:
    set /A "IDX+=1"
    rem // Toggle delayed expansion to avoid troubles with `!`:
    setlocal EnableDelayedExpansion
    rem // Put current items counter into a `for` meta-variable:
    for %%I in (!IDX!) do (
        rem // Check whether current item begins with `"`, hence it is quoted:
        if "!BUFFER:~,1!" == ""^" (
            rem /* This point is reached when current item is quoted, hence
            rem    remove the opening `"` and split off everything past the
            rem    next one (that is the closing `"`) from the items buffer: */
            for /F tokens^=1*^ delims^=^"^ eol^=^" %%A in (";!BUFFER:~1!") do (
                endlocal
                rem // Store current item into array in unquoted manner:
                set "$ITEM[%%I]=%%A" & setlocal EnableDelayedExpansion
                for /F "delims=" %%C in ("$ITEM[%%I]=!$ITEM[%%I]:~1!") do (
                    endlocal & set "%%C" & if not defined $ITEM[%%I] set /A "IDX-=1"
                )
                rem /* Transfer remaining items beyond `endlocal` barrier;
                rem    note that a delimiters-only line still causes `for /F`
                rem    to iterate when there is just `tokens=*`: */
                for /F "tokens=* delims=;" %%D in (";%%B") do set "BUFFER=%%D"
                setlocal EnableDelayedExpansion
            )
        ) else (
            rem /* This point is reached when current item is not quoted,
            rem    hence split items string at next semicolon `;`: */
            for /F "tokens=1* delims=;" %%A in ("!BUFFER!") do (
                endlocal
                rem // Store current (unquoted) item into array:
                set "$ITEM[%%I]=%%A"
                rem // Transfer remaining items beyond `endlocal` barrier:
                set "BUFFER=%%B"
                setlocal EnableDelayedExpansion
            )
        )
    )
    endlocal
    rem // Loop back to retrieve next item:
    goto :PARSE_LOOP
)
rem // Return all retrieved items:
setlocal EnableDelayedExpansion
for /L %%I in (1,1,%IDX%) do echo ITEM %%I: !$ITEM[%%I]!
endlocal

echo/
echo INFO:   %IDX% items
echo ERROR:  %ErrorLevel%

endlocal
exit /B

Here is a well commented script that parses a semicolon-separated list of items, regarding optional quotation of each item and allowing even items with semicolons in them (when quoted). Since it utilises a goto loop it might not be the fastest approach though, but it should be safe against special characters:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define semicolon-separated list here (sample taken from https://stackoverflow.com/a/5472168):
set "BUFFER=foo & bar;baz<>gak;"semi;colons;^&embedded";foo again!;throw (in) some (parentheses);"unmatched ;-)";(too"

setlocal EnableDelayedExpansion
echo BUFFER: !BUFFER!
echo/
endlocal

rem // Initialise items counter and clear (pseudo-)array variable:
set /A "IDX=0" & for /F "delims==" %%V in ('set $ITEM[ 2^> nul') do set "%%V="
rem // Utilise a `goto` loop to retrieve all items:
:PARSE_LOOP
rem // Check for availability of further items:
if defined BUFFER (
    rem // Increment items counter:
    set /A "IDX+=1"
    rem // Toggle delayed expansion to avoid troubles with `!`:
    setlocal EnableDelayedExpansion
    rem // Put current items counter into a `for` meta-variable:
    for %%I in (!IDX!) do (
        rem // Check whether current item begins with `"`, hence it is quoted:
        if "!BUFFER:~,1!" == ""^" (
            rem /* This point is reached when current item is quoted, hence
            rem    remove the opening `"` and split off everything past the
            rem    next one (that is the closing `"`) from the items buffer: */
            for /F tokens^=1*^ delims^=^"^ eol^=^" %%A in (";!BUFFER:~1!") do (
                endlocal
                rem // Store current item into array in unquoted manner:
                set "$ITEM[%%I]=%%A" & setlocal EnableDelayedExpansion
                for /F "delims=" %%C in ("$ITEM[%%I]=!$ITEM[%%I]:~1!") do (
                    endlocal & set "%%C" & if not defined $ITEM[%%I] set /A "IDX-=1"
                )
                rem /* Transfer remaining items beyond `endlocal` barrier;
                rem    note that a delimiters-only line still causes `for /F`
                rem    to iterate when there is just `tokens=*`: */
                for /F "tokens=* delims=;" %%D in (";%%B") do set "BUFFER=%%D"
                setlocal EnableDelayedExpansion
            )
        ) else (
            rem /* This point is reached when current item is not quoted,
            rem    hence split items string at next semicolon `;`: */
            for /F "tokens=1* delims=;" %%A in ("!BUFFER!") do (
                endlocal
                rem // Store current (unquoted) item into array:
                set "$ITEM[%%I]=%%A"
                rem // Transfer remaining items beyond `endlocal` barrier:
                set "BUFFER=%%B"
                setlocal EnableDelayedExpansion
            )
        )
    )
    endlocal
    rem // Loop back to retrieve next item:
    goto :PARSE_LOOP
)
rem // Return all retrieved items:
setlocal EnableDelayedExpansion
for /L %%I in (1,1,%IDX%) do echo ITEM %%I: !$ITEM[%%I]!
endlocal

echo/
echo INFO:   %IDX% items
echo ERROR:  %ErrorLevel%

endlocal
exit /B
べ繥欢鉨o。 2024-11-03 07:25:06

回到这一点,如果您有 node.js 并受到 Bob Stein 回答 的启发python 单行代码。

node -e "console.log(process.env.path.replace(/;/g,'\n'))"

或者更长的方式:

node -e "console.log(process.env.path.split(';').join('\n'))"

我需要找到安装了 git 的行,因此:

node -e "console.log(process.env.path.split(';').filter(str=>str.match(/git/i)).join('\n'))"

Returning to this, in the case you have node.js and inspired by Bob Stein answer for python one-liner.

node -e "console.log(process.env.path.replace(/;/g,'\n'))"

Or longer way:

node -e "console.log(process.env.path.split(';').join('\n'))"

I needed to find lines where git is installed so:

node -e "console.log(process.env.path.split(';').filter(str=>str.match(/git/i)).join('\n'))"
意中人 2024-11-03 07:25:06

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char。 bat:

@echo off

rem Encode `*` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char。 bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & setlocal ENABLEDELAYEDEXPANSION & if "!__STRING__!" == "!__STRING__:**=!" ( exit /b 0 ) else endlocal :LOOP setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=1 delims=*" %%i in (".!__STRING__!") do for /F "eol= tokens=* delims=" %%j in ("!__STRING__:**=!.") do endlocal & set "__STRING__=%%i$2A%%j" & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:~1,-1!") do ^ if not "!__STRING__!" == "!__STRING__:**=!" ( endlocal & set "__STRING__=%%i" & goto LOOP ) else endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_equal_char.bat:

@echo off

rem Encode `=` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char。 bat:

@echo off

rem Encode `*` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

另一种实现几乎包含所有控制字符句柄。

警告

似乎 stackoverflow 错误地处理了复制粘贴中的制表字符(并丢失了 \x01\x04 等其他字符)代码,因此如果直接通过 CTRL+C 复制下面的代码可能不起作用。使用下面的链接直接下载脚本。

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/树/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char。 bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & setlocal ENABLEDELAYEDEXPANSION & if "!__STRING__!" == "!__STRING__:**=!" ( exit /b 0 ) else endlocal :LOOP setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=1 delims=*" %%i in (".!__STRING__!") do for /F "eol= tokens=* delims=" %%j in ("!__STRING__:**=!.") do endlocal & set "__STRING__=%%i$2A%%j" & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:~1,-1!") do ^ if not "!__STRING__!" == "!__STRING__:**=!" ( endlocal & set "__STRING__=%%i" & goto LOOP ) else endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_equal_char.bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do for /F "eol= tokens=1 delims==" %%j in (".!__STRING__!") do endlocal & set "__HEAD__=%%j" & set "__TAIL__=.%%i" & ^ setlocal ENABLEDELAYEDEXPANSION & if "!__HEAD__!" == "!__TAIL__!" ( exit /b 0 ) else endlocal set "__STRING__=" setlocal ENABLEDELAYEDEXPANSION :LOOP if "!__HEAD__!" == "!__TAIL__!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!!__TAIL__:~1!") do endlocal & endlocal & set "__STRING__=%%i" & exit /b 0 set "__OFFSET__=2" & set "__TMP__=!__HEAD__!" & for %%i in (65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do if not "!__TMP__:~%%i,1!" == "" set /A "__OFFSET__+=%%i" & set "__TMP__=!__TMP__:~%%i!" if defined __TAIL__ set "__TAIL__=!__TAIL__:~%__OFFSET__%!" set "__STRING__=!__STRING__!!__HEAD__:~1!$3D" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do for /F "eol= tokens=1 delims==" %%j in (".!__TAIL__!") do for /F "eol= tokens=* delims=" %%k in (".!__TAIL__!") do ^ endlocal & set "__STRING__=%%i" & set "__HEAD__=%%j" & set "__TAIL__=%%k" & setlocal ENABLEDELAYEDEXPANSION goto LOOP

示例:

1.

>
echo_path_var.bat PATH ` `
  1. 使用双引号:
>
set __?PREFIX__=^"
set __?SUFFIX__=^"
echo_path_var.bat PATH
setlocal DISABLEDELAYEDEXPANSION

set AAA=123
rem with tabulation character
set BBB="?a/b/%%c%%!" ^;     "!d! ^; e ; f" ; ; ; !AAA! ^^; g,g ;;; h h

call "%%~dp0echo_var.bat" BBB ` `
call "%%~dp0echo_path_var.bat" BBB ` `
`"?a/b/%c%!" ;   "!d! ^; e ; f" ; ; ; !AAA! ^; g,g ;;; h h`
`"?a/b/%c%!" `
`        "!d! ^; e ; f" `
` `
` `
` !AAA! ^`
` g,g `
` h h`
  1. 使用通配符:
set "A=c:\windows\*.exe"
call "%%~dp0echo_var.bat" A
call "%%~dp0echo_pathglob_var.bat" A
c:\windows\*.exe
c:\windows\bfsvc.exe
c:\windows\explorer.exe
c:\windows\HelpPane.exe
c:\windows\hh.exe
c:\windows\notepad.exe
c:\windows\py.exe
c:\windows\pyw.exe
c:\windows\regedit.exe
c:\windows\splwow64.exe
c:\windows\winhlp32.exe
c:\windows\write.exe
set "A=c:\windows\??.exe"
call "%%~dp0echo_var.bat" A
call "%%~dp0echo_pathglob_var.bat" A
c:\windows\??.exe
c:\windows\hh.exe
c:\windows\py.exe

优点

  • 使用 for %%i in (...) 语句解析路径列表值以解析 VALUE;VALUE"VALUE";"VALUE"相同。
  • 按原样打印空格和制表字符。
  • 忽略 ; 序列(例如:;;;)并将其视为单个字符。
  • 可以按原样使用通配符或打印,无需转义(在 Windows 中,路径不应包含 *? 字符)。

缺点

  • 需要单独修剪前导和尾随空白字符。

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char.bat:

@echo off

rem Encode `*` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char.bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & setlocal ENABLEDELAYEDEXPANSION & if "!__STRING__!" == "!__STRING__:**=!" ( exit /b 0 ) else endlocal :LOOP setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=1 delims=*" %%i in (".!__STRING__!") do for /F "eol= tokens=* delims=" %%j in ("!__STRING__:**=!.") do endlocal & set "__STRING__=%%i$2A%%j" & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:~1,-1!") do ^ if not "!__STRING__!" == "!__STRING__:**=!" ( endlocal & set "__STRING__=%%i" & goto LOOP ) else endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_equal_char.bat:

@echo off

rem Encode `=` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char.bat:

@echo off

rem Encode `*` character.

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem CAUTION:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

@echo off

rem Decode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not decode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

@echo off

rem Encode these characters:
rem  $          - encode character
rem  |&()<>     - control flow characters
rem  '`^%!+     - escape or sequence expand characters (`+` is a unicode codepoint sequence character in 65000 code page)
rem  ?*         - globbing characters in the `for ... %%i in (...)` expression or in a command line
rem  <space>,=  - separator characters in the `for ... %%i in (...)` expression or in a command line

rem Does not encode these characters:
rem  "          - quotes in the `for ... %%i in (...)` expression or or in a command line
rem  ;          - separator characters in the `for ... %%i in (...)` expression or in a command line

rem CAUTION:
rem   The delayed expansion feature must be disabled before this script call: `setlocal DISABLEDELAYEDEXPANSION`, otherwise
rem   the `!` character will be expanded.
rem

rem NOTE:
rem   Character `

Yet another implementation with almost all control characters handle.

CAUTION:

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Use the link below to directly download the scripts.

https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_path_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_pathglob_var.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/echo_var.bat

echo_path_var.bat:

@echo off

rem USAGE:
rem   echo_path_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_pathglob_var.bat:

@echo off

rem USAGE:
rem   echo_pathglob_var.bat <VAR> [<PREFIX> [<SUFFIX>]]

rem <VAR>: VALUE1;VALUE2;...;VALUEN

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

if /i not "%__?VAR__%" == "__STRING__" (
  set "__STRING__="
  setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!%__?VAR__%!") do endlocal & set "__STRING__=%%i"
)

call "%%~dp0%%encode\encode_pathlist_chars_glob.bat"

setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & for %%j in (%%i) do set "__STRING__=%%j" & ^
call "%%~dp0%%encode\decode_pathlist_chars_glob.bat" & ^
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%k in ("!__?PREFIX__!!__STRING__!!__?SUFFIX__!") do endlocal & echo.%%k

exit /b 0

echo_var.bat:

@echo off

rem USAGE:
rem   echo_var.bat <Var> [<Prefix> [<Suffix>]]

setlocal DISABLEDELAYEDEXPANSION

set "__?VAR__=%~1"
if not defined __?PREFIX__ set "__?PREFIX__=%~2"
if not defined __?SUFFIX__ set "__?SUFFIX__=%~3"

if not defined __?VAR__ exit /b 255
if not defined %__?VAR__% exit /b 1

rem safe echo
setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__?PREFIX__!!%__?VAR__%!!__?SUFFIX__!") do endlocal & echo.%%i
exit /b 0

encode_pathlist_chars.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_asterisk_char.bat" & call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__:?=$3F!" & set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_pathlist_chars_glob.bat:

already does encode. rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:$=$24!") do endlocal & set "__STRING__=%%i" rem NOTE: restore the quote character later setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:!=$21%" call "%%~dp0encode_equal_char.bat" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:|=$7C!" & set "__STRING__=!__STRING__:&=$26!" & set "__STRING__=!__STRING__:(=$28!" & set "__STRING__=!__STRING__:)=$29!" & ^ set "__STRING__=!__STRING__:<=$3C!" & set "__STRING__=!__STRING__:>=$3E!" & set "__STRING__=!__STRING__:'=$27!" & set "__STRING__=!__STRING__:`=$60!" & ^ set "__STRING__=!__STRING__:^=$5E!" & set "__STRING__=!__STRING__:%%=$25!" & set "__STRING__=!__STRING__:+=$2B!" & ^ set "__STRING__=!__STRING__: =$20!" & set "__STRING__=!__STRING__:,=$2C!" & ^ set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars.bat:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$3F=?!" & set "__STRING__=!__STRING__:$2A=*!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

decode_pathlist_chars_glob:

already does decode. rem if not defined __STRING__ exit /b 0 rem NOTE: restore the quote character later setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:"=$22!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" set "__STRING__=%__STRING__:$21=!%" setlocal ENABLEDELAYEDEXPANSION & set "__STRING__=!__STRING__:$22="!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & set "__STRING__=%%i" setlocal ENABLEDELAYEDEXPANSION & ^ set "__STRING__=!__STRING__:$7C=|!" & set "__STRING__=!__STRING__:$26=&!" & set "__STRING__=!__STRING__:$28=(!" & set "__STRING__=!__STRING__:$29=)!" & ^ set "__STRING__=!__STRING__:$3C=<!" & set "__STRING__=!__STRING__:$3E=>!" & set "__STRING__=!__STRING__:$27='!" & set "__STRING__=!__STRING__:$60=`!" & ^ set "__STRING__=!__STRING__:$5E=^!" & set "__STRING__=!__STRING__:$25=%%!" & set "__STRING__=!__STRING__:$2B=+!" & ^ set "__STRING__=!__STRING__:$20= !" & set "__STRING__=!__STRING__:$2C=,!" & set "__STRING__=!__STRING__:$3D==!" & set "__STRING__=!__STRING__:$24=$!" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_asterisk_char.bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & setlocal ENABLEDELAYEDEXPANSION & if "!__STRING__!" == "!__STRING__:**=!" ( exit /b 0 ) else endlocal :LOOP setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=1 delims=*" %%i in (".!__STRING__!") do for /F "eol= tokens=* delims=" %%j in ("!__STRING__:**=!.") do endlocal & set "__STRING__=%%i$2A%%j" & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__:~1,-1!") do ^ if not "!__STRING__!" == "!__STRING__:**=!" ( endlocal & set "__STRING__=%%i" & goto LOOP ) else endlocal & endlocal & set "__STRING__=%%i" exit /b 0

encode_equal_char.bat:

must be encoded separately BEFORE this script call! rem if not defined __STRING__ exit /b 0 setlocal DISABLEDELAYEDEXPANSION & ^ setlocal ENABLEDELAYEDEXPANSION & for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do for /F "eol= tokens=1 delims==" %%j in (".!__STRING__!") do endlocal & set "__HEAD__=%%j" & set "__TAIL__=.%%i" & ^ setlocal ENABLEDELAYEDEXPANSION & if "!__HEAD__!" == "!__TAIL__!" ( exit /b 0 ) else endlocal set "__STRING__=" setlocal ENABLEDELAYEDEXPANSION :LOOP if "!__HEAD__!" == "!__TAIL__!" for /F "eol= tokens=* delims=" %%i in ("!__STRING__!!__TAIL__:~1!") do endlocal & endlocal & set "__STRING__=%%i" & exit /b 0 set "__OFFSET__=2" & set "__TMP__=!__HEAD__!" & for %%i in (65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do if not "!__TMP__:~%%i,1!" == "" set /A "__OFFSET__+=%%i" & set "__TMP__=!__TMP__:~%%i!" if defined __TAIL__ set "__TAIL__=!__TAIL__:~%__OFFSET__%!" set "__STRING__=!__STRING__!!__HEAD__:~1!$3D" & ^ for /F "eol= tokens=* delims=" %%i in ("!__STRING__!") do for /F "eol= tokens=1 delims==" %%j in (".!__TAIL__!") do for /F "eol= tokens=* delims=" %%k in (".!__TAIL__!") do ^ endlocal & set "__STRING__=%%i" & set "__HEAD__=%%j" & set "__TAIL__=%%k" & setlocal ENABLEDELAYEDEXPANSION goto LOOP

Examples:

1.

>
echo_path_var.bat PATH ` `
  1. To use with double quotes:
>
set __?PREFIX__=^"
set __?SUFFIX__=^"
echo_path_var.bat PATH
setlocal DISABLEDELAYEDEXPANSION

set AAA=123
rem with tabulation character
set BBB="?a/b/%%c%%!" ^;     "!d! ^; e ; f" ; ; ; !AAA! ^^; g,g ;;; h h

call "%%~dp0echo_var.bat" BBB ` `
call "%%~dp0echo_path_var.bat" BBB ` `
`"?a/b/%c%!" ;   "!d! ^; e ; f" ; ; ; !AAA! ^; g,g ;;; h h`
`"?a/b/%c%!" `
`        "!d! ^; e ; f" `
` `
` `
` !AAA! ^`
` g,g `
` h h`
  1. With globbing:
set "A=c:\windows\*.exe"
call "%%~dp0echo_var.bat" A
call "%%~dp0echo_pathglob_var.bat" A
c:\windows\*.exe
c:\windows\bfsvc.exe
c:\windows\explorer.exe
c:\windows\HelpPane.exe
c:\windows\hh.exe
c:\windows\notepad.exe
c:\windows\py.exe
c:\windows\pyw.exe
c:\windows\regedit.exe
c:\windows\splwow64.exe
c:\windows\winhlp32.exe
c:\windows\write.exe
set "A=c:\windows\??.exe"
call "%%~dp0echo_var.bat" A
call "%%~dp0echo_pathglob_var.bat" A
c:\windows\??.exe
c:\windows\hh.exe
c:\windows\py.exe

Pros:

  • Use the for %%i in (...) statement to parse a path list value to parse VALUE;VALUE the same way as "VALUE";"VALUE".
  • Prints spaces and tabulation characters as is.
  • Ignores sequence of ; (ex: ;;;) and treats it a single character.
  • Can use globbing or print as is, no need to escape (in windows a path should not contain * and ? characters).

Cons:

  • Needs to separately trim leading and trailing white space characters.
夏见 2024-11-03 07:25:06

该脚本将使用 JREPL.bat 将当前 Windows 路径解析为 txt 文件。
它的灵感来自 dbenham 的上述帖子。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:::: THIS SCRIPT WILL PARSE THE CURRENT WINDOWS PATH INTO AT TXT FILE ::::
:::: EACH FOLDER FOUND IN PATH WILL BE ON A SEPARATE LINE ::::

:::: SCRIPT INSTRUCTIONS ::::
:: PLACE JREPL.bat IN C:\WINDOWS\SYSTEM32 FOLDER OR CUSTOM LOCATION OF YOUR CHOOSING ::
:: IF PLACED IN CUSTOM FOLDER YOU MUST LINK IT TO WINDOWS PATH ::
:: YOU CAN ACCESS WINDOWS PATH BY RUNNING THE BELOW COMMAND IN CMD.EXE ::
:: Rundll32 sysdm.cpl,EditEnvironmentVariables ::
:: DOWNLOAD JREPL.bat https://www.dostips.com/forum/viewtopic.php?t=6044 ::

:: SET WORKING DIRECTORY ::
CD /D "C:\WINDOWS\SYSTEM32"

:: UNCOMMENT LINE BELOW AND SET YOUR JREPL.bat SAVED FOLDER PATH IF YOU HAVE A BACKUP COPY ::
:: SET JOUT=<FOLDER PATH>

:: SET OUTPUT FILE ::
    SET FOUT=%USERPROFILE%\DESKTOP\PARSE.TXT

:: SET FILE TO SEARCH FOR ::
:: THIS SEARCHES FOR JREPL.BAT IN THE CURRENT WORKING DIR ::
    SET I=JREPL.BAT

:: SET CONTROL FILE TO CHECK AGAINST ::
:: THIS IS FOR DEBUGGING PURPOSES AND SHOULD NOT BE CHANGED OTHERWISE ::
    SET J=JREPL.BAT

:::: START SCRIPT ::::
    SET RESULT=
    FOR /F "DELIMS=" %%A IN ('DIR /B /O:N ^| FINDSTR /S /I "%I%" %TMP_RESULT_FILE%') DO (
    SET RESULT=%%A
    )

IF /I !RESULT! EQU %J% (
ECHO !RESULT! ^^!^^!EXISTS^^!^^!
    TIMEOUT 3 >NUL
    CALL :FOUND
    GOTO END
) ELSE (
    GOTO NOTFOUND
)
    GOTO ERROR

:FOUND
    FOR %%G IN (%I%) DO (
    %%G "([^;\Q]+|\Q.*?(\Q|$))+" $0 /X /JMATCH /S PATH>>%FOUT%
    CLS && ECHO.
ECHO %I% ^^!^^!EXECUTED SUCCESSFULLY^^!^^!
    TIMEOUT /T 3 >NUL
    EXPLORER "%FOUT%"
    GOTO END
( ELSE )
    GOTO ERROR
)

:NOTFOUND
    ECHO %I% ^^!^^!NOT FOUND^^!^^!
    TIMEOUT 3 >NUL
    CLS && ECHO.
:: UNCOMMENT THE LINES BELOW TO OPEN JREPL.BAT SAVE FOLDER IF AVAILABLE ::
    :: ECHO ^^!^^!OPENING^^!^^! %I%'S SAVED FOLDER
  :: TIMEOUT 3 >NUL
    :: EXPLORER "%JOUT%"
  GOTO END
    ( ELSE )
    GOTO ERROR
    )

:ERROR
    CLS && ECHO.
    ECHO ^^!^^!ERROR RUNNING^^!^^! %I%
    TIMEOUT 3 >NUL
    CLS && ECHO.
    ECHO ^^!^^!PLEASE FIX SCRIPT^^!^^! ::::::::::::::::::
    TIMEOUT 3 >NUL

:END
    ENDLOCAL && EXIT /B

This script will parse the current windows path into at txt file using JREPL.bat.
It was inspired by the above post by dbenham.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:::: THIS SCRIPT WILL PARSE THE CURRENT WINDOWS PATH INTO AT TXT FILE ::::
:::: EACH FOLDER FOUND IN PATH WILL BE ON A SEPARATE LINE ::::

:::: SCRIPT INSTRUCTIONS ::::
:: PLACE JREPL.bat IN C:\WINDOWS\SYSTEM32 FOLDER OR CUSTOM LOCATION OF YOUR CHOOSING ::
:: IF PLACED IN CUSTOM FOLDER YOU MUST LINK IT TO WINDOWS PATH ::
:: YOU CAN ACCESS WINDOWS PATH BY RUNNING THE BELOW COMMAND IN CMD.EXE ::
:: Rundll32 sysdm.cpl,EditEnvironmentVariables ::
:: DOWNLOAD JREPL.bat https://www.dostips.com/forum/viewtopic.php?t=6044 ::

:: SET WORKING DIRECTORY ::
CD /D "C:\WINDOWS\SYSTEM32"

:: UNCOMMENT LINE BELOW AND SET YOUR JREPL.bat SAVED FOLDER PATH IF YOU HAVE A BACKUP COPY ::
:: SET JOUT=<FOLDER PATH>

:: SET OUTPUT FILE ::
    SET FOUT=%USERPROFILE%\DESKTOP\PARSE.TXT

:: SET FILE TO SEARCH FOR ::
:: THIS SEARCHES FOR JREPL.BAT IN THE CURRENT WORKING DIR ::
    SET I=JREPL.BAT

:: SET CONTROL FILE TO CHECK AGAINST ::
:: THIS IS FOR DEBUGGING PURPOSES AND SHOULD NOT BE CHANGED OTHERWISE ::
    SET J=JREPL.BAT

:::: START SCRIPT ::::
    SET RESULT=
    FOR /F "DELIMS=" %%A IN ('DIR /B /O:N ^| FINDSTR /S /I "%I%" %TMP_RESULT_FILE%') DO (
    SET RESULT=%%A
    )

IF /I !RESULT! EQU %J% (
ECHO !RESULT! ^^!^^!EXISTS^^!^^!
    TIMEOUT 3 >NUL
    CALL :FOUND
    GOTO END
) ELSE (
    GOTO NOTFOUND
)
    GOTO ERROR

:FOUND
    FOR %%G IN (%I%) DO (
    %%G "([^;\Q]+|\Q.*?(\Q|$))+" $0 /X /JMATCH /S PATH>>%FOUT%
    CLS && ECHO.
ECHO %I% ^^!^^!EXECUTED SUCCESSFULLY^^!^^!
    TIMEOUT /T 3 >NUL
    EXPLORER "%FOUT%"
    GOTO END
( ELSE )
    GOTO ERROR
)

:NOTFOUND
    ECHO %I% ^^!^^!NOT FOUND^^!^^!
    TIMEOUT 3 >NUL
    CLS && ECHO.
:: UNCOMMENT THE LINES BELOW TO OPEN JREPL.BAT SAVE FOLDER IF AVAILABLE ::
    :: ECHO ^^!^^!OPENING^^!^^! %I%'S SAVED FOLDER
  :: TIMEOUT 3 >NUL
    :: EXPLORER "%JOUT%"
  GOTO END
    ( ELSE )
    GOTO ERROR
    )

:ERROR
    CLS && ECHO.
    ECHO ^^!^^!ERROR RUNNING^^!^^! %I%
    TIMEOUT 3 >NUL
    CLS && ECHO.
    ECHO ^^!^^!PLEASE FIX SCRIPT^^!^^! ::::::::::::::::::
    TIMEOUT 3 >NUL

:END
    ENDLOCAL && EXIT /B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文