2010-09-15 67 views
0

BATCH文件中是否存在會使%字符加倍的內容?有沒有東西可以使輸出的字符加倍?

test.txt的輸入文件包含以下

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 

回答

3

回答您的問題:

1)在一個基本的腳本這可能是做一個字符串交換的最簡單方法。

2)「/ n」不匹配空行。一個空白行將只是「」。 3)「tokens = *」將整行放入%%變量中,所以echo %%a是回覆整行的最佳方式。

4)你需要使用另一個變量來做%翻一番這樣的:

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

你能和你的源文件的樣本,你現在在說什麼更新你的問題? – 2010-09-17 19:41:02

+1

在@echo off之後添加一行,表示「SETLOCAL ENABLEDELAYEDEXPANSION」(不帶引號)。 – 2010-09-19 04:03:15

+0

我很確定XP確實有這個。你可以通過輸入「cmd /?」來確定(沒有引號)在命令行。該幫助應該提及延期擴張。 – 2010-09-19 04:08:48