Solved

DOS batch script to append a file to include date and time stamp of file creation

Posted on 2014-03-05
6
6,881 Views
Last Modified: 2014-03-25
I would like a DOS batch script file that once the file is ran on a weekly basis via a schedule task, it will append the file with the current date and time stamp

The file should be appended like so:-

currentdatetime_mmrdetails.txt

Thanks
0
Comment
Question by:rakkad
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 39907642
This looks a bit complicated because it gets the time from WMI, but it prevents errors that can happen due to different date/time format settings when retrieving substrings from %date% and %time%:
@echo off
setlocal enabledelayedexpansion
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeroes if less than 10.
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%
set FileName=%TimeStamp%_mmrdetails.txt
echo File name: %FileName%

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39907696
Of course, if the script will always be running on the same system and you know the format of the date time won't be changing, you can go a little simplere with something like:

REM Build YYYYMMDD_hhmmss format date/time stamp for new file name
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,8%
set Stamp=%Stamp::=%
set Stamp=%Stamp: =0%
set Filename=%Stamp%_mmrdetails.txt

Open in new window

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39907713
Or, one of my favorites, if you can place a small EXE with the BAT file where it runs, you can get this utility:

http://www.jfitz.com/dos/#DOFF

And do it as simple as this:

for /f %%A in ('doff') do set Filename=%%A_mmrdetails.txt

Open in new window

This util can be very helpful because it allows you to specify the format of the returned date and time, and also allows math on the date, so you can find the date +/- any number of days from today.

~bp
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:rakkad
ID: 39911764
I would like another entry to the file:-

- checking to ensure that a file is created, has the currentdatetime  and if not, output an error that the file has not been produced

Thanks
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39912431
Once you have the %Filename% variable with the name o the desired file from any of the techniques above, just add the following to check that the file exists (which already has the date/time in the name you will be checking) and report if not found.

if not exist "%Filename%" echo *ERROR* File not created: "%Filename%"

Open in new window


if you want the output in a log file you can send it there, like this:

if not exist "%Filename%" echo *ERROR* File not created: "%Filename%" >> "c:\folder\log.txt"

Open in new window

~bp
0
 

Author Closing Comment

by:rakkad
ID: 39954547
Helped thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now