?
Solved

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

Posted on 2014-03-05
6
Medium Priority
?
7,493 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 85

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 56

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 56

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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 56

Accepted Solution

by:
Bill Prew earned 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 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