why doesnt this batch work, it looks totally logical to me

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 127
Joined: 26 Aug 2017 06:11

why doesnt this batch work, it looks totally logical to me

#1 Post by nnnmmm » 27 Aug 2017 19:51

Code: Select all

REM @ECHO OFF

IF EXIST "C:\Documents and Settings\AAA\Desktop\ABC.lnk" (
   SET    VAR=N
   SET /P VAR="a file already exists, Overwrite it (y/n):"
   IF /I %VAR%==Y GOTO END1
   IF /I %VAR%==N GOTO END2
) ELSE (
  ECHO.
)

:END1
ECHO YES DONE
GOTO :END

:END2
ECHO NO DONE
GOTO :END

:END
PAUSE


why doesnt this batch work, it looks totally logical to me

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: why doesnt this batch work, it looks totally logical to me

#2 Post by aGerman » 28 Aug 2017 00:57

Variables in the same command line or the same (in surrounding paretheses enclosed) block of command lines are expanded to their values only once. That is before the the command line or block is executed. To avoid this "early" expansion you have to enable delayed variable expansion. After that you can access the new values if you replace the surounding percent signs with exclamation marks.

Steffen

nnnmmm
Posts: 127
Joined: 26 Aug 2017 06:11

Re: why doesnt this batch work, it looks totally logical to me

#3 Post by nnnmmm » 28 Aug 2017 01:27

>Variables in the same command line or the same (in surrounding paretheses enclosed) block of command lines are expanded to their >values only once.
i hope that you can explain more, why once and where once? what do you mean by once? so it will evaluate this line "SET VAR=N" and then leave the ( )'s?


i didnt use "enable delayed " below , but there are mnany command lines

Code: Select all

FOR %%%%U IN (%DRSPEC%) DO (
   IF EXIST %%%%U: (        <------------- command line once
      %%%%U:                    <------------- command line twice
      CD\                             <--------------command line thrid
      FOR /F "TOKENS=*" %%%%V IN ('DIR "%FSPEC1%" /A-D /B /S') DO (        <----------- command line 4th
      )
   )
)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: why doesnt this batch work, it looks totally logical to me

#4 Post by aGerman » 28 Aug 2017 01:55

As I said: "or the same (in surrounding paretheses enclosed) block of command lines".
Nearly everything is enclosed into parentheses in your code.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
...
   IF /I !VAR!==Y GOTO END1
   IF /I !VAR!==N GOTO END2
...

Steffen

nnnmmm
Posts: 127
Joined: 26 Aug 2017 06:11

Re: why doesnt this batch work, it looks totally logical to me

#5 Post by nnnmmm » 28 Aug 2017 02:31

IF /I %VAR%==Y GOTO END1..... is this command line?

i still dont understand why delayed expansion has to be used, when it is not FOR in (), but that is ok
i will just take it as "do not use two IF's or try delayed, i might understand these sometime later. thanks

Code: Select all

AA=
for in (
     if (
       for in (
            if ( 
           )
        )
    )
)

AA= is allowed, even though AA= is more complicated.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: why doesnt this batch work, it looks totally logical to me

#6 Post by aGerman » 28 Aug 2017 03:10

Variables that you defined outside of the pair of parentheses (outer pair if nested) are available without delayed expansion. If you define a new variable inside or you change the value of an existing variable and you want to access the new value then you need it.

Code: Select all

@echo off &setlocal EnableDelayedExpansion

set "n=0"

echo before: %n%

for /l %%i in (1 1 10) do (
  set "n=%%i
  echo ~~~
  echo %n%
  echo !n!
)

echo ~~~
echo after: %n%

pause

Steffen

nnnmmm
Posts: 127
Joined: 26 Aug 2017 06:11

Re: why doesnt this batch work, it looks totally logical to me

#7 Post by nnnmmm » 28 Aug 2017 05:03

AA=
for /l %%i in (1 1 10) do (
set "n=%%i
echo ~~~
echo %n%
echo !n!
)
i know AA=, i had a hard time initially, because FOR () has a looping behavior (variable circulation), you must use delayed, all "%" variables will be nullified (but it takes the last value once it is outside of FOR ()) .
wait a minute, does "IF Exist" consider to have a "loop" bahavior?

BB=
:END1
IF EXIST (
SET /P VAR="a file already exists, Overwrite it (y/n):"
IF /I %VAR%==Y GOTO END1
)

BB= is a loop, so i know it will throw up. but my END1 was at the bottom not at the top, it is not looping in my understanding. it will just go through all lines once, maybe it doesnt like GOTO statement because they just might be placed on top

i will try to search the meaning of "nested" once i have a little more clue, then i ask, nested can not be meant by just within a () , they also do well inside of many ()'s because most of my scripts are built with them.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: why doesnt this batch work, it looks totally logical to me

#8 Post by aGerman » 28 Aug 2017 05:15

It has nothing to do with a loop. It's just that the parenthesis
for /l %%i in (1 1 10) do (
and
IF EXIST "some file or folder" (
begin a block. And the related ) end the block.

Nestet just means
(...(...(...)...)...)
(regardless if in the same line or in multiple lines)
where with "outer parentheses" I meant the red marked.

Steffen

Post Reply