是否有一些东西可以加倍输出%字符?

时间:2010-09-15 03:09:30

标签: windows batch-file cmd char escaping

BATCH文件中是否有某些内容可以加倍%字符?

test.txt INPUT文件包含以下内容

Hello World

The filename is : filename with spaces and 50% percentages signs.txt
The %~nf removes extenstions
Is there something to double a % charactor?
As I would like 50% to be outputed as 50%% 
because the output of this batch is to create input for another batch.

批处理文件。

@echo off
setlocal EnableDelayedExpansion

echo. > test2.txt

for /f "tokens=*" %%a in ('type test.txt') do (
    if "%%a"=="Update=Yes" (
        @echo Update=No >> test2.txt
    ) else if "%%a"=="Update=No" (
        @echo Update=Yes >> test2.txt
    ) else if "%%a"=="" (
        rem Questions TWO
        rem print a blank line doesn't work
        @echo. >> test2.txt
    ) else (
        set tmpvar=%%a
        set str=!tmpvar:%%=%%%%!
        echo !str!  >> test2.txt
    )
)

start test2.txt

test2.txt输出是(注意Hello World下的空行)

Hello World  
The filename is : filename with spaces and 50%% percentages signs.txt  
The %%~nf removes extenstions  
Is there something to double a %% charactor?  
As I would like 50%% to be outputed as 50%%%%   
because the output of this batch is to create input for another batch.  

问题二:如何检查%% a是否为空行?

这确实有效,但需要400行;有没有办法使用For LOOP?

@echo off   

set STR2=ON%%E

echo This is STR2 %STR2%

IF "%STR2:~0,1%"=="%%" (set STR3=%STR3%%%%%) else  set STR3=%STR3%%STR2:~0,1%

IF "%STR2:~1,1%"=="%%" (set STR3=%STR3%%%%%) else  set STR3=%STR3%%STR2:~1,1%

IF "%STR2:~2,1%"=="%%" (set STR3=%STR3%%%%%) else  set STR3=%STR3%%STR2:~2,1%

IF "%STR2:~3,1%"=="%%" (set STR3=%STR3%%%%%) else  set STR3=%STR3%%STR2:~3,1%

echo This is STR3 %STR3%

pause

1 个答案:

答案 0 :(得分:3)

回答你的问题:

1)在基本脚本中,这可能是进行字符串交换的最简单方法。

2)“/ n”与空行不匹配。空白行只是“”。

3)“tokens = *”将%%中的整行放在变量中,因此echo %%a是回显整行的最佳方式。

4)你需要使用另一个变量来做这样的%倍增:

) else (
    set tmpvar=%%a
    set str=!tmpvar:%%=%%%%!
    echo !str!
)