Variable inside an IF not showing correctly

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Variable inside an IF not showing correctly

#1 Post by Docfxit » 08 Oct 2023 13:16

I can't get this script to work correctly. My syntax is wrong.
This is the script:

Code: Select all

setlocal EnableDelayedExpansion
set wkdir=%cd%
set IP=192.168.1.2
Set "IPADDRESS=\\%IP%"

if %_OS%==64 (
		Set "FileName=SecureVNCPlugin64.dsm"
		if not exist "!FileName!" (
			set "file=!file!!lf!   ?    %FileName%"
			robocopy "%IPADDRESS%\Dnload\VNC" "%wkdir%" "!FileName!"
			Echo "FileName = -" ^^^!FileName!
			Echo "wrkdir = -" !wrkdir!
			@Pause>nul|(@color 17&echo  3-%_OS%==64&@color)
			IF NOT ERRORLEVEL 1 GOTO FileCopied)
)
In the cmd window it shows as:

Code: Select all

C:\Dnload\VNC>if 64 == 64 (
Set "FileName=SecureVNCPlugin64.dsm"
 if not exist "!FileName!" (
set "file=!file!!lf!   ?    VNCsetup.reg"
 robocopy "\\192.168.168.7\Dnload\VNC" "C:\Dnload\VNC" "!FileName!"
 Echo "FileName = -" ^!FileName!
 Echo "wrkdir = -" !wrkdir!

 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
"FileName = -" !FileName
"wrkdir = -"
 3-64==64
Could someone please help me fix the code?

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Variable inside an IF not showing correctly

#2 Post by Batcher » 08 Oct 2023 20:03

Code: Select all

setlocal EnableDelayedExpansion
set "wrkdir=%cd%"
set "IP=192.168.1.2"
set "IPADDRESS=\\%IP%"

if %_OS%==64 (
    set "FileName=SecureVNCPlugin64.dsm"
    if not exist "!FileName!" (
        set "file=!file!!lf!   ?    %FileName%"
        robocopy "%IPADDRESS%\Dnload\VNC" "!wrkdir!" "!FileName!"
        echo "FileName = -" !FileName!
        echo "wrkdir = -" !wrkdir!
        if not errorlevel 1 (
            goto :FileCopied
        )
    )
)

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#3 Post by Docfxit » 09 Oct 2023 16:17

Thank you for the reply...

With your updates the cmd window is now showing:

Code: Select all

C:\Dnload\VNC>if 64 == 64 (
Set "FileName=SecureVNCPlugin64.dsm"
 if not exist "!FileName!" (
set "file=!file!!lf!   ?    VNCsetup.reg"
 robocopy "\\192.168.168.7\Dnload\VNC" "!wkdir!" "!FileName!"
 Echo "FileName = -" !FileName!
 Echo "wrkdir = -" !wrkdir!

 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
)
The line:
set "file=!file!!lf! ? VNCsetup.reg"
Should be showing:
set "file=!file!!lf! ? SecureVNCPlugin64.dsm"

The Echo lines should be showing the variable contents.

Any ideas?

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Variable inside an IF not showing correctly

#4 Post by Batcher » 09 Oct 2023 19:44

Code: Select all

setlocal EnableDelayedExpansion
set "wrkdir=%cd%"
set "IP=192.168.1.2"
set "IPADDRESS=\\%IP%"

if %_OS%==64 (
    set "FileName=SecureVNCPlugin64.dsm"
    if not exist "!FileName!" (
        set "file=!file!!lf!   ?    !FileName!"
        robocopy "%IPADDRESS%\Dnload\VNC" "!wrkdir!" "!FileName!"
        echo "FileName = -" !FileName!
        echo "wrkdir = -" !wrkdir!
        if not errorlevel 1 (
            goto :FileCopied
        )
    )
)

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#5 Post by Docfxit » 09 Oct 2023 22:02

Thank you for the reply...
What I'm seeing in the cmd windows now is:

Code: Select all

C:\Dnload\VNC>if 64 == 64 (
Set "FileName=SecureVNCPlugin64.dsm"
 if not exist "!FileName!" (
set "file=!file!!lf!   ?    !FileName!"
 robocopy "\\192.168.168.7\Dnload\VNC" "!wkdir!" "!FileName!"
 Echo "FileName = -" !FileName!
 Echo "wrkdir = -" !wrkdir!

 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
)
I'm not sure if we are on the same page.
What I would like to see is:

Code: Select all

C:\Dnload\VNC>if 64 == 64 (
Set "FileName=SecureVNCPlugin64.dsm"
 if not exist "SecureVNCPlugin64.dsm" (
set "file=!file!!lf!   ?    SecureVNCPlugin64.dsm"
 robocopy "\\192.168.168.7\Dnload\VNC" "C:\Dnload\VNC" "SecureVNCPlugin64.dsm"
 Echo "FileName = -" SecureVNCPlugin64.dsm
 Echo "wrkdir = -" C:\Dnload\VNC

 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
)
Thank you,

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Variable inside an IF not showing correctly

#6 Post by ShadowThief » 09 Oct 2023 22:44

If you're seeing !variable! instead of its actual value, then you're missing setlocal enabledelayedexpansion from your script. If you're seeing !variable! and its value, then you need to add @echo off to the top of your script.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#7 Post by Docfxit » 10 Oct 2023 08:55

Before this I always had Echo On with setlocal EnableDelayedExpansion.
I have now changed it to Echo Off.

With this code:

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion
if %_OS%==64 (
		Set "FileName=SecureVNCPlugin64.dsm"
		if not exist "!FileName!" (
			set "file=!file!!lf!   ?    !FileName!"
			robocopy "%IPADDRESS%\Dnload\VNC" "!wkdir!" "!FileName!"
			Echo "FileName = -" !FileName!
			Echo "wrkdir = -" !wrkdir!
			Echo "wrkdir = -" !!wrkdir!!
			Echo "wrkdir = -" %wrkdir%
			Echo "wrkdir = -" %%wrkdir%%
			Echo "FileName = -" "!FileName!"
			Echo "wrkdir = -" "!wrkdir!"
			Echo "wrkdir = -" "!!wrkdir!!"
			Echo "wrkdir = -" "%wrkdir%"
			Echo "wrkdir = -" "%%wrkdir%%"
			@Pause>nul|(@color 17&echo  3-%_OS%==64&@color)
			IF NOT ERRORLEVEL 1 GOTO FileCopied)
)
I get this in the cmd window when I run it:

Code: Select all

"FileName = -" SecureVNCPlugin64.dsm
"wrkdir = -"
"wrkdir = -"
"wrkdir = -"
"wrkdir = -" %wrkdir%
"FileName = -" "SecureVNCPlugin64.dsm"
"wrkdir = -" ""
"wrkdir = -" ""
"wrkdir = -" ""
"wrkdir = -" "%wrkdir%"
 3-64==64
I'm running this in Windows 10 22H2 with all updates

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Variable inside an IF not showing correctly

#8 Post by miskox » 10 Oct 2023 10:32

Code: Select all

set wkdir=%cd%
			Echo "wRkdir = -" !wRkdir!
wkdir and wRkdir variables.

Saso

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Variable inside an IF not showing correctly

#9 Post by Batcher » 10 Oct 2023 21:14

Two different ways to delay expansion, no matter you're running XP / Vista / Win7 / Win10 / Win11, no matter which updates.

1. call + %%wrkdir%%

Code: Select all

@echo off
for %%i in (A B C) do (
    set "wrkdir=%%i"
    call echo,%%wrkdir%%
)
pause
2. setlocal EnableDelayedExpansion + !wrkdir!

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%i in (A B C) do (
    set "wrkdir=%%i"
    echo,!wrkdir!
)
pause

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#10 Post by Docfxit » 10 Oct 2023 22:50

miskox wrote:
10 Oct 2023 10:32

Code: Select all

set wkdir=%cd%
			Echo "wRkdir = -" !wRkdir!
wkdir and wRkdir variables.

Saso
I really don't understand why a batch file won't run correctly when I don't spell variables correctly.
The DOS scripting language is very old. In this day and age it would be nice if it would tell me I made a blundering error and tell me how to fix it.
I can't believe I did that. It's amazing how things work so well when you spell variables correctly.

Thank you very much for catching that.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#11 Post by Docfxit » 12 Oct 2023 15:47

When I use this code with xcopy the Errorlevel is "0":

Code: Select all

:FileCopied
Echo ~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~
set "file="
if not exist "%FileName%" (
	set "file=!file!!lf!   ?    %FileName%"
	xcopy /y /f "Z:\%FileName%" "%wkdir%"
	Echo %ERRORLEVEL%
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
This is what shows in the cmd window:

Code: Select all

C:\Dnload\VNC>Echo ~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~
~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~

C:\Dnload\VNC>set "file="

C:\Dnload\VNC>if not exist "VNC UltraVNC_1_4_06_X64_Setup.exe" (
set "file=!file!!lf!   ?    VNC UltraVNC_1_4_06_X64_Setup.exe"
 xcopy /y /f "Z:\VNC UltraVNC_1_4_06_X64_Setup.exe" "C:\Dnload\VNC\"
 Echo 0
 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
Z:\VNC UltraVNC_1_4_06_X64_Setup.exe -> C:\Dnload\VNC\VNC UltraVNC_1_4_06_X64_Setup.exe
1 File(s) copied
0

C:\Dnload\VNC>Echo ~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~
~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~

C:\Dnload\VNC>set "file="

C:\Dnload\VNC>if not exist "VNC UltraVNC_1_4_06_X64_Setup.exe" (
set "file=!file!!lf!   ?    VNC UltraVNC_1_4_06_X64_Setup.exe"
 xcopy /y /f "Z:\VNC UltraVNC_1_4_06_X64_Setup.exe" "C:\Dnload\VNC\"
 Echo 0
 IF NOT ERRORLEVEL 1 GOTO FileCopied
)
The code passes FileCopied the first time
The code does copy the file the first time
On the line " IF NOT ERRORLEVEL 1 GOTO FileCopied it does GOTO FileCopied
The second time it goes through FileCopied the "if not exist" skips the code
Which is exactly what I want it to do.

When I use this code with RoboCopy the Errorlevel is "0":

Code: Select all

:FileCopied
Echo ~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~
set "file="
if not exist "%FileName%" (
	set "file=!file!!lf!   ?    %FileName%"
	robocopy "%IPADDRESS%\Dnload\VNC" "%wkdir%" "%FileName%"
	Echo %ERRORLEVEL%
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
This is what shows in the cmd window:

Code: Select all

C:\Dnload\VNC>Echo ~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~
~*~*~*~*~*~*~*~*~*~*~*~ I passed by FileCopied ~*~*~*~*~*~*~*~*~*~*~*~

C:\Dnload\VNC>set "file="

C:\Dnload\VNC>if not exist "VNC UltraVNC_1_4_06_X64_Setup.exe" (
set "file=!file!!lf!   ?    VNC UltraVNC_1_4_06_X64_Setup.exe"
 robocopy "\\192.168.1.127\Dnload\VNC" "C:\Dnload\VNC" "VNC UltraVNC_1_4_06_X64_Setup.exe"
 Echo 0
 IF NOT ERRORLEVEL 1 GOTO FileCopied
)

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Thursday, October 12, 2023 2:38:38 PM
   Source : \\192.168.1.127\Dnload\VNC\
     Dest : C:\Dnload\VNC\

    Files : VNC UltraVNC_1_4_06_X64_Setup.exe

  Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

                           1    \\192.168.1.127\Dnload\VNC\
100%        New File               5.0 m        VNC UltraVNC_1_4_06_X64_Setup.exe

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         1         1         0         0         0         0
   Bytes :    5.08 m    5.08 m         0         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00


   Speed :            11026743 Bytes/sec.
   Speed :             630.955 MegaBytes/min.
   Ended : Thursday, October 12, 2023 2:38:39 PM

0
The code passes FileCopied the first time
The code does copy the file the first time
On the line " IF NOT ERRORLEVEL 1 GOTO FileCopied it does NOT GOTO FileCopied
The ERRORLEVEL = 0
The code continues in the script
Which is NOT what I want it to do.

Why doesn't it GOTO FileCopied with RoboCopy like it does with XCopy?

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: Variable inside an IF not showing correctly

#12 Post by mataha » 12 Oct 2023 16:17

Docfxit wrote:
12 Oct 2023 15:47
Why doesn't it GOTO FileCopied with RoboCopy like it does with XCopy?
See robocopy exit codes.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Variable inside an IF not showing correctly

#13 Post by Docfxit » 12 Oct 2023 17:22

Thanks for the reply...

I saw:
Error Meaning if set

0 No errors occurred, and no copying was done.
The source and destination directory trees are completely synchronized.

I'm getting ERRORLEVEL = 0 which says "no copying was done"
Yet it does copy the file.
My code is:
"IF NOT ERRORLEVEL 1 GOTO FileCopied"
It is NOT ERRORLEVEL 1

I changed:
IF NOT ERRORLEVEL 1 GOTO FileCopied
To:
IF %ERRORLEVEL% LSS 8 GOTO FileCopied

And that fixed that problem.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Variable inside an IF not showing correctly

#14 Post by OJBakker » 13 Oct 2023 04:58

Your problem is that you are using %errorlevel% in a () codeblock.
Because of the codeblock %errorlevel% will give the value of the errorlevel special variable at the start of the codeblock and not the errorlevel as returned by xcopy or robocopy.
Change the line echo %errorlevel% to echo !errorlevel! or call echo %%errorlevel%% and you will see the actual errorlevel returned by xcopy/robocopy.
The old syntax for using errorlevel uses the actual value so this works:

Code: Select all

If not errorlevel 1 goto ...
The newer syntax using %errorlevel% will not use the actual value because of the codeblock.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Variable inside an IF not showing correctly

#15 Post by miskox » 13 Oct 2023 07:16

One thing to add:

Code: Select all

robocopy "%IPADDRESS%\Dnload\VNC" "%wkdir%" "%FileName%"
	Echo %ERRORLEVEL%
	IF NOT ERRORLEVEL 1 GOTO FileCopied
ERRORLEVEL that is compared in this case is the one from the ECHO command and it would probably always return success. You would have to use something like this:

Code: Select all

set errlev=%errorlevel%&REM or !errorlevel! - depends where/how you use it
and then check the %errlev% variable.

Saso

Post Reply