Edoctoor
Edoctoor

Reputation: 181

Is there something to double output a % character?

Is there something in a BATCH file that would double a % character?

The test.txt INPUT file contains the following

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.

The batch file.

@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

The test2.txt output is (Note No Blank Line under 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.  

Problem Two: How do I check if %%a is an empty line?

This does work but would require 400 lines; is there a way to do it using a 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

Upvotes: 0

Views: 197

Answers (1)

Andrew Cooper
Andrew Cooper

Reputation: 32576

To answer your questions:

1) In a basic script that's probably the easiest way to do a string swap.

2) "/n" won't match a blank line. A blank line will be just "".

3) "tokens=*" puts the whole line in the %%a variable, so echo %%a is the best way to just echo the whole line.

4) You'll need to use another variable to do the % doubling like this:

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

Upvotes: 3

Related Questions