Finding an error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Finding an error

#1 Post by Docfxit » 07 Nov 2022 16:11

I am getting an error that is ending this script. I don't see what the error is.
The script is ending on this line:

Code: Select all

xcopy /y /f "%wkdir%ultravnc.ini" "%wkdir%VNCultravnc.ini*"

Code: Select all

@echo On
:: The following code will make sure this is running as Administrator
(
>nul 2>&1 %__APPDIR__%net.exe session
) || (
	echo Set UAC = CreateObject^("Shell.Application"^) > "%tmp%\uac.vbs"
	echo UAC.ShellExecute "%~snx0", "%*", "%~sdp0", "runas", 1 >> "%tmp%\uac.vbs"
) && (
	"%tmp%\uac.vbs" && exit /b
)
if exist "%tmp%\uac.vbs" del /f /q "%tmp%\uac.vbs"
:: The Above code will make sure this is running as Administrator

:: This will install UltraVNC on a PC remotely.
::

::
@echo on
Set Port=5911
set "_os=64"
if "%PROCESSOR_ARCHITECTURE%"=="x86" (if not defined PROCESSOR_ARCHITEW6432 set "_os=32")
::if %_OS%==32 echo This is a 32bit operating system
	if %_OS%==64 (
	echo This is a 64bit operating system 
	Set FileName=VNC UltraVNC_1_3_81_X64_Setup.exe
	) else (
	Set FileName=VNC UltraVNC_1_3_81_X86_Setup.exe
	)
set wkdir=%~dp0
cd /D %wkdir%
(set lf=^
	%= creates a line feed, do not alter =%
)
@echo on 
setlocal EnableDelayedExpansion
::net use z: \\yourserver\sharename
::copy "z:\FolderName" "C:\TEST_BACKUP_FOLDER"
net use * /d /y
net use Z: \\192.168.168.7\Dnload\VNC

