Logging a batch file

0

I have the following problem:

We receive a daily backup of the 4AM database at 5AM, and we have a batch job running at 6 AM to get the database running on a VM.

In Task Scheduler, everything is ok, and it runs every day, however we want to have a log of the actions that the batch file does.

Is there any way to do this?

What I've already tried:

  • Call the batch with another batch that writes the output to a file. The scheduler causes the task to run indefinitely.
  • Call a powershell script to execute the batch and write to a file. Same problem as above
  • Surround batch statements with () and thereby make them print the results of the statements. >"arquivo" (instruções)
  • asked by anonymous 04.01.2019 / 18:04

    1 answer

    1
      

    If I understood, it would be simpler to add if in a conditional loop to redirect a call to itself "set" the file   log:

         

    Insert these lines in the .bat file of your .bat file:

       
    @echo off && setlocal enableextensions enabledelayedexpansion
    
    if /i "./!_log!/." equ ".//." (
    
     echo/Chamada do lote iniciada: !date! - / - !time!
     set _log="%temp%\log_bat.log"&& echo/>!_log! & "%~0" >>!_log! & exit /b
    
    ) else (
    
     endlocal
    
    )
    
    :_start
    
    :: seu código do batch começa nesse ponto salvando todas as saídas das execuções para o arquivo “%temp%\log_bat.log”
    
        
    05.01.2019 / 23:11