Jump to content

Appending Date to filenames


Recommended Posts

I currently have a batch file that does backup of some folders and piping the output to a file called BackupLog.txt. This file gets overwritten each time the backup task runs. I would like to append the date to this filename such that there will be a new output file each day. eg. BackupLog08052007.txt

Anyone can help me with the syntax? I currently have >> "D:\BackupLog.txt"

Thanks!!

Link to comment
Share on other sites


I currently have a batch file that does backup of some folders and piping the output to a file called BackupLog.txt. This file gets overwritten each time the backup task runs. I would like to append the date to this filename such that there will be a new output file each day. eg. BackupLog08052007.txt

Anyone can help me with the syntax? I currently have >> "D:\BackupLog.txt"

Thanks!!

Here try this then add what you need to yours.

@Echo Off
CLS
Color B1
Mode 65,7
Title Date Demo
SET Y1=%date:~10,4%
SET M1=%date:~4,2%
SET D1=%date:~7,2%
SET C1=%M1%%D1%%Y1%
Echo.
Echo ^> Year %Y1%
Echo ^> Month %M1%
Echo ^> Day %D1%
Echo ^> Today %Date%
Set /P = ^<^> Press To Close Cmd Window %C1%

Link to comment
Share on other sites

@ECHO OFF

REM Backup "DB Folder"

XCOPY "C:\Program Files\McAfee\ePO\3.6.0\DB\*.*" "D:\TNSP Cardiff\Disaster Recovery\ePO DB\" /E /H /Y >> "D:\TNSP Cardiff\Disaster Recovery\ePO DB\BackupLogs.txt"

EXIT

--------------------

the above is what i have at the moment, how do i edit it to get my desired results?

thanks.

Link to comment
Share on other sites

That is correct :whistle:

Incidentally because people have different locales, the format of the date differs therefore creating different results. Here's mine:

th.95a3da14ee.png

My suggestion, being how I speak the language far more fluently would be this!

@ECHO OFF&SETLOCAL
FOR /F "TOKENS=1-3 DELIMS=-/." %%a IN (
'ECHO/EXIT^|%COMSPEC%/Q/K PROMPT $D') DO SET "D_=%%a%%b%%c"
ECHO/ Your date appendage is: %D_%

Link to comment
Share on other sites

That is correct :whistle:

Incidentally because people have different locales, the format of the date differs therefore creating different results. Here's mine:

th.95a3da14ee.png

My suggestion, being how I speak the language far more fluently would be this!

@ECHO OFF&SETLOCAL
FOR /F "TOKENS=1-3 DELIMS=-/." %%a IN (
'ECHO/EXIT^|%COMSPEC%/Q/K PROMPT $D') DO SET "D_=%%a%%b%%c"
ECHO/ Your date appendage is: %D_%

Nice Batch, Yzowl

My question is how would you remove the shortday name that appears at the beggining

of %D_%, I only ask because the poster wanted a format like this BackupLog08052007.txt.

This is what I got when I ran the script Your date appendage is: Wed 05092007, could you

use something like this to remove the shortname of the day on all locals.

@ECHO OFF&SETLOCAL
FOR /F "TOKENS=1-3 DELIMS=-/." %%a IN (
'ECHO/EXIT^|%COMSPEC%/Q/K PROMPT $D') DO SET "D_=%%a%%b%%c"
SET R1=%D_:~4,8%
ECHO/ Your date appendage is: %R1%
pause

This produced on my machine

Your date appendage is: 05092007

Would this work with all locals.

This is how I would do it using VBS Script

Dim Today, StrD, StrM, StrTD, StrY
Today = Date
StrD = Day(Today)
StrM = Month(Today)
StrY = Year(Today)
If Len(StrD) < 2 Then StrD = "0" & StrD End If
If Len(StrM) < 2 Then StrM = "0" & StrM End If
WScript.Echo StrY & StrM & StrD

Link to comment
Share on other sites

I wasn't aware that the day was included with the date in that particular instance. At first I thought it a possible Vista quirk, however the cmd works correctly for me in Vista too.

Try it this way instead

@ECHO OFF&SETLOCAL
FOR /F "TOKENS=1-3 DELIMS=-/." %%a IN ('DATE/t') DO SET "D_=%%a%%b%%c"
ECHO/ Your date appendage is: %D_%

Also in order to achieve the result you tried above there is no need to extract the eight characters which occur after the first four. the problem with this method is that for those of us withour 'Wed ' we would only get back the last four characters, (2007 in my case). What you should have done was just add:

SET "D_=%D_:~-8%"

That will only use the last eight characters in all cases.

Link to comment
Share on other sites

You people are hard to please...

@ECHO OFF&SETLOCAL
FOR /F "DELIMS=0123456789 " %%? IN ("%DATE%") DO SET "D_=%%?"
FOR /F "TOKENS=1-3 DELIMS=%D_% " %%a IN ("%DATE%") DO SET "D_=%%a%%b%%c"
ECHO/ Your date appendage is: %D_%

How does that do?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...