title: Batch file log function for printing command output to both log file and screen
body: <p>I found there are some topics similar to this problem but not exactly what I want, so I raise this topic.</p>
  
  <p>I want to create a log function for printing message to both formatted log file and console output. The function is as below:</p>
  
  <pre><code>:LOGDEBUG
  @echo DEBUG: %~1
  if NOT EXIST %LOG_FILE% exit /b 1
  @echo [%date - %time%] DEBUG: %~1 &gt;&gt; %LOG_FILE% 2&gt;&amp;1
  exit /b 0
  </code></pre>
  
  <p>And I try to use it for printing the command execution output and if the output contains special character like "&lt;" and ">", this function doesn't work well and prompt "The system cannot find the file specified". My code for executing a command is below:</p>
  
  <pre><code>for /f "usebackq delims=" %%a in (`dir c:\temp`) do (
      CALL :LOGDEBUG "%%a"
  )
  </code></pre>
  
  <p>However, when I use "echo" command directly instead of the log function, the output can be printed correctly on the console. Like the following code:</p>
  
  <pre><code>for /f "usebackq delims=" %%a in (`dir c:\temp`) do (
      echo %%a
  )
  </code></pre>
  
  <p>May I know what is the problem, and how can I print the output correctly by using the log function? Thanks</p>