Why doesn't this replacement work?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 475
Joined: 07 Jan 2016 07:47

Why doesn't this replacement work?

#1 Post by SIMMS7400 » 11 Jul 2020 09:23

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?

T3RRY
Posts: 83
Joined: 06 May 2020 10:14

Re: Why doesn't this replacement work?

#2 Post by T3RRY » 11 Jul 2020 11:51

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

pieh-ejdsch
Posts: 232
Joined: 04 Mar 2014 11:14
Location: germany

Re: Why doesn't this replacement work?

#3 Post by pieh-ejdsch » 14 Jul 2020 10:53

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

SIMMS7400
Posts: 475
Joined: 07 Jan 2016 07:47

Re: Why doesn't this replacement work?

#4 Post by SIMMS7400 » 01 Aug 2020 03:26

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?

Post Reply