Escape characters in ENABLEDELAYEDEXPANSION

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Escape characters in ENABLEDELAYEDEXPANSION

#1 Post by tinfanide » 29 Jan 2012 00:39

b.log:
"WINWORD.EXE","2348","Console","1","49,852 K"
"WINWORD.EXE","4568","Console","1","48,840 K"
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
"WINWORD.EXE","1980","Console","1","36,928 K"
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.
INFO: No tasks are running which match the specified criteria.


Code: Select all

FOR /F %%C IN (C:\b.log) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
SET "old=%%C"


SET new=!old:INFO: No tasks are running which match the specified criteria.=!
:: output: INFO:

ECHO !new! >> C:\c.log
)

ENDLOCAL

PAUSE


I want to replace "INFO: No tasks are running which match the specified criteria." with "" (nothing) in the b.log text file
but I don't know how to tell CMD the ":" in the string to be replaced should be treated not as special characters
I've used ^ but failed.

Code: Select all

SET new=!old:INFO^: No tasks are running which match the specified criteria.=!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Escape characters in ENABLEDELAYEDEXPANSION

#2 Post by Ed Dyreen » 29 Jan 2012 03:29

'
Why no quotes ?

Code: Select all

@echo off &SETLOCAL ENABLEDELAYEDEXPANSION

set "$var=INFO: No tasks are running which match the specified criteria."
set "$var"
set "$var=!$var:INFO: No tasks are running which match the specified criteria.=!"
set "$var"

pause
if defined $var ECHO.!$var!>>"C:\c.log"
type "C:\c.log"
pause
exit

Code: Select all

$var=INFO: No tasks are running which match the specified criteria.
Omgevingsvariabele $var is niet gedefinieerd
Druk op een toets om door te gaan. . .

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Escape characters in ENABLEDELAYEDEXPANSION

#3 Post by jeb » 29 Jan 2012 08:20

Ed Dyreen wrote:Why no quotes ?

Ok, quotes makes it more secure, but they don't have any correlation to the problem :)

tinfanide wrote:but I don't know how to tell CMD the ":" in the string to be replaced should be treated not as special characters
I've used ^ but failed.
Yes, it fails, as the colon doesn't need any escape character.
You run into an other problem, not the replacement fails, it's the output of the result :!:

If your replacement is successful, the variable new is empty, or better it isn't defined.
That results in your line ECHO !new! >> C:\c.log into an empty echo statement, that will output something like
echo is off

So you could use a "safe" echo like

Code: Select all

echo(!new!>> C:\c.log
Important is the "(" after the echo and the missing space between "!new!>>".
But even the "safe" echo would append empty lines, if you want to supress them too, you could use the solution of Ed.

Code: Select all

if defined new (
  echo(!new!>>C:\c.log
)


jeb

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Escape characters in ENABLEDELAYEDEXPANSION

#4 Post by tinfanide » 29 Jan 2012 11:03

Ed Dyreen wrote:'
Why no quotes ?

Code: Select all

@echo off &SETLOCAL ENABLEDELAYEDEXPANSION

set "$var=INFO: No tasks are running which match the specified criteria."
set "$var"
set "$var=!$var:INFO: No tasks are running which match the specified criteria.=!"
set "$var"

pause
if defined $var ECHO.!$var!>>"C:\c.log"
type "C:\c.log"
pause
exit

Code: Select all

$var=INFO: No tasks are running which match the specified criteria.
Omgevingsvariabele $var is niet gedefinieerd
Druk op een toets om door te gaan. . .


Sorry, I couldn't follow your suggested codes.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Escape characters in ENABLEDELAYEDEXPANSION

#5 Post by tinfanide » 29 Jan 2012 11:05

jeb wrote:
Ed Dyreen wrote:Why no quotes ?

Ok, quotes makes it more secure, but they don't have any correlation to the problem :)

tinfanide wrote:but I don't know how to tell CMD the ":" in the string to be replaced should be treated not as special characters
I've used ^ but failed.
Yes, it fails, as the colon doesn't need any escape character.
You run into an other problem, not the replacement fails, it's the output of the result :!:

If your replacement is successful, the variable new is empty, or better it isn't defined.
That results in your line ECHO !new! >> C:\c.log into an empty echo statement, that will output something like
echo is off

So you could use a "safe" echo like

Code: Select all

echo(!new!>> C:\c.log
Important is the "(" after the echo and the missing space between "!new!>>".
But even the "safe" echo would append empty lines, if you want to supress them too, you could use the solution of Ed.

Code: Select all

if defined new (
  echo(!new!>>C:\c.log
)


jeb


Code: Select all


@ECHO OFF

FOR /F %%C IN (C:\b.log) DO (
SETLOCAL ENABLEDELAYEDEXPANSION

SET "old=%%C"
SET new=!old:INFO: No tasks are running which match the specified criteria.=!


IF defined new (
ECHO(!new!>>C:\c.log
)

ENDLOCAL

PAUSE


Sorry, it does not work. It returns error.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Escape characters in ENABLEDELAYEDEXPANSION

#6 Post by tinfanide » 29 Jan 2012 18:37

Code: Select all

FOR /F "tokens=*" %%C IN (C:\Users\Tin\Desktop\text.txt) DO (
SETLOCAL ENABLEDELAYEDEXPANSION

SET "old=%%C"
SET new=!old:INFO: No tasks are running which match the specified criteria.=!
IF DEFINED new (

:: ECHO) = ECHO.
   ECHO.!new!

)
ENDLOCAL
)


Finally, I've sorted it out. It might be me mistaking some of your codes because of being new in MS-DOS.

Yes, the most important part that fails is the !new! variable being undefined. And second, ECHO) // ECHO. (without spaces) does matter.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Escape characters in ENABLEDELAYEDEXPANSION

#7 Post by !k » 30 Jan 2012 10:24

Why not

Code: Select all

FINDSTR /V /C:"INFO: No tasks are running which match the specified criteria." b.log
?

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Escape characters in ENABLEDELAYEDEXPANSION

#8 Post by tinfanide » 30 Jan 2012 10:41

!k wrote:Why not

Code: Select all

FINDSTR /V /C:"INFO: No tasks are running which match the specified criteria." b.log
?


Yes, using FINDSTR /V in this situation is just simple and beautiful.
I didn't know why I forgot it. Or I messed it up somehow.

Post Reply