Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to add else condition in my code it is not executing my .batch file.
When I add only if condition it works fine.



Problem getting in apply else condition in the last of the code.

What I have tried:

Cd /d D:


	SetLocal EnableDelayedExpansion
	set "file=newabc.c"
	set var={"abc1
	set var2="};
	for /F "tokens=1,* delims==" %%i in ('findstr "abc[] = " newabc.c') do (
    set "versionVar=%%~i"
    set "versionVal=%%~j"
	set "string=!versionVal:~7,5!"
	set /a "string+=1"
	::set "string=%versionVal:~2,1%"
	::%string:~4,3%
    :: set /a "string=string+1"
	::echo %sequence%
	
	)
	for /f "tokens=1,2 delims=." %%a in ("!versionVal!") do (
    set BEFORE_UNDERSCORE=%%a
    set AFTER_UNDERSCORE=%%b
)
	::echo !BEFORE_UNDERSCORE!
	
	for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
    ::set "line=%%~j"
	if "!BEFORE_UNDERSCORE!" == "!var!" (
	
	set "line=%%~j"
	echo !line! hello
    if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!var!.!string!
    echo(!line!>>!file!)
	
	) else 
	
	 set "line=%%~j"
	 if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!versionVar!=!versionVal!
	 echo !line!>>!file!)
	)
	
	pause
Posted
Updated 11-Nov-22 3:33am
Comments
Richard MacCutchan 11-Nov-22 9:31am    
You have not explained exzactly what happens in this script. Just saying "t is not executing" does not tell ius anything useful.

1 solution

You are missing the opening parenthesis of the ELSE clause. You have:
BAT
) else

It should be:
BAT
) else (


[EDIT]

Your if statements are not correct, you have the following:
BAT
    if "!line!" == "!versionVar!=!versionVal!" set <-- THIS IS INCORRECT
line=!versionVar!=!var!.!string! <-- THE IF PART IS NOT CLOSED
    echo(!line!>>!file!) 
	
	) else <-- THE ELSE CLAUSE IS NOT OPENED
	
	 set "line=%%~j"
	 if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!versionVar!=!versionVal!
	 echo !line!>>!file!)
	)

Try this:
BAT
if "!line!" == "!versionVar!=!versionVal!" (
    set line=!versionVar!=!var!.!string!
    echo(!line!>>!file!) 
    
) else (

    set "line=%%~j"
    if "!line!" == "!versionVar!=!versionVal!" (
        set line=!versionVar!=!versionVar!=!versionVal!
        echo !line!>>!file!
    )
)


And try using consistent and proper indentation so your code is readable.

[/EDIT]
 
Share this answer
 
v2
Comments
Radha sharma 2022 11-Nov-22 9:55am    
@Richard MacCautchan ,I tried after ( after else but it is not working
Radha sharma 2022 11-Nov-22 9:56am    
Script is not executed while else is placed in the code
Richard MacCutchan 11-Nov-22 11:51am    
You need to be more explicit about exactly what is happening. No one here can guess what is meant by "it is not working".
Richard MacCutchan 11-Nov-22 12:06pm    
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900