|
Date-Time [set up time formats YYYYMMDD, HHMMSSSS etc].cmd |
The code below shows how you can get date/time information.
Quite often you will want to use these in a directory or filename, this means you will need to avoid characters such as a colon (":") which is invalid in Windows filenames.
As shown below, you can use string replacement to change the ":" into another character (underscore).
The following example uses the "DATE" & "TIME" environment variables directly:
echo DATE-SAFE "%date:/=-%" (date env var with "/" replaced by "-") echo TIME-SAFE "%time::=_%" (time env var with ":" replaced by "_")
The Code for: "Date-Time [set up time formats YYYYMMDD, HHMMSSSS etc].cmd" |
This is the example, a shortcut was installed to this code :
@echo off ::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ :: $Header: C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].Date-Time [set up time formats YYYYMMDD, HHMMSSSS etc].cmd.pvcs 1.1 11 Jul 2014 19:31:00 USER "Dennis" $ ::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ setlocal ::--- get date and time (and remove ":" from time) -------------------------- call :GetDateTime set FILENAME_SAFE_HHMMSSSS=%HHMMSS::=_% ::--- Use the date/time ----------------------------------------------------- set ResultDir=out\%YYYYMMDD% (%DAY3%) @ %FILENAME_SAFE_HHMMSSSS% echo Will generate output to timestamped directory: echo * "%ResultDir%" goto :EOF ::=========================================================================== :GetDateTime :: Tokenise date into DD MM and YY independent of locale :: NEWSGROUP: microsoft.public.win2000.cmdprompt.admin :: SUBJECT : How can I to get the current month in commandline? :: WHEN/WHO : Mar 14 2001, 9:05 pm post by Michael (maj0) :: :: DATE output (when prompting for new date) looks like: :: The current date is: Sat 05/07/2014 :: Enter the new date: (dd-mm-yy) :: :: The first loop reads the DATE from the environment variable and splits it up :: The second loop reads 2nd line of the date output and splits up "(dd-mm-yy)" :: It then sets up the "DD", "MM" and "YY" environment variables. ::=========================================================================== ::--- Get the date ------------------------------------------------------ for /f "tokens=2-4 delims=.:/-, " %%i in ("%date%") do ( for /f "tokens=2-4 delims=/-,() skip=1" %%l in ('echo.^|date') do ( set %%l=%%i set %%m=%%j set %%n=%%k ) ) set YYYYMMDD=%YY%-%MM%-%DD% ::--- Get the day of the week ------------------------------------------- for /f "tokens=1 delims= " %%W in ("%date%") do set Day3=%%W ::--- Get some time formats --------------------------------------------- set HHMMSSSS=%TIME: =0% set HHMMSS=%HHMMSSSS:~0,8% set HHMM=%HHMMSSSS:~0,5% goto :EOF ::** [CommentBlockStart (5 July 2014 11:27:45 AM, Dennis) ::**+---------------------------------------------------------------------- ::**|::=========================================================================== ::**|:GetDateTime ::**|::=========================================================================== ::**| for /F "tokens=1,2,3,4 delims=/ " %%i in ('date /T') do set YYYYMMDD=%%l-%%k-%%j&set Day3=%%i ::**| set HHMMSSSS=%TIME: =0% ::**| set HHMMSS=%HHMMSSSS:~0,8% ::**| set HHMM=%HHMMSSSS:~0,5% ::**| goto :EOF ::**| ::**+---------------------------------------------------------------------- ::** CommentBlockEnd] (5 July 2014 11:27:45 AM, Dennis)
Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)
![]() ![]() |
| ![]() ![]() |