Page 1 of 1

Why doesn't this replacement work?

Posted: 11 Jul 2020 09:23
by SIMMS7400
Hi Folks -

I have the following script (trimmed down for example purposes):

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET X=T

PUSHD "D:\TEMP"	
	IF DEFINED X (
		SET "SEARCH="Rule Name","File Name:","Error: 3303","Error: 3304","Error: 3333","Error: 3335","Error: 3336","Error: 3337","does not exist for the specified cube or you do not have access to it""
		SET "FDMEE_KOF=_KICKOUTS_.txt"
        FOR %%A IN ( !SEARCH:,= ! ) DO (
            FOR /F "tokens=* delims=" %%a IN ('FINDSTR /C:"%%~A","20200711_1055_FIN_IQ_2002.log"') DO (
                ECHO %%a>>"!FDMEE_KOF!"
            )
        )
	POPD & EXIT /B 1
)
CLS	
GOTO :EOF
I'm obviously within a code block so I need to use !, but why won't the comma to space replacement work?

Code: Select all

( !SEARCH:,= ! ) DO (
I can remove the commas, leave the spaces and use !SEARCH! and it works fine, but here is room for error with that approach.

Now I could just put the SEARCH variable outside the block, but I don't want to do that. ANy ideas?

Re: Why doesn't this replacement work?

Posted: 11 Jul 2020 11:51
by T3RRY
The coma is a delimiter within the for loop set - much the same as space, which is what prevents the Substring modification from occuring when the for loop executes. The upside of this is that the substring modification is uneccesary, just use the variable as it's already defined. The output will be the same as it would if you:

Code: Select all

Set Search=!Search:,= !
Immediatley prior to the inner forloop and used

Code: Select all

FOR %%A IN ( !SEARCH! ) DO (
...


As an aside, String substitution of a coma within a for loop set can be forced by douplequoting the string as the below example shows, however a second for loop is required to approriately strip the Doubleqoutes after the fact.

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET X=T

	IF DEFINED X (
	    SET SEARCH="Rule Name","File Name:","Error: 3303","Error: 3304","Error: 3333","Error: 3335","Error: 3336","Error: 3337","does not exist for the specified cube or you do not have access to it"
            FOR /F "Delims=" %%# IN ('Echo/"!Search:,= !"') Do For %%V in (%%~#) Do Echo/%%~V|FINDSTR "Error"
	)
Pause

Re: Why doesn't this replacement work?

Posted: 14 Jul 2020 10:53
by pieh-ejdsch
Hallo,
That's how you actually write one-liners!
You should simply write the list as it is with each other in the for- loop, then you will have it a little clearer.
A second for-loop for the name of the log file completes the matter.

Code: Select all

IF DEFINED X for /f "tokens=2delims==" %%K in ("FDMEE_KOF=_KICKOUTS_.txt") do  >>"%%K" (
 for %%S in ( "Rule Name"
  "File Name:"
  "Error: 3303"
  "Error: 3304"
  "Error: 3333"
  "Error: 3335"
  "Error: 3336"
  "Error: 3337"
  "does not exist for the specified cube or you do not have access to it""
 ) DO FINDSTR /C:"%%~S" "20200711_1055_FIN_IQ_2002.log"
)
- or you'd rather leave it out.

Code: Select all

if defined X >>"_KICKOUTS_.txt" ( for %%S in ("Rule Name"
  "..."
 ) do findstr /c:"%%~S" "20200711_1055_FIN_IQ_2002.log"
)
Phil

Re: Why doesn't this replacement work?

Posted: 01 Aug 2020 03:26
by SIMMS7400
Thank you T3RRY and Phil!!

T3RRY's solution is working as expected, but is there one that is preferred between the two? WHat is better and why? Or does it not matter?