Pan24112012
Pan24112012

Reputation: 307

Append current date and time in filename with batch file

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

Upvotes: 5

Views: 36315

Answers (2)

Ashish Singhal
Ashish Singhal

Reputation: 425

To append date and time before file in batch script, it will work

set "hr=%time:~0,2%-%time:~3,2%-%time:~6%" set "filename=%date%_%hr%_response_report.csv" echo >> %filename%

Upvotes: 0

dbenham
dbenham

Reputation: 130809

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"

Upvotes: 10

Related Questions