Show how many days to go between today and future date.

October 8, 2020 at 13:44:20
Specs: Windows 10 Pro
Hi, I wonder is someone can help. I need to the output Date from the batch script below and then work out how many days it is between ‘today’ and the output date and then pass it to a var?

Is this possible using only batch script?

 for /f "tokens=3,4 delims= " %%f in ('net user %targetuser% /domain ^| find /I "Password expires"') do set expire=%%f %%g
set "expireDate=%expire: =" & set "dummy=%"
echo. Password will expire on: %expireDate:?=% 

Example output:

Password will expire on: 10/10/2020 (2 days from now)

echo. Password will expire on: %expireDate:?=% (%expireDays% from now)

Ideally it will show ‘1 day from now / 2 days from now’ depending on the amount of days left.

Many thanks,

Bri


See More: Show how many days to go between today and future date.


#1
October 9, 2020 at 21:26:00
'--------- begin vbscript dd.vbs, one line. errorlevel will be set to the difference, in days:
wscript.quit datediff("d",wscript.arguments(0),date)
'------ end vbscript

To use this in batch:

dd.vbs %date_expire%
set /a expdays=-%errorlevel%
echo %expdays% left
::----------- end snippet
If you want, you can make the vbs from the batch before executing:
>dd.vbs echo wscript.quit datediff^("d",wscript.arguments^(0^),date^)

message edited by nbrane


Reply ↓  Report •

#2
October 10, 2020 at 01:27:14
Hi nbrane,

Thank you for your reply. I would prefer to keep everything together in one file so embedding vbscript in the main batch would be better than calling it externally. However, being a total noob with vbscript I have no idea how to do this.

Could you offer some suggestions?

Thanks,

Bri


Reply ↓  Report •

#3
October 10, 2020 at 03:29:58
The output shows 0 days left but todays date is 10/10/2020 and password expiry date is 10/11/2020.

::Show formatted todays date
for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G
set day=%datetime:~6,2%
set month=%datetime:~4,2%
set year=%datetime:~0,4%
echo. Todays date: %day%/%month%/%year%

:: Show when AD users password expires
>date.vbs echo wscript.quit datediff^("d",wscript.arguments^(0^),date^) %date_expire%
set /a expdays=-%errorlevel%

for /f "tokens=3,4 delims= " %%f in ('net user %targetuser% /domain ^| find /I "Password expires"') do set expire=%%f %%g
set "expireDate=%expire: =" & set "dummy=%"
echo. Password expiry date: %expireDate:?=% &echo. (%expdays% days left)


Reply ↓  Report •

Related Solutions

#4
October 10, 2020 at 09:43:45
In an effort to clean up the output from %expireDate:?=% that is taken from the following:

for /f "tokens=3,4 delims= " %%f in ('net user %targetuser% /domain ^| find /I "Password expires"') do set expire=%%f %%g
set "expireDate=%expire: =" & set "dummy=%"
echo. Password will expire on: %expireDate:?=%

I tried to break it down by doing the following:

set expday=%expireDate:~1,2%
set expmonth=%expireDate:~5,6%
set expyear=%expireDate:~9,12%
echo. Formatted expiry date: %expday%/%expmonth%/%expyear%

With this the Day and Month shows correctly but the Year does not. Instead I get ?20/2020 as opposed to just 2020.

As a side note, I had to change %expireDate:?=% (which shows the correct date. EG: 10/11/2020) to %expireDate:% (which shows: ?10/?11/?2020) as the following would not work:

set expday=%expireDate:?=~1,2%
set expmonth=%expireDate:?=~5,6%
set expyear=%expireDate:?=~9,12%
echo. Formatted expiry date: %expday%/%expmonth%/%expyear%

So, my question is, what do I need to change this line to: set expyear=%expireDate:~9,12% to show only the year (2020)?

Thanks in advance,

Bri


Reply ↓  Report •

#5
October 10, 2020 at 16:06:11
Fixed it with:

set expyear=%expireDate:~-4%

It’s been a loooong time since using batch 😄

Still need a way to show how many days it is until the password expires though.. Anyone?

message edited by BriG001


Reply ↓  Report •

#6
October 10, 2020 at 22:48:49
I just would like you to post output from this:
for /f "tokens=*" %%f in ('net user %targetuser% /domain ') do echo %%f

I can't get that from my system as domains/users are not established.
Basically, you DON'T send the current date to the vbs, but the parsed output from the above 'net' operation. This script requires no wmic to get current date. vbs provides that. The vbs input is the expiration date, sent from the batch, and can handle
a very robust range of formats. In your failed script above, you passed the current date to the vbs, before you even established the 'net user' expiry date.
Here's the "self-contained" script:

@echo off & setlocal
>dd.vbs echo wscript.quit datediff^("d",date,wscript.arguments^(0^)^)
:: verbatim from your work...
for /f "tokens=3,4 delims= " %%f in ('net user %targetuser% /domain ^| find /I "Password expires"') do set expire=%%f %%g
set "expireDate=%expire: =" & set "dummy=%"
rem echo. Password will expire on: %expireDate:?=% 
:: I need %expireDate%. or as requested above - preferably raw output from 'net user'
for /f "tokens=1-3 delims=? " %%a in ("%expireDate%") do set ed=%%a%%b%%c
echo does this look right? submitting %ed% to dd.vbs: 
dd.vbs %ed%
echo %ed%, %errorlevel% days left
::----------- end snip

You can test the dd.vbs script:
dd.vbs 2021/1/12
echo %errorlevel%
dd.vbs 12-20
echo %errorlevel%
dd.vbs 4/3/2021
echo %errorlevel%
dd.vbs 3-3-20
echo %errorlevel%
dd.vbs 3-3/21
echo %errorlevel%

message edited by nbrane


Reply ↓  Report •

#7
October 11, 2020 at 02:28:00
Hi, thanks for getting back to me - I can't really post all of the info from for /f "tokens=*" %%f in ('net user %targetuser% /domain ') do echo %%f for obvious reasons. However, the Password expires section displays the following:

?10/?11/?2020 21:42:39

Your code gives the following Windows Error:

Line 1
Char 1
Error: Type mismatch: '[string: "?10/?11/?2020"]'
Code: 800A000D
Source: Microsoft VBScript runtime error

I tried to select the last 4 chars (IE: The year 2020) using:

set expday=%expireDate:~1,2%
set expmonth=%expireDate:~5,6%
set expyear=%expireDate:~-4%

When I echo everything together like so: echo %expday%^/%expmonth%^/%expyear% it then shows: 10/11/?20/2020. However if I echo only %expyear% by itself it shows 2020 as it should!?

Needless to say I am confused...


Reply ↓  Report •

#8
October 11, 2020 at 05:17:02
Also had to change set expmonth=%expireDate~5,2% as I thought the fist number was the one to start on and the second was the one to end on as opposed to the amount after the first.

Reply ↓  Report •

#9
October 11, 2020 at 19:02:28
My bad, apologies. I should have requested only this:
for /f "tokens=*" %%f in ('net user %targetuser% /domain ^| find /I "Password expires"') do echo %%f

This should deliver the raw output from 'net user' without any sensitive info. Note I've not added any filtration cause I just want the raw output.
(If anything sensitive, mask it off!). Post #6 approximates, but I'd like to see the raw material from the above.

message edited by nbrane


Reply ↓  Report •

Ask Question