The curious case of the missing exclamation mark in CMD files

前端 未结 3 1417
北荒
北荒 2021-01-06 08:02

I have whittled down a more complex CMD script to the essentials. It reads an input file line by line, unquotes it (if quoted) and writes it out to another CMD file.

3条回答
  •  梦毁少年i
    2021-01-06 08:37

    Try this:

    @echo off
    if exist bang2.cmd del bang2.cmd
    for /f "tokens=*" %%a in (bang1.txt) do call :doit1 %%a
    exit /b
    
    :doit1
    set "P1=%1"
    if %P1%.==. exit /b
    call :unquotex P1 %P1%
    echo>>bang2.cmd echo P1:[%P1%]
    exit /b
    
    :unquotex
    set "%1=%~2"
    exit /b
    

    Using parameters, you can get the version without quotes using %~1 instead of %1. If %1 contains "hello world" for example, then %~1 contains hello world. This allows for an easier unquoting mechanism, removing the need for delayed expansion.

提交回复
热议问题