0

I've wrote a subroutine with some if..else nested Unfortunately i can't understand why my code are executed before and after the "ELSE" (!!) I read it for hours, but i don't find any error.. can you help me? I copied the code (with a lot of echo-debugs) and the output

This is my little code:

 setlocal
 set debug=echo 
 %debug%   # %~0: %*
 for /f "usebackq tokens=1-3 delims=;" %%a in ('"%*"') do (set dir=%%~a& set own=%%~b)
 %debug%   - dir="!dir!"
 %debug%   - own="!own!"
 %debug%   -+ analisi cartella: "!dir!"
 if exist "!dir!" (
    %debug%    x la cartella esiste già, passo oltre
    ) else (
    %debug%    - la cartella non esite quindi la creo
    mkdir "!dir!" || echo   [E] non sono riuscito a creare la cartella&& echo %err3_txt% [dir:!dir!] 1>&2 && exit %err3_id%
    )
 %debug%   -+ analisi proprietario: "!own!"
 for /f "usebackq tokens=1,2 delims=\" %%x in ('"!own!\\"') do (set own1=%%~x& set own2=%%~y)
 if [!own!] equ [!own:~0,1! ] (
    %debug%    x il proprietario non è stato specificato quindi non va cambiato, passo oltre
    ) else (
    %debug%    - own1: "!own1!" 
    %debug%    - own2: "!own2!"
    if [%own2%] equ [] (
   %debug%    - il proprietario ha un dominio implicito, lo calcolo
       set ownn=%own1%
       set ownd=%userdomain%
       ) else (
       %debug%    - il proprietario ha un dominio esplicitamente indicato
       set ownd="input non valido"
       if /i [!own1!] equ [L] set ownd=%computername%
       if /i [!own1!] equ [D] set ownd=%userdomain%
       if "%ownd%" equ "input non valido" (echo    [E] il dominio esplitamente indicato non è riconosciuto: !own1!& echo %err4_txt% [dir:!dir!][own:!own!] 1>&2 & exit %err4_id%)
       )
    %debug%    - la configurazione prevede l'impostazione del seguente proprietario: !ownd!\!ownn!
    )
 %debug%   x done.

this is my output :

# :CSG_check_fs: C:; ;

 - dir="C:"

 - own=" "

 -+ analisi cartella: "C:"

  x la cartella esiste giÓ, passo oltre

 -+ analisi proprietario: " "

  x il proprietario non Þ stato specificato quindi non va cambiato, passo oltre

  - own1: " "

  - own2: ""

  - il proprietario ha un dominio implicito, lo calcolo

  - la configurazione prevede l'impostazione del seguente proprietario: CSG\    

x done.

in particular, let us appropriate commands are executed BEFORE and AFTER the else clause!!! why??? this thing is incredible!! or am i too tired?

CSG
  • 247
  • 5
  • 14

2 Answers2

0

Imo this might be not perfect, but please keep in mind, this is no de-bugging service here & I'm not your father, Luke.

@ echo off&setlocal

for /f "usebackq tokens=1-3 delims=;" %%a in ("%*") do (set dir=%%~a& set own=%%~b)
if not exist "%dir%" mkdir "%dir%" || echo   [E] non sono riuscito a creare la cartella&& echo %err3_txt% [dir:%dir%]&& exit %err3_id%
for /f "usebackq tokens=1,2 delims=\" %%x in ("%own%\\") do (set own1=%%~x& set own2=%%~y)

if not [%own%] equ [%own:~0,1%] (
   if [%own2%] equ [] (
      set ownn=%own1%
      set ownd=%userdomain%
    ) else (
      set "ownd=input non valido"
      if /i [%own1%] equ [L] set ownd=%computername%
      if /i [%own1%] equ [D] set ownd=%userdomain%
      if "%ownd%" equ "input non valido" (echo    [E] il dominio esplitamente indicato non è riconosciuto: %own1%& echo %err4_txt% [dir:%dir%][own:%own%]& exit %err4_id%)
    )
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

It is certainly very odd. CMD seems to be having a real problem with this very complex statement (and so do I...) :

 if [!own!] equ [!own:~0,1! ] (
    %debug%    x il proprietario non è stato specificato quindi non va cambiato, passo oltre
    ) else (
    %debug%    - own1: "!own1!" 
    %debug%    - own2: "!own2!"
    if [%own2%] equ [] (
   %debug%    - il proprietario ha un dominio implicito, lo calcolo
       set ownn=%own1%
       set ownd=%userdomain%
       ) else (
       %debug%    - il proprietario ha un dominio esplicitamente indicato
       set ownd="input non valido"
       if /i [!own1!] equ [L] set ownd=%computername%
       if /i [!own1!] equ [D] set ownd=%userdomain%
       if "%ownd%" equ "input non valido" (echo    [E] il dominio esplitamente indicato non è riconosciuto: !own1!& echo %err4_txt% [dir:!dir!][own:!own!] 1>&2 & exit %err4_id%)
       )
    %debug%    - la configurazione prevede l'impostazione del seguente proprietario: !ownd!\!ownn!
    )

Looking at

if [!own!] equ [!own:~0,1! ] (

You may think that this should be evaluated, with OWN set to a single space, as

if [ ] equ [ ] (

Which should generate a syntax error because the third token in the command is ] and that's not a valid comparison-operator. The !var! confuses matters because it can't be evaluated until EXECUTION TIME - and by then, the parser has finished its work.

The correct form is

 if "%own%" equ "%own:~0,1% " (

because the parser can then substitute the CURRENT (ie. PARSE-TIME) value of own and the syntax is "quoted string" operator "another quoted string"

I'm sure that this change would solve the major part of the problem.

Similarly,

    if [%own2%] equ [] (

should be

    if "%own2%" equ "" (

or even (my preference)

    if not defined own2
  • and the same goes for the other IF [..] equ [...] constructs.

Finally, in the sequence

       set ownd="input non valido"
       if /i [!own1!] equ [L] set ownd=%computername%
       if /i [!own1!] equ [D] set ownd=%userdomain%
       if "%ownd%" equ "input non valido" (echo    [E] il dominio ...

there's a small problem (other than the if..[...

ownd may be SET within the code, but the %ownd% in the last of these lines will have the PARSE-TIME value of ownd substituted, so it's unlikely to contain the target string. Here's where you NEED !var! because the value of var is being changed by the code and you want the changed value, not the value as it stood when the statement was parsed.

HTH

Magoo
  • 77,302
  • 8
  • 62
  • 84