Page 1 of 1
Windows Batch - Print line file containing exclamation mark
Posted: 06 Apr 2020 06:14
by CharlesMM
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":
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:
As you can see, the "!" character is not printed.
Can anyone tell me why and how to solve?
Thanks in advance, bye.
Re: Windows Batch - Print line file containing exclamation mark
Posted: 06 Apr 2020 09:12
by elzooilogico
disable delayed expansion, unless you are doing something with
sLine you don't need it, change
to
or use
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
Re: Windows Batch - Print line file containing exclamation mark
Posted: 06 Apr 2020 10:21
by CharlesMM
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
Re: Windows Batch - Print line file containing exclamation mark
Posted: 06 Apr 2020 13:15
by CharlesMM
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
Re: Windows Batch - Print line file containing exclamation mark
Posted: 06 Apr 2020 14:27
by Eureka!
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:
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
Re: Windows Batch - Print line file containing exclamation mark
Posted: 07 Apr 2020 02:06
by CharlesMM
Thanks, this works, but the same problem occurs when the "%" character is present in the file (these are files that contain SQL scripts)
.
Isn't there a way to escape the entire row?
Thanks again.
Re: Windows Batch - Print line file containing exclamation mark
Posted: 14 Apr 2020 05:19
by penpen
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