Page 1 of 1
convert date in yyyymmdd
Posted: 13 Jul 2016 01:00
by sal21
Code: Select all
:: get the file's date as dd/mm/yyyy
for /f "tokens=1" %%a in ('dir "%folder%\%file%" ^| find /i "%file%"') do set filedate=%%a
:: change the date to yyyymmdd
set date_created=%filedate:~10,4!!filedate:~7,2!!filedate:~4,2!
the set date_created not return date in yyyymmdd format!
Why?
Re: convert date in yyyymmdd
Posted: 13 Jul 2016 05:47
by Squashman
Date settings are region dependent. We need to see the output of your DIR command to see what format it is in.
Regardless of that, you should really try using the variable modifiers to just get the date.
Code: Select all
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
Re: convert date in yyyymmdd
Posted: 14 Jul 2016 08:37
by foxidrive
Here's some code to get a specific date format in any region.
Code: Select all
rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
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 "MS=%dt:~15,3%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
Code: Select all
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "date-time=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%"
echo date-time: "%date-time%"
pause
Re: convert date in yyyymmdd
Posted: 16 Jul 2016 19:37
by SIMMS7400
I use this method in all my scripts:
Code: Select all
@ECHO OFF
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)
ECHO %date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0 >>C:\DOS_TIPS_FORUMS.txt
It outputs this:
20160716_2136_DOS_TIPS_FORUMS
Hope this helps!
Re: convert date in yyyymmdd
Posted: 17 Jul 2016 11:26
by foxidrive
SIMMS7400 wrote:I use this method in all my scripts:
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
ECHO %date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0
That works fine, but in other parts of the world it will fail. That's the problem that MS never fixed. Even on a computer in the house next door to you a person can alter the regional settings and your script will fail for them.
I think the programmer at Microsoft was high on magic mushrooms when he wrote the code for date and time.
Re: convert date in yyyymmdd
Posted: 17 Jul 2016 14:16
by Squashman
The user is asking for the date of a file in a specific format. Not the current date in a specific format.
Re: convert date in yyyymmdd
Posted: 18 Jul 2016 03:11
by Compo
Once again I'm drawn to powershell…
Code: Select all
:: get the file's date as yyyyMMdd
For /F UseBackQ %%a In (
`Powershell "(dir '%folder%\%file%').LastWriteTime.ToString('yyyyMMdd')"`
) Do Set MyVar=%%a
You can change LastWrite to Creation if it suits your needs better!
Re: convert date in yyyymmdd
Posted: 18 Jul 2016 04:37
by foxidrive
Squashman wrote:The user is asking for the date of a file in a specific format. Not the current date in a specific format.
I am president of the local pedantic club. LOL
He's asking why the date format was wrong and didn't mention a file at all.
But yes, he didn't mention the current date.
I'll have to resign my position at the club..
I have to admit that I don't pay attention to code that has no explanation. These days there is no guarantee that the code even represents what the user has on their computer.
Re: convert date in yyyymmdd
Posted: 19 Jul 2016 15:56
by thefeduke
The original post had two lines of batch code, so here my two lines:
Code: Select all
:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~3,2%%filedate:~,2%
Please note that the date is that of last update and not the creation date and the regional date format is a default that seems to work for you. I have added some code to demonstrate the difference and how to extract the creation date if that is what you really wanted:
Code: Select all
@echo off &setlocal
::Posted: Wed Jul 13, 2016 3:00 am by sal21
::http://www.dostips.com/forum/viewtopic.php?p=47743#p47743
:: Post subject: convert date in yyyymmdd
::modified: Tue Jul 19, 2016 by thefeduke
Set "folder=%Userprofile%\scripts\testdata"
Set "file=FileDate.txt"
:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Default date is : %date_created%
:: get the file's date as dd/mm/yyyy using default write date in dir
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d') do (
if /I "%%E" EQU "%file%" Set "filedate=%%~A"
)
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Default date is : %date_created%
:: get the file's date as dd/mm/yyyy using create date in dir
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d /Tc') do (
if /I "%%E" EQU "%file%" Set "filedate=%%~A"
)
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Creation date is: %date_created%
Exit /b
John A.
Re: convert date in yyyymmdd
Posted: 20 Jul 2016 12:47
by foxidrive
thefeduke wrote:Code: Select all
:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"
John A.
The %%~tD format depends on the region it is being used in John so it's unreliable for setting a specific date format. Even user changes in the control panel will alter this.
Re: convert date in yyyymmdd
Posted: 22 Jul 2016 16:51
by thefeduke
foxidrive wrote:The %%~tD format depends on the region it is being used in John so it's unreliable for setting a specific date format. Even user changes in the control panel will alter this.
So I followed foxi's lead and went to the control panel to see what I could do:
Code: Select all
@Echo off&SetLocal
::Posted: Wed Jul 13, 2016 3:00 am by sal21
::http://www.dostips.com/forum/viewtopic.php?p=47743#p47743
:: Post subject: convert date in yyyymmdd
::modified: Tue Jul 22, 2016 by thefeduke
Set "folder=%Userprofile%\scripts\testdata"
Set "file=FileDate.txt"
:: Get the date format from registry settings
For /F "usebackq tokens=3 delims= " %%F in (`reg query "HKCU\Control Panel\International" /V sShortDate`) do Set "sDateFormat=%%F"
Rem.Echo.sDate format: %sDateFormat%
:: Create date format table for US English
>"%temp%\%~n0_sDate.txt" (
Echo.dd-MMM-yy Echo 2-digit Year tested
Echo.yy/MM/dd Echo 2-digit Year tested
Echo.yyyy-MM-dd 0 5 8 tested
Echo.MM/dd/yy Echo 2-digit Year tested
Echo.MM/dd/yyyy 6 0 3 tested
Echo.M/d/yyyy 6 0 3 tested
Echo.M/d/yy Echo 2-digit Year tested
)
:: Set offsets for date format
Set "OffY="&Set "OffM="&Set "OffD="
For /F "usebackq tokens=1-4* delims= " %%D in (`type "%temp%\%~n0_sDate.txt"`) do (
IF "%%~D" EQU "%sDateFormat%" (
If /I "%%E" EQU "echo" (
%%E %%F %%G %%H for setting: %%D
) Else Set "OffY=%%E"&Set "OffM=%%F"&Set "OffD=%%G"
)
)
If NOT Defined Offy (Echo.Unexpected date format.&Exit /b 1 )
:: get file's date in regional format using create date in DIR command
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d /Tc') do (
if /I "%%E" EQU "%file%" Set "filedate=%%~A"
)
:: change the date to yyyymmdd using custom offsets for date format
setlocal enableDelayedExpansion
set date_created=!filedate:~%OffY%,4!!filedate:~%OffM%,2!!filedate:~%OffD%,2!
Echo.File Creation date is: %date_created%
Exit /B
I did not attempt to work with the two digit year formats, but it is much better than before. I only tested on Windows 7.
Compo wrote:Once again I'm drawn to powershell…
Abandon ship! I found Compo's solution vastly superior in every way that I could see.
At least this pure batch improvement works in some cases and indicates that it cannot when appropriate.
John A.