Is there a way to pass a percentage (%) into a routine?

dos batch file

The input file has three entries:

HOW NOW BROWN COW 

JACK AND JILL 

100% JUST YOU & ME 

The script looks like this:

@echo off

set infile=e:\file.txt
set outfile=e:\outfile.txt

for /F "tokens=*" %%H in (%infile%) do (
    call :Loop "%%H"
)

exit /b 0

:Loop
    for %%a in (%*) do echo %%~a >> %outfile%
exit /b 0

CONCLUSION:

E:\>foo2

E:\>type outfile.txt
HOW NOW BROWN COW

JACK AND JILL

100 JUST YOU & ME

The% symbol is gone! Do you know, why?

+3
source share
2 answers

This is a special character, if you make 100 %%, it will be correctly displayed at 100%. I would suggest that there is a way to handle this gracefully, but you might have to play with some.

EDIT: Digging a bit and stumbled upon BatchSubstitute.bat at http://www.dostips.com/DtCodeBatchFiles.php

I adapted to this, and I think that this should work for you - if someone fully understands what is happening, I would like to hear it - I have only a gauge concept:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION


set infile=e:\file.txt
set outfile=e:\outfile.txt

for /f "tokens=1,* delims=]" %%A in ('"type %infile%|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line%%" 
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >> %outfile% 
    ) ELSE echo. >> %outfile%
)
+2

call :loop "%%H", call , , % .

, call :func myVar.
call , - .

for-loop - ! carets ^.
, .

set infile=e:\file.txt
set outfile=e:\outfile.txt
setlocal DisableDelayedExpansion
for /f "delims=" %%A in ('"findstr /n ^^ %infile%"') do (
   set "line=%%A"
   setlocal EnableDelayedExpansion

   set "line=!line:*:=!"
   (echo(!line!)>>%outfile%

   rem CALL :func line
   endlocal
)
0

Source: https://habr.com/ru/post/1789052/


All Articles