Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append current date and time in filename with batch file

Tags:

cmd

i try to append the current date and time in the filename of my log file. it's working very well but only from 10:00:00 am never before and i did not try when it stops to work maybe at 00:00:00 i don't know.

how can i resolve this problem, please?

Here is the batch file:

echo off
cls
date /t >> d:\Folder_Log\log.txt
time /t >> d:\Folder_Log\log.txt
echo Starting execution >> d:\Folder_Log\log.txt
java -jar d:\NetBeansProjects\myapplication\dist\myapplication.jar 2>> d:\Folder_Log\log.txt
echo Finished execution >> d:\Folder_Log\log.txt
:: Writing log file in D:\Folder_Log\Log.txt***********************************************************
Timeout /t 3 /nobreak >nul
ren d:\Folder_Log\log.txt-log-%date:~0,2%-%date:~3,2%-%date:~6,4%-%time:~0,2%-%time:~3,2%-%time:~6,2%.txt

Thank you

like image 732
Pan24112012 Avatar asked Dec 18 '12 03:12

Pan24112012


1 Answers

If the hour is less than 10, then you get a space in your name. Names with spaces must be quoted.

ren "d:\Folder_Log\log.txt" "log-%date:~0,2%-%date:~3,2%-%date:~6,4%-%time:~0,2%-%time:~3,2%-%time:~6,2%.txt"
like image 120
dbenham Avatar answered Oct 28 '22 09:10

dbenham