Windows Batch - Print line file containing exclamation mark

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CharlesMM
Posts: 4
Joined: 06 Apr 2020 06:07

Windows Batch - Print line file containing exclamation mark

#1 Post by CharlesMM » 06 Apr 2020 06:14

Hello everybody,
I developed a batch that cycles through the contents of the file and prints the value of the lines in another file, but I'm having problems with lines containing the "!" character.

Below is a simplified example of what I am doing and the result obtained:
Contents of the one-line example file "test_file.txt":

Code: Select all

TEST != TEST
Contents of the batch "test.bat":

Code: Select all

@echo OFF

setlocal ENABLEDELAYEDEXPANSION

for /F "usebackq delims== tokens=*" %%A in ("test_file.txt") do (
    set sLine=%%A
    echo.!sLine!
)

endlocal
exit /b %ERRORLEVEL%
Result of the batch execution:

Code: Select all

TEST = TEST
As you can see, the "!" character is not printed.
Can anyone tell me why and how to solve?

Thanks in advance, bye.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Windows Batch - Print line file containing exclamation mark

#2 Post by elzooilogico » 06 Apr 2020 09:12

disable delayed expansion, unless you are doing something with sLine you don't need it, change

Code: Select all

set "sLine=%%A"
echo !sLine!
to

Code: Select all

echo %%A
or use

Code: Select all

call set "..."
call echo %...%
ro reflect changes in a block of code

you cannot do string proccessing with for variables, but you can turn off delayed expansion, then call echo, and use endlocal to return to previous expansion mode

CharlesMM
Posts: 4
Joined: 06 Apr 2020 06:07

Re: Windows Batch - Print line file containing exclamation mark

#3 Post by CharlesMM » 06 Apr 2020 10:21

Thanks for the reply, but I was unable to resolve.

I'm probably wrong, but I've tried both:

Code: Select all

@echo OFF

setlocal ENABLEDELAYEDEXPANSION

for /F "usebackq delims== tokens=*" %%A in ("file_prova.txt") do (
    echo %%A"
)

endlocal
exit /b %ERRORLEVEL%
that:

Code: Select all

@echo OFF

setlocal ENABLEDELAYEDEXPANSION

for /F "usebackq delims== tokens=*" %%A in ("file_prova.txt") do (
    call set "sLine=%%A"
    call echo !sLine!
)

endlocal
exit /b %ERRORLEVEL%
the result has not changed.

I am not very practical, could you please post the complete code?

Thanks again

CharlesMM
Posts: 4
Joined: 06 Apr 2020 06:07

Re: Windows Batch - Print line file containing exclamation mark

#4 Post by CharlesMM » 06 Apr 2020 13:15

I have used both methods but the result has not changed

I'm not very good with batch, can you please post the whole code?

Thanks in advance

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Windows Batch - Print line file containing exclamation mark

#5 Post by Eureka! » 06 Apr 2020 14:27

Try it without the ENABLEDELAYEDEXPANSION as that uses ! for variables (like %):

Code: Select all

@echo off
setlocal

   for /F "usebackq delims=" %%A in ("test_file.txt") do (
      call :ACTION "%%A"
   )

endlocal
exit /b %ERRORLEVEL%


:ACTION
   echo. This line is: [%~1]
goto :EOF


Result:

Code: Select all

 This line is: [TEST != TEST]
 
Note: I also changed the For /f "options" as you want the whole line.
Note 2: Not entirely sure, but I thought the delims= should be the last of the options. But the very knowledgeable people here can probably give the final verdict on that :)

CharlesMM
Posts: 4
Joined: 06 Apr 2020 06:07

Re: Windows Batch - Print line file containing exclamation mark

#6 Post by CharlesMM » 07 Apr 2020 02:06

Thanks, this works, but the same problem occurs when the "%" character is present in the file (these are files that contain SQL scripts) :cry: .

Isn't there a way to escape the entire row?

Thanks again.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Windows Batch - Print line file containing exclamation mark

#7 Post by penpen » 14 Apr 2020 05:19

You should retry the approach of elzooilogico (as it was meant):

Code: Select all

@echo OFF
setlocal enableExtensions disableDelayedExpansion

for /F "usebackq delims== tokens=*" %%A in ("file_prova.txt") do (
	set line=%%A

	setlocal enableDelayedExpansion
	rem: work with !line!, for example 
	rem: echo(!line!
	endlocal

	echo(%%A
)

endlocal
exit /b %ERRORLEVEL%
penpen

Post Reply