Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Variable expiration date  (Read 44261 times)

0 Members and 1 Guest are viewing this topic.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Variable expiration date
« on: October 21, 2022, 02:20:23 PM »
Can I make a file that has a variable expiration date depending on the date it was run, for example,
for I want to make my batch expire after one month from starting date Or I must use a fixed expiration date stored inside the variable in the batch file as below.
Code: [Select]
@echo off
Title EXPIRATION DATE WITH BATCH
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
REM echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20201231"
:::::::::::::::::::::

if %LocalDateTime% GTR %EXP_DATE% (
 color 0C
 echo this wont work anymore
 Timeout /t 5 /NoBreak >NUL
 exit /b
)

START "" "Chrome"
Exit

This is the Expiration date coded by Hackoo and as I know he's from the best people here for coding but this date is a Fixed date, not a variable date for example IF I sent my batch to anyone and for some reason, he didn't open my file for a while this may lead my file to be expired before opening, so I tried to write code to make the expired date as a variable to make my batch a trial edition for one month as an example
and Here is my code

Code: [Select]
@echo off

:: Getting your local date
for /f "tokens=* delims=" %%a in ('wmic OS get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
:: Store the local date inside avariable
set "CurrentDateTime=%LocalDateTime:~0,8%"

IF Exist Exp_File.txt GOTO FILE_EXIST

:: Analysis the date into three parts
set /a "Current_Year=%CurrentDateTime:~0,4%"
set /a "Current_Month=%CurrentDateTime:~4,2%"
set /a "Current_Day=%CurrentDateTime:~6,2%"
echo %CurrentDateTime% & pause

::Determine the next month
set /a "Next_Month=%Current_Month%+1"
::echo %Next_Month% & pause
:STEP1
set /a "EXP_Date=%Current_Year%%Next_Month%%Current_Day%"
echo %EXP_Date%>Exp_File.txt
::echo %EXP_Date% & pause
GOTO STEP2

:FILE_EXIST
set /p EXP_Date=<Exp_File.txt
echo %EXP_Date% & pause

::Compare between Expiration date and Current date
:STEP2
IF %CurrentDateTime% GTR %EXP_Date% ( GOTO EXIT ) else GOTO PAYPASS

:PAYPASS
echo Application working
pause

:EXIT
echo Your Application is expired & pause
exit

But unfortunately, it didn't work as I can write a simple code only and after testing this code I found three issues in this code
1- it didn't work if i changed the month of my local PC to be 08 or 09 and it give me an error message

Code: [Select]
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

2- If I tried to change my date from 1 to 8 also it gives me the wrong expiration date stored in my outside file I mean if the current date is 20220601 the expiration date will be 2022701 instead of 20220701 and this leads to my batch displaying expiration message, so how to insert 0 before the number of months it ranges between 1 and 9

3- It didn't work for me if I changed my date month to 12  I mean how to calculate the next month if the starting month is 12 correctly?

Finally, how do I store my expiration date value as  a key in the registry file

Can I make a file that has a variable expiration date depending on the date it was run, for example,
for I want to make my batch expire after one month from starting date Or I must use a fixed expiration date stored inside the variable in the batch file as below.
Code: [Select]
@echo off
Title EXPIRATION DATE WITH BATCH
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
REM echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20201231"
:::::::::::::::::::::

if %LocalDateTime% GTR %EXP_DATE% (
 color 0C
 echo this wont work anymore
 Timeout /t 5 /NoBreak >NUL
 exit /b
)

START "" "Chrome"
Exit

This is the Expiration date coded by Hackoo and as I know he's from the best people here for coding but this date is a Fixed date, not a variable date for example IF I sent my batch to anyone and for some reason, he didn't open my file for a while this may lead my file to be expired before opening, so I tried to write code to make the expired date as a variable to make my batch a trial edition for one month as an example
and Here is my code

Code: [Select]
@echo off

:: Getting your local date
for /f "tokens=* delims=" %%a in ('wmic OS get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
:: Store the local date inside avariable
set "CurrentDateTime=%LocalDateTime:~0,8%"

IF Exist Exp_File.txt GOTO FILE_EXIST

:: Analysis the date into three parts
set /a "Current_Year=%CurrentDateTime:~0,4%"
set /a "Current_Month=%CurrentDateTime:~4,2%"
set /a "Current_Day=%CurrentDateTime:~6,2%"
echo %CurrentDateTime% & pause

::Determine the next month
set /a "Next_Month=%Current_Month%+1"
::echo %Next_Month% & pause
:STEP1
set /a "EXP_Date=%Current_Year%%Next_Month%%Current_Day%"
echo %EXP_Date%>Exp_File.txt
::echo %EXP_Date% & pause
GOTO STEP2

:FILE_EXIST
set /p EXP_Date=<Exp_File.txt
echo %EXP_Date% & pause

::Compare between Expiration date and Current date
:STEP2
IF %CurrentDateTime% GTR %EXP_Date% ( GOTO EXIT ) else GOTO PAYPASS

:PAYPASS
echo Application working
pause

:EXIT
echo Your Application is expired & pause
exit

But unfortunately, it didn't work as I can write a simple code only and after testing this code I found three issues in this code
1- it didn't work if i changed the month of my local PC to be 08 or 09 and it give me an error message
Code: [Select]
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

2- If I tried to change my date from 1 to 8 also it gives me the wrong expiration date stored in my outside file I mean if the current date is 20220601 the expiration date will be 2022701 instead of 20220701 and this leads to my batch displaying expiration message, so how to insert 0 before the number of months it ranges between 1 and 9

3- It didn't work for me if I changed my date month to 12  I mean how to calculate the next month if the starting month is 12 correctly?

Finally, how do I store my expiration date value as  a key in the registry file

Can I make a file that has a variable expiration date depending on the date it was run, for example,
for I want to make my batch expire after one month from starting date Or I must use a fixed expiration date stored inside the variable in the batch file as below.
Code: [Select]
@echo off
Title EXPIRATION DATE WITH BATCH
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
REM echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20201231"
:::::::::::::::::::::

if %LocalDateTime% GTR %EXP_DATE% (
 color 0C
 echo this wont work anymore
 Timeout /t 5 /NoBreak >NUL
 exit /b
)

START "" "Chrome"
Exit

This is the Expiration date coded by Hackoo and as I know he's from the best people here for coding but this date is a Fixed date, not a variable date for example IF I sent my batch to anyone and for some reason, he didn't open my file for a while this may lead my file to be expired before opening, so I tried to write code to make the expired date as a variable to make my batch a trial edition for one month as an example
and Here is my code

Code: [Select]
@echo off

:: Getting your local date
for /f "tokens=* delims=" %%a in ('wmic OS get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
:: Store the local date inside avariable
set "CurrentDateTime=%LocalDateTime:~0,8%"

IF Exist Exp_File.txt GOTO FILE_EXIST

:: Analysis the date into three parts
set /a "Current_Year=%CurrentDateTime:~0,4%"
set /a "Current_Month=%CurrentDateTime:~4,2%"
set /a "Current_Day=%CurrentDateTime:~6,2%"
echo %CurrentDateTime% & pause

::Determine the next month
set /a "Next_Month=%Current_Month%+1"
::echo %Next_Month% & pause
:STEP1
set /a "EXP_Date=%Current_Year%%Next_Month%%Current_Day%"
echo %EXP_Date%>Exp_File.txt
::echo %EXP_Date% & pause
GOTO STEP2

:FILE_EXIST
set /p EXP_Date=<Exp_File.txt
echo %EXP_Date% & pause

::Compare between Expiration date and Current date
:STEP2
IF %CurrentDateTime% GTR %EXP_Date% ( GOTO EXIT ) else GOTO PAYPASS

:PAYPASS
echo Application working
pause

:EXIT
echo Your Application is expired & pause
exit

But unfortunately, it didn't work as I can write a simple code only and after testing this code I found three issues in this code
1- it didn't work if i changed the month of my local PC to be 08 or 09 and it give me an error message
Code: [Select]
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

2- If I tried to change my date from 1 to 8 also it gives me the wrong expiration date stored in my outside file I mean if the current date is 20220601 the expiration date will be 2022701 instead of 20220701 and this leads to my batch displaying expiration message, so how to insert 0 before the number of months it ranges between 1 and 9

3- It didn't work for me if I changed my date month to 12  I mean how to calculate the next month if the starting month is 12 correctly?

Finally, how do I store my expiration date value as  a key in the registry file

Appreciate it for anyone can help

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Variable expiration date
« Reply #1 on: October 22, 2022, 01:44:45 PM »
Hi all,
My Question is repeated 3 times by mistake I'm so sorry.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Variable expiration date
« Reply #2 on: October 22, 2022, 10:43:33 PM »
 :'( Nobody?

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Variable expiration date
« Reply #3 on: October 24, 2022, 10:57:15 AM »
Hi  ;)
Code: [Select]
@echo off
Title Compare Current Date against EXPIRATION DATE WITH BATCH
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
:: Store the local date inside avariable
set "CurrentDateTime=%LocalDateTime:~0,8%"
:: Here adding 1 month for trial application
@for /f %%a in ('Powershell -C "(Get-Date).AddMonths(1).ToString('yyyyMMdd')"') do set "EXPDATE=%%a"
echo CurrentDateTime : %CurrentDateTime%
:: Check the registy if the varaible EXPDATE is set or not
reg query "HKCU\SOFTWARE\MyApp">nul 2>&1
If [%ErrorLevel%] EQU [1] Reg Add "HKCU\Software\MyApp" /v EXPDATE /t REG_SZ /d %EXPDATE%>nul 2>&1
:: Read from the registry the expiration date and set it as variable
@for /f "tokens=3" %%i in ('reg query "HKCU\SOFTWARE\MyApp" ^| find "EXPDATE"') do set "EXPDATE=%%i"
echo EXPDATE         : %EXPDATE%
:: Compare Current Date against EXPIRATION DATE ::
:::::::::::::::::::::
:STEP2
IF %CurrentDateTime% GTR %EXPDATE% ( GOTO EXIT ) else ( GOTO PAYPASS )
::-----------------------------------------------------------------
:PAYPASS
color 0A & echo Application working
pause
exit /b
::-----------------------------------------------------------------
:EXIT
Color 0C & echo Your Application is expired & pause
exit /b
::-----------------------------------------------------------------

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Variable expiration date
« Reply #4 on: October 31, 2022, 08:24:57 PM »
Thank you Hackoo so much it worked with me As always you are the man who can help.