Reputation: 137
How to get rid from below error if my text file containing the special character like this?
Set_Param_10A "TRUE" {xnetwork[<11:0:-1>]}
If I remove this line in the text file, then my batch program can run smoothly, else it will give the below error:
The filename, directory, or volume label syntax is incorrect
I understand that it is cause by the special such as < in the file, but i am not able to remove this in a file, i just wanna batch program to ignore this...
Below is my code:
@ECHO OFF
SETLOCAL
SET matchpattern="0632"
FOR /f "delims=" %%a IN (log_network.txt) DO (SET currentline=%%a & CALL :match_function)
pause
GOTO :EOF
:match_function
ECHO %currentline%|findstr /I /R /C:"%matchpattern%" > NUL
if %errorlevel%==0 (
echo %currentline%
)
log_network.txt:
Set_Param_10A "TRUE" "xnetwork.exist.5846"
Set_Param_10A "TRUE" "xnetwork.exist.7425"
Set_Param_10A "TRUE" "xnetwork.exist.1420"
Set_Param_10A "TRUE" "xnetwork.exist.0632"
Set_Param_10A "TRUE" "xnetwork.exist.1112"
Set_Param_10A "TRUE" "xnetwork.exist.8524"
Set_Param_10A "TRUE" "xnetwork.exist.3675"
Set_Param_10A "TRUE" "xnetwork.exist.3344"
Set_Param_10A "TRUE" "xnetwork.exist.1276"
Set_Param_10A "TRUE" "xnetwork.exist.4796"
Set_Param_10A "TRUE" "xnetwork.exist.3349"
Set_Param_10A "TRUE" "xnetwork.exist.0048"
Set_Param_10A "TRUE" {xnetwork[<11:0:-1>]}
Upvotes: 0
Views: 108
Reputation: 67226
@ECHO OFF
SETLOCAL
SET "matchpattern=0632"
findstr /I /R /C:"%matchpattern%" log_network.txt
However, in this case you don't need a Batch file, just enter this in the command-line:
findstr "0632" log_network.txt
The /I switch is not needed because you are not searching letters, the /R switch is not needed because you look just for the "0632" literal, and the /C switch is not needed because you look just for one string (the only one).
Upvotes: 0
Reputation: 70943
You have the option to enclose in quotes the data that you are echoing
echo("%currentline%"|findstr /I /R /C:"%matchpattern%" > NUL
but then the quotes will be included in the data feed to findstr and you may have to take them into account. And if the lines start or contains multiple quotes it can also give errors as the problematic characters can fall out of the quotes.
You can use an argument to the subroutine
FOR /f "delims=" %%a IN (log_network.txt) DO (CALL :match_function "%%a")
....
:match_function
echo(%1|findstr /I /R /C:"%matchpattern%" > NUL
if %errorlevel%==0 (
echo(%~1
)
goto :eof
but you also have to consider the aditional quotes
To avoid it, you can use delayed expansion, but it is complicated
@ECHO OFF
setlocal enableextensions disabledelayedexpansion
SET "matchpattern=0632"
FOR /f "delims=" %%a IN (log_network.txt) DO (SET "currentline=%%a" & CALL :match_function)
pause
GOTO :EOF
:match_function
(cmd /q /v:on /c "echo(!currentline!")|findstr /I /R /C:"%matchpattern%" > NUL
if %errorlevel%==0 (
echo(%currentline%
)
goto :eof
To echo the special characters to the pipe using a variable name you need delayed expansion, but inside a pipe, as a new process is created for each of the sides of the pipe, you can not ensure the cmd
instance that will execute the echo
has or not delayed expansion enabled, so, it is necessary to directy spawn the instance with delayed expansion enabled, then from there echo the variable and then the data is piped into findstr
This is easier and usually a lot faster
@ECHO OFF
setlocal enableextensions disabledelayedexpansion
SET "matchpattern=0632"
for /f "delims=" %%a in (
'findstr /r /i /c:"%matchpattern%" log_network.txt'
) do (
echo(%%a
)
Upvotes: 0
Reputation: 9545
Your %%a
variable have to be between "
to escape the <
and >
chars.
Try like this :
@ECHO OFF
SETLOCAL
SET matchpattern="0632"
FOR /f "delims=" %%a IN (log_network.txt) DO CALL :match_function "%%a"
pause
GOTO :EOF
:match_function
ECHO %1 | findstr /I /R /C:"%matchpattern%" > NUL && echo %~1
Upvotes: 1