:FileCopied
set "file="
if not exist "%FileName%" (
	set "file=!file!!lf!   ?    %FileName%"
	xcopy /y /f "Z:\%FileName%" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
if not exist "VNCsetup.inf" (
	set "file=!file!!lf!   ?    VNCsetup.inf"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\VNCsetup.inf" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
if not exist "VNCsetup.reg" (
	set "file=!file!!lf!   ?    VNCsetup.reg"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\VNCsetup.reg" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
if not exist "VNCultravnc.ini" (
	set "file=!file!!lf!   ?    VNCultravnc.ini"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\VNCultravnc.ini" "%wkdir%"
		if not exist "BatchSubstitute.bat" (
		set "file=!file!!lf!   ?    BatchSubstitute.bat"
		xcopy /y /f "\\192.168.168.7\Dnload\VNC\BatchSubstitute.bat" "%wkdir%"
			)
	BatchSubstitute.bat "5905" !Port! VNCultravnc.ini>ultravnc.ini
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
if %_OS%==64 (
		if not exist "SecureVNCPlugin64.dsm" (
		set "file=!file!!lf!   ?    SecureVNCPlugin64.dsm"
		xcopy /y /f "\\192.168.168.7\C\Programs\UltraVNC\SecureVNCPlugin64.dsm" "%wkdir%"
		IF NOT ERRORLEVEL 1 GOTO FileCopied)
) else (
		if not exist "BatchSubstitute.bat" (
			set "file=!file!!lf!   ?    BatchSubstitute.bat"
			xcopy /y /f "\\192.168.168.7\Dnload\VNC\BatchSubstitute.bat" "%wkdir%"
			)
		BatchSubstitute.bat "SecureVNCPlugin64" SecureVNCPlugin VNCultravnc.ini>ultravnc.ini
		xcopy /y /f "%wkdir%ultravnc.ini" "%wkdir%VNCultravnc.ini*"
		IF NOT ERRORLEVEL 1 GOTO FileCopied)
if not exist "20121120_Server_ClientAuth.pubkey" (
	set "file=!file!!lf!   ?    20121120_Server_ClientAuth.pubkey"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\20121120_Server_ClientAuth.pubkey" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
This is the code from the machine I am getting the error on:
VNCInstallBat.jpg
VNCInstallBat.jpg (232.21 KiB) Viewed 12841 times

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

Re: Finding an error

#2 Post by aGerman » 08 Nov 2022 10:44

Open a CMD prompt, navigate to the script folder using the CD command, and run your script by calling it from the prompt.

FWIW What should people ever do with a screenshot of code? It's like a picture of a bar of soap. Try to wash your fingers with it :lol:

Steffen

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

Re: Finding an error

#3 Post by miskox » 08 Nov 2022 11:38

Use ECHO ON. This is very useful.
And let me translate aGerman's post: put your code as text between code tags.
Maybe '!' and '%' are causing problems?

Saso

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

Re: Finding an error

#4 Post by Docfxit » 08 Nov 2022 13:35

This is the cmd window when I run it:
Please note: The batch file ends with no error.
Please note: I do have "ECHO ON" in a few places. I don't have "Echo Off" anyplace in the script.
VNCInstallBatError.jpg
VNCInstallBatError.jpg (94.18 KiB) Viewed 12821 times
This is the section of code shown in the cmd window:

Code: Select all

if %_OS%==64 (
		if not exist "SecureVNCPlugin64.dsm" (
		set "file=!file!!lf!   ?    SecureVNCPlugin64.dsm"
		xcopy /y /f "\\192.168.168.7\C\Programs\UltraVNC\SecureVNCPlugin64.dsm" "%wkdir%"
		IF NOT ERRORLEVEL 1 GOTO FileCopied)
) else (
		if not exist "BatchSubstitute.bat" (
			set "file=!file!!lf!   ?    BatchSubstitute.bat"
			xcopy /y /f "\\192.168.168.7\Dnload\VNC\BatchSubstitute.bat" "%wkdir%"
			)
		BatchSubstitute.bat "SecureVNCPlugin64" SecureVNCPlugin VNCultravnc.ini>ultravnc.ini
		xcopy /y /f "%wkdir%ultravnc.ini" "%wkdir%VNCultravnc.ini*"
		IF NOT ERRORLEVEL 1 GOTO :FileCopied )
if not exist "20121120_Server_ClientAuth.pubkey" (
	set "file=!file!!lf!   ?    20121120_Server_ClientAuth.pubkey"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\20121120_Server_ClientAuth.pubkey" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
if not exist "20121120_Viewer_ClientAuth.pkey" (
	set "file=!file!!lf!   ?    20121120_Viewer_ClientAuth.pkey"
	xcopy /y /f "\\192.168.168.7\Dnload\VNC\20121120_Viewer_ClientAuth.pkey" "%wkdir%"
	IF NOT ERRORLEVEL 1 GOTO FileCopied)
If I call the bat file that has the error with this bat file:

Code: Select all

call "VNC UltraVnc silent install.bat"  
::cls
echo An error occured!
<Your commands>
pause
cmd/k
I get this in the cmd window:
VNCInstallBatError4.jpg
VNCInstallBatError4.jpg (101.37 KiB) Viewed 12821 times
Please Note: I have put a space after the tag before the run above:
":FileCopied "

I have put a space after "FileCopied " on this line:
IF NOT ERRORLEVEL 1 GOTO :FileCopied )

I have put a colon before "FileCopied " on this line:
IF NOT ERRORLEVEL 1 GOTO :FileCopied )
Even though it works without the colon and the space after the label in other code identical to this.

I have added garbage in front of the tag in case it had a block boundary problem:
":FileCopied "

Code: Select all

REM Had To put this here so it worked because it was straddling a block bounry, hopefuly lengthening the EOL will push it over it.
REM Had To put this here so it worked because it was straddling a block bounry, hopefuly lengthening the EOL will push it over it.
:FileCopied 

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

Re: Finding an error

#5 Post by aGerman » 08 Nov 2022 14:11

I've seen things like that if some lines in the script end with only LF instead of CRLF.

Steffen

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

Re: Finding an error

#6 Post by Docfxit » 08 Nov 2022 14:17

aGerman wrote:
08 Nov 2022 14:11
I've seen things like that if some lines in the script end with only LF instead of CRLF.

Steffen
Thanks for the reply...

That's why I posted what shows in my text editor with CRLF in every line.

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

Re: Finding an error

#7 Post by aGerman » 08 Nov 2022 14:35

Looks like NP++. To ensure really every line ends with CRLF, double click on "Windows (CR LF)" down in the status bar, turn it into "UNIX (LF)", and back to "Windows (CR LF)". If this doesn't help I'm afraid I have no further idea.

Steffen

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

Re: Finding an error

#8 Post by Docfxit » 08 Nov 2022 14:46

aGerman wrote:
08 Nov 2022 14:35
Looks like NP++. To ensure really every line ends with CRLF, double click on "Windows (CR LF)" down in the status bar, turn it into "UNIX (LF)", and back to "Windows (CR LF)". If this doesn't help I'm afraid I have no further idea.

Steffen
I didn't know you could do that. Thanks.
That brought me to the attention of the option to the right of that. It shows it's in Korean character set. That can't be right.
I'm in the USA. What should the character set be? With NP++ open with no files open it defaults to UTF-8.

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

Re: Finding an error

#9 Post by miskox » 08 Nov 2022 15:01

I would suggest you try to debug it like this: make more IF statements: instead of IF ELSE you make two IF statements (so IFs will be easier to read and there would be less parentheses - maybe they are causing the problem):

Code: Select all

if %_OS%==64 (
)
if %_OS%==32 (
)
If this still doesn't work use REM in your commands to see where the error is:

Code: Select all

REM		if not exist "SecureVNCPlugin64.dsm" (
REM 		set "file=!file!!lf!   ?    SecureVNCPlugin64.dsm"
REM		xcopy /y /f "\\192.168.168.7\C\Programs\UltraVNC\SecureVNCPlugin64.dsm" "%wkdir%"
		echo X - should return errorlevel=0 so next IF should be TRUE
		IF NOT ERRORLEVEL 1 GOTO FileCopied)
Maybe moving the last ')' to a new line after each FIleCopied?

Do you really need IF NOT ERRORLEVEL? Or you can you use IF NOT "%ERRORLEVEL%"=="1" or maybe IF "%ERRORLEVEL%"=="0"?

Code: Select all

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT               Specifies that Windows should carry out
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code *equal to or greater* than the number
                    specified.
Saso

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

Re: Finding an error

#10 Post by miskox » 08 Nov 2022 15:02

Docfxit wrote:
08 Nov 2022 14:46
aGerman wrote:
08 Nov 2022 14:35
Looks like NP++. To ensure really every line ends with CRLF, double click on "Windows (CR LF)" down in the status bar, turn it into "UNIX (LF)", and back to "Windows (CR LF)". If this doesn't help I'm afraid I have no further idea.

Steffen
I didn't know you could do that. Thanks.
That brought me to the attention of the option to the right of that. It shows it's in Korean character set. That can't be right.
I'm in the USA. What should the character set be? With NP++ open with no files open it defaults to UTF-8.
Open it with NOTEPAD (windows executable) and save as ANSI and try again.

Saso

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

Re: Finding an error

#11 Post by aGerman » 08 Nov 2022 17:45

NP++ is just completely misinterpreting your frame characters as shown in your other thread:
viewtopic.php?f=3&t=10559&p=67713#p67713

Don't worry, it's just weirder than other editors would interpret them. They would most likely recognize ANSI which is Windows-1252 in your case where those frames look like

Code: Select all

echo    ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
echo    ?                                
echo    ?  The files "%file%" do not exist in this directory
echo    ?  Move the above files into %CD% 
echo    ?                                
echo    ?                                
echo    ?                                
echo    ?  Press any key to close this window                              
echo    ?                                
echo    ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
Still weird. Your CMD defaults to interpret the characters Codepage 437 encoded which is called OEM-US in NP++. And indeed you'll see the right frame characters as soon as you change the encoding to OEM-US. However, next time that you open the file, NP++ will recognize Korean again :lol:

To get around it, use the actual frame characters ...

Code: Select all

echo    ┌──────────────────────────────────────────────────────────────┐
echo    ?                                
echo    ?  The files "%file%" do not exist in this directory
echo    ?  Move the above files into %CD% 
echo    ?                                
echo    ?                                
echo    ?                                
echo    ?  Press any key to close this window                              
echo    ?                                
echo    └──────────────────────────────────────────────────────────────┘
... safe the script UTF-8 encoded (not UTF-8 BOM !), and insert ...

Code: Select all

>nul chcp 65001
.. after the @echo off

Steffen

Post Reply