Search code examples
windowsbatch-fileechonul

Create A Batch File Within Batch File - Echo ">NUL"


Situation:

  • I have a text log file that gets updated frequently.
  • I already have a batch file that can check it manually.
  • I want it (upon start-up of batch) to create a new .bat.
  • I'm having problems exporting/creating the "timeOut /t 2 >NUL" part.
  • It won't export the >NUL part; leaves a blank space.
  • Example: timeout /t 2 /noBreak

I can't seem to figure out any way to export/echo out that ">NUL" snippet.. Any input would be appreciated!

echo mode con:cols=80 lines=28 >> %UserProfile%\Desktop\observeLog.bat
echo @echo off >> %UserProfile%\Desktop\observeLog.bat
echo Title Error Log >> %UserProfile%\Desktop\observeLog.bat
echo :startLogObserve >> %UserProfile%\Desktop\observeLog.bat
echo type %UserProfile%\Desktop\testLog.txt >> %UserProfile%\Desktop\observeLog.bat
echo timeout /t 2 >NUL /noBreak >> %UserProfile%\Desktop\observeLog.bat
echo cls >> %UserProfile%\Desktop\observeLog.bat
echo goTo :startLogObserve >> %UserProfile%\Desktop\observeLog.bat

Solution

  • You need to escape special characters, so the parser knows which parts you want to echo and which ones you want to be executed.

    Btw. It's easier to redirect a complete block

    (
      echo mode con:cols=80 lines=28
      echo @echo off
      echo Title Error Log
      echo :startLogObserve
      echo type "%%UserProfile%%\Desktop\testLog.txt"
      echo timeout /t 2 ^>NUL /noBreak
      echo cls
      echo goTo :startLogObserve
    ) > %UserProfile%\Desktop\observeLog.bat
    

    I also changed the type %UserProfile%.. to type "%%UserProfile%%..." else you get the expanded version in your observeLog.bat The quotes are useful when %UserProfile% contains spaces or special characters