Solved

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

Posted on 2014-03-05
6
6,993 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 53

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 53

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 53

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

772 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