Error level IF/THEN

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RedBeard
Posts: 1
Joined: 26 Nov 2020 13:46

Error level IF/THEN

#1 Post by RedBeard » 26 Nov 2020 13:54

HELLO! Thanks for coming by, I have a simple script that has an error level If/Then function but when running the script I get both error based messages upon completion when I should only be seeing one based upon its success or failure. I am not a "coder" I have no real experience or formal education, it's just something I enjoy tinkering with. That said, I have tried reading and tweaking the script on my own to no success, which is why I am here.

Code: Select all

@ECHO OFF

SET /p HOSTFILE=Host File Name?&CLS

SET /p HIDEFILE=File To Hide?&CLS

SET /p OUTPUT=Output File Name?&CLS

SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS

COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >null

if %ERRORLEVEL% EQU 0 GOTO continue

if %ERRORLEVEL% EQU 2 GOTO error

:continue
    ECHO %OUTPUT%%EXTENSION% Created.

:error
    ECHO File Not Created.

TIMEOUT /T 2 >null

WiVi71
Posts: 19
Joined: 06 Jan 2020 02:33

Re: Error level IF/THEN

#2 Post by WiVi71 » 27 Nov 2020 11:15

RedBeard wrote:
26 Nov 2020 13:54
HELLO! Thanks for coming by, I have a simple script that has an error level If/Then function but when running the script I get both error based messages upon completion when I should only be seeing one based upon its success or failure. I am not a "coder" I have no real experience or formal education, it's just something I enjoy tinkering with. That said, I have tried reading and tweaking the script on my own to no success, which is why I am here.

Code: Select all

@ECHO OFF

SET /p HOSTFILE=Host File Name?&CLS

SET /p HIDEFILE=File To Hide?&CLS

SET /p OUTPUT=Output File Name?&CLS

SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS

COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >null

if %ERRORLEVEL% EQU 0 GOTO continue

if %ERRORLEVEL% EQU 2 GOTO error

:continue
    ECHO %OUTPUT%%EXTENSION% Created.

:error
    ECHO File Not Created.

TIMEOUT /T 2 >null
First, Use "nul" instead of "null".
redirecting to null will redirect output to file "null".

Second, In most programs, Output from a console (Command Prompt) application or command is often sent to two separate streams. The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR).
To redirect all outputs to nul, you should redirect both STDOUT and STDERR.

So use ">nul 2>nul" or ">nul 2>&1" instead.

More informations on:
https://docs.microsoft.com/en-us/troubl ... and-prompt
https://ss64.com/nt/syntax-redirection.html

Your code:

Code: Select all

:: Fixed bugs and added some stuff
@ECHO OFF
setlocal enableDelayedExpansion
for %%A in (
	"HOSTFILE=Host File Name"
	"HIDEFILE=File To Hide"
	"OUTPUT=Output File Name"
	"EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)"
) DO (
	SET /p %%~A?
	CLS
)

if defined EXTENSION if not "!EXTENSION:~0,1!" == "." set "EXTENSION=.!EXTENSION!"
COPY /b "%HOSTFILE%" + "%HIDEFILE%" !OUTPUT!!EXTENSION! >nul 2>&1
if %ERRORLEVEL% EQU 0 GOTO continue
if %ERRORLEVEL% EQU 2 GOTO error

:continue
    ECHO !OUTPUT!!EXTENSION! Created.
    EXIT /B

:error
    ECHO File Not Created.
    EXIT /B

TIMEOUT /T 2 >nul
And also, you can consider File/folder chooser dialog to select files/folders.
https://stackoverflow.com/questions/158 ... tch-script
Last edited by WiVi71 on 27 Nov 2020 21:14, edited 1 time in total.

penpen
Expert
Posts: 1886
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Error level IF/THEN

#3 Post by penpen » 27 Nov 2020 18:32

The batch command line interpreter processes one line after another. So if it reaches your first errorlevel-based message, it simply proceeeds by processing the empty line after that, then the line with the :error-label and then the second errorlevel-based message.


penpen

Post Reply