set var= if true (goto) else (echo.&echo.%var%)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jess Selien
Posts: 13
Joined: 26 Sep 2012 12:08

set var= if true (goto) else (echo.&echo.%var%)

#1 Post by Jess Selien » 26 Sep 2012 12:54

Is there any way to set this line of code to a variable which I can execute multiple times in a program?

set date=
set var= if true (goto) else (echo.&echo.%date%)
...
set date=Monday&%var%
set date=Tuesday&%var%
...etc...

In this example, what results is the date variable is set to blank and is that way through out the code. Is there any way to use some type of escape char for this to work?

Workaround=
set date=Monday
if true (goto) else (echo.&echo.%date%)
set date=Tuesday
if true (goto) else (echo.&echo.%date%)

Jess Selien
Posts: 13
Joined: 26 Sep 2012 12:08

Re: set var= if true (goto) else (echo.&echo.%var%)

#2 Post by Jess Selien » 26 Sep 2012 14:36

My goal is finding a way to shorten code. Not necessarilly prove the date is correct.

Another example -
set streampath=D:\temp\Output.log
set streammsg=
set streamlog=echo.%streammsg%>>%streampath%

set streammsg=Hello world
if statement equ true (%streamlog%) else echo.%streammsg%
set streammsg=Today is Tuesday
if statement equ true (%streamlog%) else echo.%streammsg%
set streammsg=It is cool out today
if statement equ true (%streamlog%) else echo.%streammsg%
etc...

Instead of typing:
set streammsg=Hello world
if statement equ true (echo.%streammsg%>>%streampath%) else echo.%streammsg%
etc...

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

Re: set var= if true (goto) else (echo.&echo.%var%)

#3 Post by Squashman » 26 Sep 2012 19:01

This code makes absolutely no sense.

Code: Select all

set streammsg=Hello world
if statement equ true (%streamlog%) else echo.%streammsg%
set streammsg=It is cool out today
if statement equ true (%streamlog%) else echo.%streammsg%


How is anyone suppose to logically determine if "Hello World" is a true statement. Same goes for "It is cool out today".

State something more specific as to what you want accomplished.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: set var= if true (goto) else (echo.&echo.%var%)

#4 Post by Liviu » 26 Sep 2012 21:38

Jess Selien wrote:Another example
[...]
if statement equ true (%streamlog%) else echo.%streammsg%
What language is that supposed to be in? In a batch file, the "else" part would get executed every time. Please take your own advice from the other thread and post an intelligible, answerable question.

It might be that what you're after is...

Code: Select all

@echo off & setlocal
set streamlog=echo.%%streammsg%%^>^>"=%temp%\Output.log"

set statement=true
call :duh
set statement=false
call :duh
endlocal & goto :eof

:duh
set streammsg=Hello world
if %statement% equ true (call %streamlog%) else echo.%streammsg%
...but that might also (and likely) be the wrong answer to a question you never fully articulated.

Liviu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: set var= if true (goto) else (echo.&echo.%var%)

#5 Post by foxidrive » 26 Sep 2012 23:12

Your problem might be handled in a loop where you execute something with a range of items.

The better your description of the problem is, the better the answer you will get.

@echo off
for %%a in (monday tuesday wednesday thursday friday) do (
call :routine %%a
if defined result (do this) else (do that)
)

Jess Selien
Posts: 13
Joined: 26 Sep 2012 12:08

Re: set var= if true (goto) else (echo.&echo.%var%)

#6 Post by Jess Selien » 28 Aug 2013 16:32

Sorry for the late reply and poor description.
Liviu provided the answer I was looking for.

I want the dos batch code the same to be executed multiple times but yet have a different value for the variable.

set new value
call :duh
set new value
call :duh

It is exactly what I was looking for. Thanks!

Post Reply