How can I modify this script to exit when needed?

I would like to modify the following script:

pushd "C:\Programs\Temp"
setlocal enabledelayedexpansion
set InputFile=C:\Programs\input.txt

findstr.exe /r /i /c:"^SAC" "%InputFile%" >NUL
if errorlevel 1 (
	Powershell.exe -executionpolicy remotesigned -File  C:\Programs\scripts\3.ps1
	
)

set FoundDouble=FALSE
set /a TestIndex = -1
for /f "tokens=1* delims=:" %%a in ('type "%InputFile%" ^| findstr.exe /n /r /i /c:"^DTM"') do (
	echo Line %%a: %%b
	if !TestIndex!==%%a (
		set FoundDouble=TRUE
	) else (
		set /a TestIndex = %%a + 1
	)
)
if %FoundDouble%==FALSE (
	cscript.exe /nologo "C:\Programs\scripts\1.vbs"
) else (
	cscript.exe /nologo "C:\Programs\scripts\2.vbs"
)

popd

Open in new window

It's part of a larger script, so after the popd... there is much more that it needs to continue with.

The problem is that when running the first part of the script, when it cannot find the SAC then it runs 3.ps1 - however after it runs that, it should continue on and skip the part about checking the DTM - and perhaps it should continue just before the popd, and continue on- but it should not check for the DTM.  What is happening in this script is that if the SAC is not found, it runs 3.ps1, writes the output file, but then also runs a check for the DTM after which it overwrites the file.

What is missing in the script so that after IF it finds the SAC it runs the 3.ps1 and then continues just before the popd?

Thank you.
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Try this, I just added the DTM logic into an ELSE off the first check, so it only happens when needed.

pushd "C:\Programs\Temp"
setlocal enabledelayedexpansion
set InputFile=C:\Programs\input.txt

findstr.exe /r /i /c:"^SAC" "%InputFile%" >NUL
if errorlevel 1 (
	Powershell.exe -executionpolicy remotesigned -File  C:\Programs\scripts\3.ps1
) else (
    set FoundDouble=FALSE
    set /a TestIndex = -1
    for /f "tokens=1* delims=:" %%a in ('type "%InputFile%" ^| findstr.exe /n /r /i /c:"^DTM"') do (
    	echo Line %%a: %%b
    	if !TestIndex!==%%a (
    		set FoundDouble=TRUE
    	) else (
    		set /a TestIndex = %%a + 1
    	)
    )
    if %FoundDouble%==FALSE (
    	cscript.exe /nologo "C:\Programs\scripts\1.vbs"
    ) else (
    	cscript.exe /nologo "C:\Programs\scripts\2.vbs"
    )
)
popd

Open in new window

~bp
100questionsAuthor Commented:
Thanks Bill.  The script doesn't run but I was able to get a screen shot and it says at one point..

 ( was unexpected at this time

From what I can tell, it's at point after the findstr has finished.. I think it's somewhere after the >NUL..
Bill PrewIT / Software Engineering ConsultantCommented:
Did it run before with no errors?

~bp
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You should decide to use only one (or at least not more than two) scripting language(s) here ...
However, I cannot see the issue with the script of Bill.

For debugging you should
a) put an @echo on the line in front of findstr
b) run the script in a cmd window you opened manually. It should then stop at the error line, with some output.
100questionsAuthor Commented:
@Bill Prew - The script ran before however as mentioned at the outset ""What is happening in this script is that if the SAC is not found, it runs 3.ps1, writes the output file, but then also runs a check for the DTM after which it overwrites the file."

After I added your modification, it simply does not even run the 3.ps1.
I am able to tell that there is an issue since I was able to screenshot the error
"( was unexpected at this time".

Hope this helps.
NVITEnd-user supportCommented:
Line 6
Change errorlevel to %errorlevel%
NVITEnd-user supportCommented:
Also... I didn't check all but... Any variable set within parens then referenced may need to be wrapped with ! instead of %. E.g. %FoundDouble% line 19

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
http:#a41135384  is wrong and will lead to a syntax error
http:#a41135413  is correct, and might be the culprit, because it changes the syntax (as there wasn't any delimiter used).
100questionsAuthor Commented:
Works when the change is made to the script as per http:#a41135413 
Thank you.
NVITEnd-user supportCommented:
> http:#a41135384  is wrong and will lead to a syntax error
Thanks for catching that, Qlemo. I was thinking of the other way to do it...
if %errorlevel% equ 1

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Strictly seen :D :
if %errorlevel% geq 1

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.