locked
How do i log my Batch file? RRS feed

  • Question

  • I created a script and i scheduled it to run every day.

    Some days the script doesn't succed and i need to know why.

    The script's porpuse is to compress and then move the Sql's database to another location.

    How do i make it create a log so i can see where it failed?

    This is the script:


    "c:\Program Files\WinRAR\Rar.exe" a -ag+YYYYMMDDHHMM -r e:\DRP\2ZIPFile\full.rar E:\priority\backup

    move E:\DRP\2ZIPFile\*.rar e:\DRP\5EncryptedReady

    E:\DRP\bmail -s "full domain name.com" -t Mail@domain.com -f Drp-Prt@domain.com -h -a "Full rar finished successfully"

    del E:\DRP\3ZIPReady\*.* /q

    move E:\DRP\5EncryptedReady\*.rar e:\DRP\6Readytoupload

    E:\DRP\robocopy e:\DRP\6Readytoupload\. \\uk1sxwn00123\e$\script-backup

    E:\DRP\bmail -s "full domain name.com" -t MAIL@domain.com -f Drp-Prt@domain.com -h -a "Full uploaded to SFTP successfully"


    del E:\DRP\6Readytoupload\*.* /q

    Thanks in advance


    • Edited by hrefayi Monday, March 24, 2014 9:40 AM auto correct
    Monday, March 24, 2014 9:39 AM

Answers

  • Thank you.

    But i need that the logs wont overwrite the old ones.

    Forgot to mention, im running it on win server 2003

    put date and time as variable in the logfile name

    set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    and then

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ECHO your_batch.bat >> %LOGFILE%


    • Proposed as answer by Maffiow Monday, March 24, 2014 12:36 PM
    • Marked as answer by hrefayi Monday, March 24, 2014 12:51 PM
    Monday, March 24, 2014 12:33 PM

All replies

  • Did you tried the below

    yourbatchfile.bat >> C:\Temp\log.txt

    Ensure ECHO is turned on.

    It's good to refer this link


    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Monday, March 24, 2014 9:52 AM
    Monday, March 24, 2014 9:50 AM
  • A scheduled task creates a log of its results..  Just look at the results page of the task.  If task history is not enabled enable it first.If you want more information just send output to a file:

    echo The is my message >> c:\test\mylog.yxy


    ¯\_(ツ)_/¯

    Monday, March 24, 2014 9:51 AM
  • create a batch where you start your current batch file and add this

    @echo off
     SET LOGFILE=myLOG.LOG
     ECHO your_batch.bat >> %LOGFILE%

    Note : If you have “Trace32″ log viewer you can directly lauch it loaded with your log file

    START trace32.exe %LOGFILE%


    • Proposed as answer by Maffiow Monday, March 24, 2014 10:01 AM
    Monday, March 24, 2014 9:53 AM
  • Thank you.

    But i need that the logs wont overwrite the old ones.

    Forgot to mention, im running it on win server 2003

    Monday, March 24, 2014 12:15 PM
  • Thank you.

    But i need that the logs wont overwrite the old ones.

    Forgot to mention, im running it on win server 2003

    put date and time as variable in the logfile name

    set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    and then

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ECHO your_batch.bat >> %LOGFILE%


    • Proposed as answer by Maffiow Monday, March 24, 2014 12:36 PM
    • Marked as answer by hrefayi Monday, March 24, 2014 12:51 PM
    Monday, March 24, 2014 12:33 PM
  • Thank you.

    But i need that the logs wont overwrite the old ones.

    Forgot to mention, im running it on win server 2003

    put date and time as variable in the logfile name

    set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    and then

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ECHO your_batch.bat >> %LOGFILE%


    When i run it, it saves the log by the name "MyLOG-Mon.txt" instead "myLOG-(date\time\day\).txt"
    Monday, March 24, 2014 1:00 PM
  • Can you post your script so I can see where it goes wrong as I get following output when testing it

    H:\>set SAVESTAMP=%DATE:/=-%@%TIME::=-%
    
    H:\>SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    
    H:\>echo %logfile%
    myLOG-ma 24-03-2014@14-04-03,81.LOG
    
    H:\>
    
    
    

    Monday, March 24, 2014 1:05 PM

  • set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ping_test.bat >> E:\DRP\logs\%logfile%
    Monday, March 24, 2014 1:09 PM

  • set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ping_test.bat >> E:\DRP\logs\%logfile%
    set SAVESTAMP=%DATE:/=-%@%TIME::=-%
    set LOGFILE=myLOG-%SAVESTAMP%.LOG
    ping_test.bat >> E:\DRP\logs\"%logfile%"



    put your last %logfile% between " so the space after the day name isn't the end of the filename
    • Proposed as answer by Maffiow Monday, March 24, 2014 1:31 PM
    Monday, March 24, 2014 1:31 PM

  • set SAVESTAMP=%DATE:/=-%@%TIME::=-%

    SET LOGFILE=myLOG-%SAVESTAMP%.LOG
    ping_test.bat >> E:\DRP\logs\%logfile%

    set SAVESTAMP=%DATE:/=-%@%TIME::=-%
    set LOGFILE=myLOG-%SAVESTAMP%.LOG
    ping_test.bat >> E:\DRP\logs\"%logfile%"



    put your last %logfile% between " so the space after the day name isn't the end of the filename
    Thank you. it works now :)
    • Proposed as answer by Maffiow Monday, March 24, 2014 3:46 PM
    Monday, March 24, 2014 2:31 PM