How to add an expiry date and time on a windows bat code

P Joshi 21 Reputation points
2022-10-02T04:53:33.013+00:00

I need to add expiry date and time with this shutdown .bat code. Any help would be appreciated,

shutdown.exe /r /t 00

All i need is have a date & time with this code so that the code is valid only until its expiration date and time. For example the code becomes invalid after 5th October 2022 at 13:00 pm (1pm).

Thanks in advance

Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
375 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,666 Reputation points
    2022-10-02T06:24:27.237+00:00

    You can add conditions to your batch file. For example see the below sample.

    @echo off  
    echo "starting the program"  
    set expiry=20221002  
    set mydate=%date:~10,4%%date:~4,2%%date:~7,2%  
    if %mydate% LEQ %expiry% (echo "execute the script") Else (echo "Date Expired")  
    

    Basically it sets an expiry date to a number with yyyymmdd format. then from the current date, set another variable with yyyymmdd format. Then do a simble Less than or Equal comparison and if yes execute the script.

    You may need to see your date format by entering the dos command Date /T in the command prompt and adjust the statement to extract the date from the current date.
    246659-image.png

    If you have may statements to execute in if statements, try to use goto. Refer: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/goto

    Find the sample output for the script where I set the expriy as Second October

    246684-image.png

    see the execution of else part when I set date as 1st October

    246549-image.png

    If you wish to learn batch script in detail, refer: https://www.tutorialspoint.com/batch_script/index.htm

    Hope this helps

    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. P Joshi 21 Reputation points
    2022-10-02T07:16:11.897+00:00

    Thank you for your help - its close to what i am looking to achieve. I am not very knowledgable about the codes.

    If i want to insert this taskill code to the above code, how can i do it?

    TASKKILL /F /IM terminal64.exe

    The above code has the date and i ran in cmd prompt and worked as shown above. How do i include TIME as well to the above code.


  2. P Joshi 21 Reputation points
    2022-10-02T09:06:28.647+00:00

    I have put the date as 1st of October but it is still executing the code, it should be giving date expired. Am i doing something wrong?
    The date displayed in cmd is this: Sub 02/10/2022. If i put date format as 01102022 it gives date expired, and if i put 03102022, it still gives date expired
    Sorry for many questions

    246735-xcapture.jpg


  3. P Joshi 21 Reputation points
    2022-10-02T09:35:58.043+00:00

    Looks like my system date is shown as yyyyddmm, now it works correctly. Thank you. I also need to compare time value just like the date. If i have need an expiry date and time as 3 Oct 2022 at 14:00 pm then how can i achieve it? Thanks


  4. P Joshi 21 Reputation points
    2022-10-02T10:10:59.25+00:00

    thanks, below i added the hour and minute to the date but for some reason its reading it as true while it should be false
    expiry is 01 Oct 2022 2100 and system date time is 02 Oct 2022 2106 - it should be giving date expired but isnt

    246696-xcapture.jpg


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.