What is wrong with this script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

What is wrong with this script?

#1 Post by goodywp » 01 Dec 2017 11:09

Hi all,

I have a text file called string.txt
----------------------
T501-08680-0101
T501-08815-0100
T501-08665-0102
T501-01110-0102
---------------------------
I have the following code to check if the string exist in this text file then set old_prof=xxx

Code: Select all

@echo off

::Mockup
findstr /c:"T501-08680" string.txt 
if not errorlevel 1 (
	set old_prof=Mockup
	echo %old_prof%
	) else ( 
	goto QA_Infra
	)

:QA_Infra
findstr /c:"T501-08815" string.txt 
if not errorlevel 1 (
	set old_prof=QA_Infra
	echo %old_prof%
	) else (
	goto NAR_Infra
	)

:NAR_Infra
findstr /c:"T501-08665" string.txt 
if not errorlevel 1 (
	set old_prof=NAR_Infra
	echo %old_prof%
	) else (
	goto MY_Prof
	)

:MY_Prof
findstr /c:"T501-01110" string.txt 
if not errorlevel 1 (
	set old_prof=MY_Prof
	echo %old_prof%
	) else (
	goto end
	)

:end
I tested it as below:
1) I modify the string.txt as
T501-08680-0101
T501-18815-0100
T501-18665-0102
T501-11110-0102

it works fine and old_prof=Mockup

2) I modify the string.txt as
T501-18680-0101
T501-08815-0100
T501-18665-0102
T501-11110-0102
it works fine but old_prof=Mockup instead of QA_Infra

Why the set value always set the previous one ??

Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: What is wrong with this script?

#2 Post by Squashman » 01 Dec 2017 11:38

goodywp wrote:
01 Dec 2017 11:09

Why the set value always set the previous one ??

Thanks
It's not. You are forgetting you are inside a parenthesized code block so you need to use Delayed Expansion to display the variable contents correctly. You can also use CALL ECHO as well.

Code: Select all

call echo %%old_prof%%
Not sure why you have the ELSE clause and the GOTO. It will naturally go to the next set of code without the need for the GOTO.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: What is wrong with this script?

#3 Post by Compo » 01 Dec 2017 11:39

Change all instances of

Code: Select all

%old_prof%
to

Code: Select all

!old_prof!
and insert this as a new line 2

Code: Select all

SetLocal EnableDelayedExpansion
.

Post Reply