Solved

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

Posted on 2014-03-05
6
6,943 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 52

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

18 Experts available now in Live!

Get 1:1 Help Now