views:

98

answers:

1

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
+1  A: 

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!
)
Andrew Cooper
Can you update your question with a sample of your source file and what you're getting now?
Andrew Cooper
Also, if you like an answer, please accept it.
Andrew Cooper
Add a line after @echo off that says "SETLOCAL ENABLEDELAYEDEXPANSION" (without the quotes).
Andrew Cooper
I'm pretty sure XP does have this. You can make sure by typing "cmd /?" (wihtout the quotes) at the command line. The help should mention delayed expansion.
Andrew Cooper
Reading through the cmd.exe helpfile on Windows XP, it appears that delayed expansion is not enabled by default, and that the CetLocal Enable... command isn't recognised. To get this working you'll either need to start cmd.exe with the /V:ON switch orset the following registry setting to 0x01: HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion
Andrew Cooper
The for command skips blank lines. I'm not sure there's much you can do about it in a script.
Andrew Cooper