2

My goal is to create and name a file with the current time in seconds as the name. In IOS there is touch "$(date +%s)_my_migration_name.up.sql" which I used, but I'm looking for a similar solution for Windows.

Mofi
  • 38,783
  • 14
  • 62
  • 115
Vinh Vo
  • 21
  • 1
  • 2
    I think this [question](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) is relevant to your problem. – nadhif Feb 01 '21 at 06:35

1 Answers1

0

It is possible to get the number of seconds since 1970-01-01 00:00:00 assigned to an environment variable like Seconds using the subroutine GetSeconds as demonstrated by the following batch file code:

@echo off
call :GetSeconds
set "FileName=%Seconds%_my_migration_name.up.sql"
set FileName
pause
goto :EOF

:GetSeconds
setlocal EnableExtensions DisableDelayedExpansion
rem Get current date/time region (country) independent.
if exist %SystemRoot%\System32\robocopy.exe for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "Year=%%I" & set "Month=%%J" & set "Day=%%K" & set "Hour=%%L" & set "Minute=%%M" & set "Second=%%N" & goto ProcessDateTime
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "DateTime=%%I"
set "Year=%DateTime:~0,4%" & set "Month=%DateTime:~4,2%" & set "Day=%DateTime:~6,2%" & set "Hour=%DateTime:~8,2%" & set "Minute=%DateTime:~10,2%" & set "Second=%DateTime:~12,2%"

:ProcessDateTime
rem echo Current date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%
rem Remove leading zeros from the date/time values or calculation could be wrong.
if %Month:~0,1%  == 0 set "Month=%Month:~1%"
if %Day:~0,1%    == 0 set "Day=%Day:~1%"
if %Hour:~0,1%   == 0 set "Hour=%Hour:~1%"
if %Minute:~0,1% == 0 set "Minute=%Minute:~1%"
if %Second:~0,1% == 0 set "Second=%Second:~1%"

set /A "Index1=Year-1979"
set /A "Index2=Index1-30"
if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
endlocal & set "Seconds=%Seconds%"
rem echo Seconds since 1970:   %Seconds%

rem Exit this subroutine.
goto :EOF

For an explanation see following answers:

The subroutine GetSeconds returns in environment variable Seconds the number of seconds since 1970-01-01 00:00:00 for the date/time range 1980-01-01 00:00:00 to 2038-01-19 03:14:07. So the code must be changed in year 2038.

The comments (remarks) can be all removed from code for a bit faster processing of the batch file code.

Mofi
  • 38,783
  • 14
  • 62
  • 115