Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 527 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare 2 dates in a Windows batch file

#1
How is it possible to compare 2 dates in batch files?
This:

if %%newdate geq %olddate% _do smth_
does not work.

In this case I have that<br> `27.05.2013` is _greater than_ `15.07.2013` and<br> `14.07.2013` is _less than_ `15.07.2013`

Zhenya
Reply

#2
call :date_to_number %date1% date1
call :date_to_number %date2% date2

if %date1% GEQ %date2% echo date1 is bigger

goto :eof
:date_to_number
setlocal
if "%~1" EQU "" goto :eof
for /f "tokens=1,2,3 delims=." %%D in ("%~1") do (
set "the_date=%%F%%E%%D"
)
endlocal & if "%~2" neq "" (set %~2=%the_date%) else echo %the_date%
goto :eof

But this will work only with dates in format DD.MM.YYYY
Reply

#3
try this:

set "sdate1=%olddate:~-4%%olddate:~3,2%%olddate:~0,2%"
set "sdate2=%newdate:~-4%%newdate:~3,2%%newdate:~0,2%"
if %sdate1% GTR %sdate2% (goto there) else echo here
Reply

#4
I had an issue where I wanted to find users who's passwords had expired, or were about to expire.

So for starters, Windows 10 has changed the format of the %date% variable so it looks different than previous versions. therefore, the output of "echo %date%" looks like:
"Thu 05/09/19"
so I use this one liner to take today's date and convert it to yyyymmdd format.

set today=20%date:~-2%%date:~4,2%%date:~7,2%

(today = 20190509)

this is easy because we know that there is exactly 2 digits for each month and day.

Now, if the "date format" that you are working from does not pad the single digit months and days with a preceding zero, you will have to to pad them yourself.
For example, the output of the command: net user %username% /domain looks like "5/9/19", note the missing zeros...

So you can extract this a couple different ways. the simplest way is to grab each field seperately, drop a zero in front of the month and day variables, and then grab only the last two digits of them.
that way 5 becomes 05, and 12 becomes 012, and then goes back to 12.

for /f "tokens=3-5 delims=/ " %%a in ('net user %username% /domain ^|find /i "Password expires"') do (
set mm=0%%a& set dd=0%%b& set yyyy=20%%c
set expires=!yyyy!!mm:~-2!!dd:~-2!
if NOT !expires! GTR %today% echo Password has expired and needs to be changed.

Reply

#5
I create a simple c program i call newest.exe. Below is the source code.

#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

// syntax: newest file1 file2
//
// if age1 = age2, prints 0 age1 age2
// if age1 < age2, prints 1 age1 age2
// if age1 > age2, prints 2 age1 age2
// where
// age1 is age of file1 is seconds.
// age2 is age of file2 is seconds.
//
// To capture the output in a DOS bat file use:
// for /f "tokens=1-3" %%i in ('%srcPath%\newest.exe %src% %des%') do (
// set newest=%%i
// set age1=%%j
// set age2=%%k
// )


int main(int argc, char **argv)
{
struct stat fileStat;
time_t now;
long age1, age2;

if(argc != 3)
{
printf("syntax: %s File1 File2\n", argv[0]);
return 1;
}

now = time(NULL);

if(stat(argv[1],&fileStat) < 0) return 1;
age1 = now - fileStat.st_mtime;

if(stat(argv[2],&fileStat) < 0) return 1;
age2 = now - fileStat.st_mtime;

if( age1 == age2 )
printf("0 %d %ld\n", age1, age2);
else if( age1 < age2 )
printf("1 %d %ld\n", age1, age2);
else
printf("2 %d %ld\n", age1, age2);

return 0;
}
Reply

#6
You should always use the ISO format for dates (yyyy-MM-dd HH:mm:ss). This makes it unambiguous, locale agnostic, they sort chronologically and you can easily compare them as strings in CMD files.

For /F "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE') do (if .%%i EQU .LocalDateTime set ldt=%%j)
set CURRENT_DATE=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
set CURRENT_TIME=%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,2%

This will give you:

CURRENT_DATE in yyyy-MM-dd format
CURRENT_TIME in HH:mm:ss format


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through