How to store sysdate in variable with formate as 25Jan2014 - windows

How to store sysdate in formate as 25Jan2014, I am able to capture date in formate as
25012014 with below mention query
#echo off
SetLocal enabledelayedexpansion
for /f "tokens=2-4 delims=/ " %%a in ('') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
set TODAY=%day%%month%%year%
echo %TODAY%
How to capture date in 25Jan2014 formate?

This is a way to achieve what you want
#echo off
SetLocal enabledelayedexpansion
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set month=%%a
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set day=%%b
if %month%=01 set monthname=Jan
if %month%=02 set monthname=Feb
if %month%=03 set monthname=Mrt
if %month%=04 set monthname=Apr
if %month%=05 set monthname=May
if %month%=06 set monthname=Jun
if %month%=07 set monthname=Jul
if %month%=08 set monthname=Aug
if %month%=09 set monthname=Sep
if %month%=10 set monthname=Oct
if %month%=11 set monthname=Nov
if %month%=12 set monthname=Dec
set TODAY=%day%%monthname%%year%
echo %TODAY%
Beaware that this solution only works if the date is represented with / on my box the delimiter is -

The Batch file below solve your particular problem:
#echo off
setlocal EnableDelayedExpansion
set m=100
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
set /A m+=1
set month[!m:~-2!]=%%a
)
for /F "tokens=2-4 delims=/ " %%a in ('date /T') do set TODAY=%%b!month[%%a]!%%c
echo %TODAY%

Even if you modify your script to give the proper format, it is locale dependent, meaning it may not work on all machines, depending on how they are configured.
The solution is trivial if you use getTimeStamp.bat - a hybrid JScript/batch utility that performs date and time computations and formatting. The utility is pure script that runs on any modern Windows machine from XP onward. Full documentation is embedded within the script.
Assuming you have getTimeStamp.bat in your current directory, or better yet, somewhere in your path:
call getTimeStamp -f {dd}{mth}{yyyy} -r TODAY
The utility defaults to English month abbreviations, but there is an option available to specify the abbreviations for any desired language. There are also options for full month name, day of week name, and day of week abbreviation.

Potential issues
Parsing either the %date% variable or the date /t output is not reliable because:
The date/time format is not the same for all locales.
The format can be further customized.
Alternate solution
Use wmic instead. Type this command to see all available date/time values:
wmic path Win32_LocalTime get /all /value /format:list
Example script
#echo off
setlocal enabledelayedexpansion
set months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
call :getDate
call :getMonthName %month%
set TODAY=%day%%monthName%%year%
echo %TODAY%
exit /b
:getDate
for /f "skip=1 tokens=1-3" %%A in ('wmic path Win32_LocalTime get day^,month^,year /value /format:table') do (
set day=00%%A
set day=!day:~-2!
set month=00%%B
set month=!month:~-2!
set year=%%C
exit /b
)
:getMonthName
for /f "tokens=%1" %%A in (%months%) do (set "monthName=%%A")
exit /b
References
GetDate | SS64.com

Related

Having trouble with a script to find the date and time in a batch file

