Monday 28 May 2018

windows - How can I get the date in a locale independent format in a batch file?


I'm trying to read a file that contains a date in the filename.


The problem is when the date format is US in the system: mm/dd/yyyy it does not working .


For this format : dd/mm/yyyy it works as expected.


I need to get the date in this format dd/mm/yyyy even so the system is set with US format.


I'm new with batch and no idea how to get the date in the required format.


Here is my batch file:


for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
rem Lets name our new variable "rdate" for reverse date
set _date=%%a-%%b-%%c
)

set source=%path%.csv
echo %source%
set dest=%path%_%_date%.csv
echo %dest%

Answer



It sounds like a bad idea to be parsing a date that could be in various formats which format you don't know. Better to get the date in a specific format, even if that means not pure batch but invoking powershell.


C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd__HH_mm_ss"') do set datetime=%a

C:\Users\tod>set datetime=2018_01_22__09_15_33

or without time.


C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd"') do set datetime=%a

C:\Users\tod>set datetime=2018_01_22

C:\Users\tod>

Once you have that you can do


C:\Users\tod>echo %datetime:~0,4%
2018

C:\Users\tod>

And so on.. So you can set whatever variable to equal the year, month, date in month.


Added


If you wanted to not use powershell and you don't mind that this will only work for US formatted dates.


C:\Users\tod>echo %date%
01/22/2018

C:\Users\tod>echo %date:~0,2%
01

C:\Users\tod>echo %date:~3,2%
22

C:\Users\tod>echo %date:~6,4%
2018

C:\Users\tod>

So you really should be able to see how to do it


set first=%date:~0,2%

set second=%date:~3,2%

set third=%date:~6,4%


C:\Users\tod>set first=%date:~0,2%

C:\Users\tod>set second=%date:~3,2%

C:\Users\tod>set third=%date:~6,4%

C:\Users\tod>echo %first%/%second%/%third%
01/22/2018

C:\Users\tod>echo %second%/%first%/%third%
22/01/2018

C:\Users\tod>

So you can say set ukdate=%second%/%first%/%third% and you can say echo %ukdate%


And that non-powershell method only works if you know the format of the date on the system, and know that it's US. I would generally recommend against batch, because for example what if a new revision of the OS comes along and the output of a command is a bit different and your code presumes a particular format in its parsing. And while in this case batch is in one sense, neat, sometimes it can be like banging sticks together. It's a good idea to know batch and to use it a bit but to install some other scripting language like Ruby or Python or Golang or NodeJS. But if you want to use solely batch for this, then there it is.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...