title: How to update %time% variable in Batch Script
body: <p>I have a batch script for backing up databases. I echo the %time% that script starts and then the %time% it ends to a log file. Even though the script takes 5 minutes to backup our databases the end time is identical to the start time.</p>
  
  <pre><code>@echo off
  
  :: Credentials preconfigured for backup-operator
  net use y: \\172.16.104.201\Backups
  
  :: Date in format YYYY.MM.DD
  set DATESTAMP=%DATE:~-4%.%DATE:~3,2%.%DATE:~0,2%
  set LOCAL_DIR=F:\Backups\
  set EXTERN_DIR=Y:\DB3\
  
  :: Output all to txt
  &gt;"%LOCAL_DIR%SQLBackups-%DATESTAMP%.txt" (
       echo.
       echo ------------------------------------------------------------------------------
       echo -- Starting SQL Backups %date% %time% --
       echo ------------------------------------------------------------------------------
       echo.
  
      :: Backup and Copy loop
      setlocal enabledelayedexpansion 
      for %%i in (
          DB1 
          DB2
          DB3
          DB4 etc...
      ) do (
          set DATABASENAME=%%i
          set BACKUPFILENAME=!LOCAL_DIR!!DATABASENAME!-!DATESTAMP!.bak
           echo.
           echo ------------------------------------------------------------------------------
           echo -- Backing Up Database !DATABASENAME! to local--
           echo ------------------------------------------------------------------------------
           echo.
          sqlcmd -E -S DB3 -d master -Q "BACKUP DATABASE [!DATABASENAME!] TO DISK = '!BACKUPFILENAME!' WITH DESCRIPTION = 'Full backup [!DATABASENAME!]', CHECKSUM, INIT, COMPRESSION"
           echo.
           echo.
           echo -- Copying !DATABASENAME! Backup to external--
          robocopy !LOCAL_DIR! !EXTERN_DIR! !DATABASENAME!-!DATESTAMP!.bak
           echo.
           echo -- !DATABASENAME! End --
           echo.
           echo.
      )
      endlocal
  
      :: Delete files older than -d days from local
      forfiles -p %LOCAL_DIR% -s -m *.bak -d "-180" /C "cmd /c echo @path &amp; del @path""
      forfiles -p %LOCAL_DIR% -s -m *.txt -d "-180" /C "cmd /c echo @path &amp; del @path""
       echo.
       echo.
       echo -- Script Complete %date% %time% --
       echo.
  )
  robocopy %LOCAL_DIR% %EXTERN_DIR% "SQLBackups-%DATESTAMP%.txt"
  net use y: /delete
  </code></pre>