#1 01 Aug 2020 08:09

brianjonnes
New Member
Registered: 04 Jul 2020
Posts: 2

"echo off" and "if" commands

Good-day

It appears that the echo state cannot be overridden in an if subclause.

I want the subcommands to echo, but not the if. The intent in the following should be clear, but the result is no output at all:

set var=on
@echo off
if %var%=on (
   echo on
   attrib /s +r c:\data_backup
   @echo off )

This is just out of curiosity; obviously I can "echo attribs applied" as a workaround (and you might say that that's better, but I still want to know).

Cheers
Brian

Offline

#2 01 Aug 2020 22:18

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 184

Re: "echo off" and "if" commands

The code block is part of the if statement, which currently falls under @echo off. You can achieve the desired result if you put the code in a subroutine:

@echo off
set var=on
if "%var%"=="on" (
   call :make_readonly
)
exit /b

:make_readonly
echo on
attrib /s +r C:\data_backup
@echo off

Offline

#3 02 Aug 2020 07:58

brianjonnes
New Member
Registered: 04 Jul 2020
Posts: 2

Re: "echo off" and "if" commands

Most kind of you, good sir! :-)

Offline

Board footer

Powered by FluxBB