How to escape reserved characters in Windows batch files

后端 未结 2 682
粉色の甜心
粉色の甜心 2020-12-21 21:01

I\'m trying to process a list of file names but don\'t know how to \"escape\" the command line to produce consistent output regardless of whether the name contains plain tex

相关标签:
2条回答
  • 2020-12-21 21:35

    try this:

    @echo off
    setlocal EnableDelayedExpansion
    set /a COUNT=0
    for /f "tokens=*" %%g in ('dir /b *.txt') do (
      set /a COUNT=!COUNT! + 1
      <nul set /p=!count!
      setlocal disableDelayedExpansion & (
        (echo( %%g)
      )
      setlocal enableDelayedExpansion
    
    )
    

    You can turn on/off the delayed expansion within the for loop for proper echoing the special chars. <nul set /p=%%g prints a string without new line at the end - which is needed for proper echoing of the count which should be in delayed expansion part.And it will work faster without subroutine.

    Edited to print the count first.

    0 讨论(0)
  • 2020-12-21 21:48

    I think this will work as well.

    @echo off
    
    set /a COUNT=0
    for /f "tokens=*" %%g in ('dir /b *.txt') do (
        set /a COUNT+=1
        set "file=%%g"
        call :LIST file COUNT
    )
    pause
    exit
    
    :LIST
    setlocal enabledelayedexpansion
    echo !%2! !%1!
    endlocal
    goto :EOF
    

    Output

    1 01 A Test.txt
    2 02 & Test.txt
    3 03 ! test.txt
    4 04 % test.txt
    Press any key to continue . . .
    
    0 讨论(0)
提交回复
热议问题