0

I have been testing a batch file, that I used to see if my ideas work in batch. I had an idea to make a logging system, but if the file cant be found it doesn't show "The system cannot find the file specified."

I've tried type A6Test.log &if %ERRORLEVEL%==1 >nul and type A6Test.log ^&if %ERRORLEVEL%==1 >nul but the first just exited the file and the second displayed:

The system cannot find the file specified.
Error occurred while processing: A6Test.log.
The system cannot find the file specified.
Error occurred while processing: &If.
The system cannot find the file specified.
Error occurred while processing: 1.

All you need to recreate this is below.

@echo off
:A6
cls
echo Log file:
type "A6Test.log" ::Problem
set /p Log=
echo %Log%>>"A6Test.log" ::this echos %log% into the log file, "A6Test.log"
goto A6

I expected type "A6Test.log" ^&if %ERRORLEVEL%==1 >nul to display nothing if the file didnt exist but it showed:

The system cannot find the file specified.
Error occurred while processing: A6Test.log.
The system cannot find the file specified.
Error occurred while processing: &If.
The system cannot find the file specified.
Error occurred while processing: 1.
Unkn0wn
  • 1
  • 3
  • `if exist "%filename%" (type "%filename%>>wherever) else (echo "%filename%" is missing)` – Magoo May 31 '19 at 03:00
  • `>nul` redirects *Stdout* to `nul`. `2>nul` redirects *Stderr* to `nul`. Error messages usually are streamed to *Stderr*. – michael_heath May 31 '19 at 03:44
  • Possible duplicate of [Suppress command line output](https://stackoverflow.com/questions/1262708/suppress-command-line-output) – montonero May 31 '19 at 07:19
  • 1
    What is **`%ERRORCODE%`**? Is it related to **`%ERRORLEVEL%`**? – Compo May 31 '19 at 07:49
  • If **`Type "A6Test.log" 2>NUL`** is what you're wanting to use, you could use that with either **`ErrorLevel`**, **`%ERRORLEVEL%`**; or the **`&&`** and **`||`** operators. Alternatively you could simply use an **`If Exist "A6Test.log"`** statement instead. – Compo May 31 '19 at 10:32

0 Answers0