Page 1 of 2

Variable inside an IF not showing correctly

Posted: 08 Oct 2023 13:16
by Docfxit
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?

Re: Variable inside an IF not showing correctly

Posted: 08 Oct 2023 20:03
by Batcher

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
        )
    )
)

Re: Variable inside an IF not showing correctly

Posted: 09 Oct 2023 16:17
by Docfxit
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?

Re: Variable inside an IF not showing correctly

Posted: 09 Oct 2023 19:44
by Batcher

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
        )
    )
)

Re: Variable inside an IF not showing correctly

Posted: 09 Oct 2023 22:02
by Docfxit
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,

Re: Variable inside an IF not showing correctly

Posted: 09 Oct 2023 22:44
by ShadowThief
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.

Re: Variable inside an IF not showing correctly

Posted: 10 Oct 2023 08:55
by Docfxit
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

Re: Variable inside an IF not showing correctly

Posted: 10 Oct 2023 10:32
by miskox

Code: Select all

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

Saso

Re: Variable inside an IF not showing correctly

Posted: 10 Oct 2023 21:14
by Batcher
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

Re: Variable inside an IF not showing correctly

Posted: 10 Oct 2023 22:50
by Docfxit
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.

Re: Variable inside an IF not showing correctly

Posted: 12 Oct 2023 15:47
by Docfxit
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?

Re: Variable inside an IF not showing correctly

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

Re: Variable inside an IF not showing correctly

Posted: 12 Oct 2023 17:22
by Docfxit
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.

Re: Variable inside an IF not showing correctly

Posted: 13 Oct 2023 04:58
by OJBakker
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.

Re: Variable inside an IF not showing correctly

Posted: 13 Oct 2023 07:16
by miskox
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