Can I make a file that has a variable expiration date depending on the date it was run, for example, for one month from the date it was run Or I must use a fixed expiration date stored inside the variable in batch?
Can I make a file that has a variable expiration date depending on the date it was run, for example, for one month from the date it was run Or I must use a fixed expiration date stored inside the variable in batch?
Here is a batch-file/powershell hybrid version:
@echo off
if exist date_diff.ps1 (
call :test "(type "date_diff.ps1" | findstr "##">nul) ||" || for /F "delims=" %%i in ('powershell Get-Date -Format "yyyy-MM-dd"') do (echo ##%%i)>>date_diff.ps1
) else (
call :test
for /F "delims=" %%i in ('powershell Get-Date -Format "yyyy-MM-dd"') do (echo ##%%i)>>date_diff.ps1
)
for /f "tokens=1delims=#" %%i in ('type .\date_diff.ps1 ^| findstr "##"') do for /f "delims=" %%d in ('powershell .\date_diff.ps1 %%i') do if %%d geq 30 (
echo Old date lapsed: %%i
call :test "(type "date_diff.ps1" | findstr "##">nul) && "
)
goto :EOF
:test
%~1 (
(echo $startd = $args[0]
echo $endd = Get-Date -Format 'yyyy-MM-dd'
echo $result = New-TimeSpan -Start $startd -End $endd
echo $result.Days
)>date_diff.ps1
)
The batch-file will create a powershell file and also append the date at the time it is initially run into the file.
It will then compare the date each time the script is run. It will only echo the line Old date lapsed: <date> once the date is 30 days or older and then also append the new date into the powershell file.
To test it right away, you can run it once off, then edit date_diff.ps1 and change the date at the bottom of the file to a date older that 30 days from the run, then re-run the script.