So, I have a script to log the date and time in a text file:
for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G
set year=%datetime:~0,4%
rem pause
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set /a day2=day+5
echo %year%/%month%/%day2%> C:\WLT\Init\lastoptim.txt
And a similar one in a script that runs on startup:
for /f "tokens=2 delims==" %%G in ('type C:\WLT\Init\lastoptim.txt') do set datetime2=%%G
set year=2%datetime2:~0,4%
rem pause
set month2=%datetime2:~4,2%
set day2=%datetime2:~6,2%
set /a day3=day2+1
echo %year2%/%month2%/%day3%> lastoptim2.txt
echo %year2%/%month2%/%day3%
But it doesn’t work. What I need is a script to extract a date from %date% into a text file, and then another one to take that date, see if the current date is X number of days later, and if it is, then run a command.
Would like this script to be entirely dependent on cmd, because this is going to be used on systems as far back as XP. Using an external app (e.g. an exe that prints out the date in a specific format) would be fine.
Your question still not clear for me, but if you want to add one day or one month for your dates, here is an example :
#echo off
Title Manipulate DATES WITH BATCH
#for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
:: Store the local date inside a variable
set "CurrentDateTime=%LocalDateTime%"
set "CurrentDate=%CurrentDateTime:~0,4%/%CurrentDateTime:~4,2%/%CurrentDateTime:~6,2%"
:: Here Adding One day
#for /f %%a in ('Powershell -C "(Get-Date).AddDays(1).ToString('yyyy/MM/dd')"') do set "NextDay=%%a"
:: Here adding 1 month
#for /f %%a in ('Powershell -C "(Get-Date).AddMonths(1).ToString('yyyy/MM/dd')"') do set "NextMonth=%%a"
echo( CurrentDate : %CurrentDate%
echo( NextDay : %NextDay%
echo( NextMonth : %NextMonth%
pause

How get only modification year using batch script?

I have a problem
i need modification date of file. And i use windows batch script. But i can't get modification date and split them.
i currently using this command
for /f %a in ('dir /b') do #echo %a %~ta
result
file89401.txt 06/18/2005 05:37 AM
file8941.txt 11/21/2000 03:48 PM
file89411.txt 09/02/2008 02:14 PM
file89421.txt 03/01/2012 02:06 PM
file89431.txt 10/23/2001 06:48 AM
file89441.txt 10/27/2010 10:21 AM
file89451.txt 11/11/2011 05:40 AM
file89461.txt 11/23/2000 02:48 AM
file89471.txt 10/10/2001 01:28 AM
file89481.txt 07/14/2000 10:17 AM
file89491.txt 02/21/2004 10:24 PM
file89501.txt 03/12/2011 09:42 AM
file8951.txt 09/26/2003 11:31 PM
file89511.txt 08/17/2001 02:31 AM
file89521.txt 01/01/2004 01:11 PM
and i need only modification date and following format
2005/06/18
2000/1/21
2008/09/02
2012/03/01
2001/10/23
2010/10/27
2011/11/11
2000/11/23
2001/10/10
2000/07/14
2004/02/21
2011/03/12
2003/9/26
2001/08/17
2004/01/01
please help me,, sorry my bad English :(
Let's say you want to see modified date of files under directory C:\Users\test in format yyyy/mm/dd, below works perfectly for you:
#echo off
setlocal
for %%F in ("C:\Users\test\*") do (
for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
echo %%C/%%A/%%B
)
)
Result:
2013/12/21
2013/11/19
2012/12/28
2012/10/25
2011/10/25
2011/11/09
2010/12/21
findstr can only output Date/Time, not Date only.
You can fix it with some code or use a command that does what you want:
forfiles /c "cmd /c echo #fdate"
this is not a well knwon command, but it does give you the dates.
For more info see forfiles /?
to format the date to the desired format use:
for /f "tokens=1,2,3 delims=." %%a in (' forfiles /m *.* /c "cmd /c echo #fdate" ') do echo %%c/%%b/%%a
(Attention, this is language-dependent This line is for german format ("25.12.2013")
For your format echo %%c/%%a/%%b should do the trick)
EDIT
just noticed, you want no leading zero on day or month.
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=." %%a in (' forfiles /m *.* /c "cmd /c echo #fdate" ') do (
set dat=%%c/%%a/%%b
set dat=!dat:/0=/!
REM replaced "/0" with "/"
echo %!dat!
)
From the prompt,
for %a in (*.*) do #for /f "delims=/ " %i in ("%~ta") do #echo %k/%i/%j
should deliver the goods.
As a batch file,
#echo off
for %%a in (*.*) do for /f "delims=/ " %%i in ("%%~ta") do echo %%k/%%i/%%j

How to get date in BAT file

I need to get today date in Window *.bat file. After it I would like to get day, month and year. How can I do this?
I can't use PowerShell
This will give you DD MM YYYY YY HH Min Sec variables and works on any Windows machine from XP Pro and later.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
You get and format like this
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
Note: Above only works on US locale. It assumes the output of echo %date% looks like this: Thu 02/13/21. If you have different Windows locale settings, you will need to modify the script based on your configuration.
%date% will give you the date.
%time% will give you the time.
The date and time /t commands may give you more detail.
Locale-independent one liner to get any date format you like. I use it to generate archive names. Back quote (`) option is needed because PowerShell command line is using single quotes (').
:: Get date in 'yyyyMMdd_HHmm' format to use with file name.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).ToString^('yyyyMMdd_HHmm'^)`) DO SET DTime=%%i
:: Get yesterday date in 'yyyy-MM-dd' format.
FOR /f "usebackq" %%i IN (`PowerShell ^(Get-Date^).AddDays^(-1^).ToString^('yyyy-MM-dd'^)`) DO SET DTime=%%i
:: Show file name with the date.
echo Archive.%DTime%.zip
set datestr=%date%
set result=%datestr:/=-%
#echo %result%
pause
This will give you the date in this format mmddyyyy saved into %today%, assuming your regional settings have you in the US locale.
FOR /f "tokens=2-4 delims=/ " %%a IN ("%date%") DO SET today=%%a%%b%%c
In my case I was looking to create a folder with todays date, like this...
mkdir C:\Users\LKrell\Documents\%today%

Windows Batch script save file as date/time

Im using this code on a windows batch script:
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%dd%%mm%%yyyy%
echo New folder name %date%
but the folder name is just 07on
i need to be able to save the file as dd-mm-yy h:m:s
I'm really not sure what you're trying to accomplish, but you might want to look at something like this:
Running this today (from a command prompt)
C:\>date
The current date is: Thu 07/04/2013
Enter the new date: (mm-dd-yy)
C:\>SET Today=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
C:\>ECHO %Today%
Produces
2013-07-04
It works like this:
SET Today=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
^ ^ ^ ^ ^
| | | | |
Year | Dash Month Day
|
Number of chars
You can get the time values the same way, replacing the %Date% with %Time% and adjusting the offsets the way you need to get the format you want to get.
You might also find this similar answer I posted helpful if you want to separate the year, month, and day into separate values.
This is a robust method to get date and time rather than rely on the PC regional settings. Requires XP Pro and higher.
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
Its easy. Just enter echo %time% or echo %date% and it will tell you. Hope it helps!

Generate unique file name with timestamp in batch script

In my .bat file I want to generate a unique name for files/directories based on date-time.
e.g.
Build-2009-10-29-10-59-00
The problem is that %TIME% won't do because it contains characters that are illegal in filename (e.g. :).
Is there something like tr in batch files?
Any other ideas how to solve this (that don't require extra command line utilities aside from the batch interpreter)?
EDIT: A better way of doing this is to take a date/time string that has a defined and unchanging format instead of using the locale-defined ones from %date% and %time%. You can use the following to get it:
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined mydate set mydate=%%x
It yields something like 20120730203126.530000+120 and you can use that to construct your file names.
(Old answer below)
You can simply replace the offending character with an empty string:
echo %time::=%
The syntax %var:str1=str2% takes the environment variable (or pseudo-variable in case of %TIME% and replaces str1 by str2. If nothing follows after the equals sign then str1 is simply deleted.
In your specific case I think you'd want the following:
rem cut off fractional seconds
set t=%time:~0,8%
rem replace colons with dashes
set t=%t::=-%
set FileName=Build-%date%-%t%
A more brute-force way in case you don't know whether colons are used (but the order of the time would be the same):
set FileName=Build-%date%-%time:~0,2%-%time:~3,2%-%time:~6,2%
All preceding things, however, assume that you use standard ISO 8601 date format, i. e. 2009-10-29. I'd assume this as simply normal, but some people use other formats so be careful. But since you didn't ask about the date I was assuming you didn't have a problem there.
Following up on #Joey's and #Kees' answers to make them instantly usable.
On the command line:
FOR /f %a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%a
SET DateTime=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%_%DTS:~8,2%-%DTS:~10,2%-%DTS:~12,2%
echo %DateTime%
In a BAT file:
#echo off
REM See http://stackoverflow.com/q/1642677/1143274
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DateTime=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%_%DTS:~8,2%-%DTS:~10,2%-%DTS:~12,2%
echo %DateTime%
Example output:
2014-10-21_16-28-52
I use this to create a unique file name for the execution of the batch file.
REM ****Set up Logging ****
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
set mytime=%mytime: =0%
set Logname="PCCU_%mydate%_%mytime%.log"
Echo. >>%Logname% 2>>&1
Echo.=================== >>%Logname% 2>>&1
I had to add the line
set mytime=%mytime: =0%
because I had the same problem where a blank was being entered before 10 AM, now I get 09 instead of 9. I also reuse the %mydate% and %mytime% variable for other files that I create with this script so that they all have the same date time stamp.
This routine, actually only one line, works on every system set to any date or time format.
Output of line 1 is in format 20140725095527.710000+120
The actual date/time format you need is determined in line 2. You can format it however you want.
Just add the resulting DateTime variable to your filename ie. Filename_%DateTime%.log
::=======================================================================
::== CREATE UNIQUE DateTime STRING IN FORMAT YYYYMMDD-HHMMSS
::=======================================================================
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DateTime=%DTS:~0,8%-%DTS:~8,6% | REM OUTPUT = 20140725-095527
I made this universal, Will work on any environment where date format may be different.
echo off
if not exist "C:\SWLOG\" mkdir C:\SWLOG
cd C:\SWLOG\
cmd /c "powershell get-date -format ^"{yyyyMMdd-HHmmss}^""> result.txt
REM echo %time% > result.txt
type result.txt > result1.txt
set /p filename=<result1.txt
echo %filename%
del C:\SWLOG\result.txt
del C:\SWLOG\result1.txt
To make the code in the previous example work, I needed to adjust slightly. I presume this is because my PC is using a UK date format. To get back "2014-04-19" in mydate I needed:
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
Below I've pasted my total script, and included an example of how to use the filename
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
set mytime=%mytime: =0%
set Logname="c:\temp\LogFiles\MyLogFile_%mydate%_%mytime%.log"
Robocopy \\sourceserver\Music H:\MyBackups\Music /MIR /FFT /Z /XA:H /W:5 /np /fp /dcopy:T /unilog:%Logname% /tee
Hope this helps!
Datetime stamp:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
rem set "datestamp=%YY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "datestamp=%YYYY%%MM%%DD%"
set "timestamp=%HH%%Min%%Sec%"
set unique_number=%datestamp%%timestamp%
echo %unique_number%
If you can guarantee that the machine has a specific version of Python installed on it and accessible in the system's PATH, you can use this solution:
FOR /F "tokens=* USEBACKQ" %%F IN (`python -c "import datetime; print
datetime.datetime.now().replace(microsecond=0).isoformat().replace(':', '-')"`) DO (
SET TIMESTAMP=%%F
)
ECHO %TIMESTAMP%
This will put the current local ISO-8601(ish) date representation into a %TIMESTAMP% variable and echo it out, like so:
2017-05-25T14:54:37
I've put replace(':', '-') after the isoformat() so that the resultant string can be used in a filename or directory, since : is a forbidden character in the Windows filesystem.

Resources