Escape special character in a windows batch

前端 未结 2 1045
陌清茗
陌清茗 2021-01-23 20:06

I have a batch file that receive a path as first argument. The path is always composed with specials characters like ^,é or è.

The

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 20:20

    You need to escape it twice - once when you input the path as an argument to the batch script, and again when echo'ing it:

    caret_input.bat:

    echo %1
    

    Double-escaped (notice how it's already escaped when the batch file starts outputting):

    C:\>caret_input.bat my\path^^^^is\special
    
    C:\>echo my\path^is\special
    my\path^is\special
    

    If you were to use a string with a special character inside the batch file, your method of escaping it just once would work just fine:

    caret_escape.bat:

    echo my\path^^is\special
    

    and the output

    C:\>echo my\path^is\special
    my\path^is\special
    

提交回复
热议问题