Solved

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

Posted on 2014-03-05
6
7,111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 84

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 54

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 54

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 54

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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