Get the difference between current date and a previous date











up vote
1
down vote

favorite












If I enter echo %date% in cmd I will get the date it is today. Let us say that today is Wednesday, 21/11/2018. If I type echo %date% cmd will print Wed 11/21/2018. Yesterday, it would have returned Tue 11/20/2018. The difference between these two dates (in days) is 1.



Is there any way I can do this kind of subtraction in command prompt? Something like:



set previous_date=Tue 11/20/2018
echo %date% - %previous_date%


I know this should be very easy to do in a programming language like python or VBScript, but I don't want to use any of those, unless it is absolutely necessary. I can download and use custom command line tools, but I want to do this with a batch file.



Anyone who knows how?










share|improve this question
























  • Is there any maximum number of days? Or it could be any number?
    – Aacini
    Nov 21 at 13:37










  • @Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
    – Asif Ali
    Nov 21 at 13:39












  • It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
    – Aacini
    Nov 21 at 13:42










  • @Aacini thanks waiting
    – Asif Ali
    Nov 21 at 13:43










  • @Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
    – Asif Ali
    Nov 21 at 13:56

















up vote
1
down vote

favorite












If I enter echo %date% in cmd I will get the date it is today. Let us say that today is Wednesday, 21/11/2018. If I type echo %date% cmd will print Wed 11/21/2018. Yesterday, it would have returned Tue 11/20/2018. The difference between these two dates (in days) is 1.



Is there any way I can do this kind of subtraction in command prompt? Something like:



set previous_date=Tue 11/20/2018
echo %date% - %previous_date%


I know this should be very easy to do in a programming language like python or VBScript, but I don't want to use any of those, unless it is absolutely necessary. I can download and use custom command line tools, but I want to do this with a batch file.



Anyone who knows how?










share|improve this question
























  • Is there any maximum number of days? Or it could be any number?
    – Aacini
    Nov 21 at 13:37










  • @Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
    – Asif Ali
    Nov 21 at 13:39












  • It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
    – Aacini
    Nov 21 at 13:42










  • @Aacini thanks waiting
    – Asif Ali
    Nov 21 at 13:43










  • @Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
    – Asif Ali
    Nov 21 at 13:56















up vote
1
down vote

favorite









up vote
1
down vote

favorite











If I enter echo %date% in cmd I will get the date it is today. Let us say that today is Wednesday, 21/11/2018. If I type echo %date% cmd will print Wed 11/21/2018. Yesterday, it would have returned Tue 11/20/2018. The difference between these two dates (in days) is 1.



Is there any way I can do this kind of subtraction in command prompt? Something like:



set previous_date=Tue 11/20/2018
echo %date% - %previous_date%


I know this should be very easy to do in a programming language like python or VBScript, but I don't want to use any of those, unless it is absolutely necessary. I can download and use custom command line tools, but I want to do this with a batch file.



Anyone who knows how?










share|improve this question















If I enter echo %date% in cmd I will get the date it is today. Let us say that today is Wednesday, 21/11/2018. If I type echo %date% cmd will print Wed 11/21/2018. Yesterday, it would have returned Tue 11/20/2018. The difference between these two dates (in days) is 1.



Is there any way I can do this kind of subtraction in command prompt? Something like:



set previous_date=Tue 11/20/2018
echo %date% - %previous_date%


I know this should be very easy to do in a programming language like python or VBScript, but I don't want to use any of those, unless it is absolutely necessary. I can download and use custom command line tools, but I want to do this with a batch file.



Anyone who knows how?







date batch-file cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 3:07









double-beep

668116




668116










asked Nov 21 at 13:33









Asif Ali

9418




9418












  • Is there any maximum number of days? Or it could be any number?
    – Aacini
    Nov 21 at 13:37










  • @Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
    – Asif Ali
    Nov 21 at 13:39












  • It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
    – Aacini
    Nov 21 at 13:42










  • @Aacini thanks waiting
    – Asif Ali
    Nov 21 at 13:43










  • @Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
    – Asif Ali
    Nov 21 at 13:56




















  • Is there any maximum number of days? Or it could be any number?
    – Aacini
    Nov 21 at 13:37










  • @Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
    – Asif Ali
    Nov 21 at 13:39












  • It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
    – Aacini
    Nov 21 at 13:42










  • @Aacini thanks waiting
    – Asif Ali
    Nov 21 at 13:43










  • @Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
    – Asif Ali
    Nov 21 at 13:56


















Is there any maximum number of days? Or it could be any number?
– Aacini
Nov 21 at 13:37




Is there any maximum number of days? Or it could be any number?
– Aacini
Nov 21 at 13:37












@Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
– Asif Ali
Nov 21 at 13:39






@Aacini any number. Even if you don't have the complete answer, just an idea on how this might be done, please share. You might point me in the right direction.
– Asif Ali
Nov 21 at 13:39














It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
– Aacini
Nov 21 at 13:42




It is relatively simple to get a difference when both dates are separated by a max of one month. Otherwise the method is more complex... I will post a partial answer soon.
– Aacini
Nov 21 at 13:42












@Aacini thanks waiting
– Asif Ali
Nov 21 at 13:43




@Aacini thanks waiting
– Asif Ali
Nov 21 at 13:43












@Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
– Asif Ali
Nov 21 at 13:56






@Aacini since you are involved I might as well tell you why I need this. I have a bat file in my Startup folder that runs a process once a week. It does this by checking the current day and running the process if its monday if %date:~0,1%==M (). Problem is I need this process to launch AT LEAST once every 7 days. But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday. So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days. regardless of what day it is.
– Asif Ali
Nov 21 at 13:56














2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer)



Batch scripting language does not have any built-in means to calculate time difference. This problem may be solved through various means such as:




  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.

  • Download existing external program such as unix date utility from gnuwin32 coreutils (manual, recipe). Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.

  • Parse output from built-in date command or %date% variable. This is discussed in great detail elsewhere (A, B, C, D, E) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.


I'd like to share another trick: using file creation date difference. It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.



Here is an example, explanation below:



if not exist "c:somemarker.txt" goto RunTheJob
robocopy /minage:7 "c:somemarker.txt" "c:sometest.txt"
if exist "c:sometest.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:sometest.txt"
echo 1 >"c:somemarker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob


Whenever we run the job (i.e. once in 7 days) we also create (or update) a "marker" file (c:somemarker.txt in the example). Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME



This also neatly solves the problem of storing date information somewhere.



Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old. Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.



So we periodically try to copy the file and see if it succeeds. We do this by checking if copied file (c:sometest.txt) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL



Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME. /y overrides confirmations just in case and may normally be omitted.



If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away. If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.



Quirks:



If some program updates the modification date of a "marker" file for some reason, the job will be delayed. If this is undesired you should put this file somewhere out of reach, such as %AppData%RoamingYOUR_SCRIPT_NAME folder. Same thing may happen if the file gets restored from a backup.



To work around such a problem you may consider compressing the marker file into an archive such as zip, but this also requires external programs, VBS or PowerShell. It might be possible to somehow abuse built-in Windows Backup utility (sdclt) instead but I have not found a way.






share|improve this answer





















  • thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
    – Asif Ali
    Nov 23 at 16:08


















up vote
3
down vote













In one of your comments you say:




I have a bat file in my Startup folder that runs a process once a
week. It does this by checking the current day and running the process
if its monday if %date:~0,1%==M (). Problem is I need this process to
launch AT LEAST once every 7 days. But if say I forget to open my
laptop on a particular monday this it doesn't launch the process until
the next monday. So now I simply want to change the logic to launch
the process if it hasn't been launched for at least 7 days




That's what TaskScheduler is built to take care of. No need to mess with date calculations or even language dependend formats of %date%.



Start TaskScheduler

"Create Task" (do not "Create Basic Task")

in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday

in "Settings" check "Run task as soon as possible after a scheduled start is missed"

Fill also the "General", "Actions" and "Conditions" tabs to your needs.






share|improve this answer





















  • thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
    – Asif Ali
    Nov 23 at 16:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413224%2fget-the-difference-between-current-date-and-a-previous-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer)



Batch scripting language does not have any built-in means to calculate time difference. This problem may be solved through various means such as:




  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.

  • Download existing external program such as unix date utility from gnuwin32 coreutils (manual, recipe). Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.

  • Parse output from built-in date command or %date% variable. This is discussed in great detail elsewhere (A, B, C, D, E) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.


I'd like to share another trick: using file creation date difference. It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.



Here is an example, explanation below:



if not exist "c:somemarker.txt" goto RunTheJob
robocopy /minage:7 "c:somemarker.txt" "c:sometest.txt"
if exist "c:sometest.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:sometest.txt"
echo 1 >"c:somemarker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob


Whenever we run the job (i.e. once in 7 days) we also create (or update) a "marker" file (c:somemarker.txt in the example). Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME



This also neatly solves the problem of storing date information somewhere.



Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old. Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.



So we periodically try to copy the file and see if it succeeds. We do this by checking if copied file (c:sometest.txt) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL



Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME. /y overrides confirmations just in case and may normally be omitted.



If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away. If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.



Quirks:



If some program updates the modification date of a "marker" file for some reason, the job will be delayed. If this is undesired you should put this file somewhere out of reach, such as %AppData%RoamingYOUR_SCRIPT_NAME folder. Same thing may happen if the file gets restored from a backup.



To work around such a problem you may consider compressing the marker file into an archive such as zip, but this also requires external programs, VBS or PowerShell. It might be possible to somehow abuse built-in Windows Backup utility (sdclt) instead but I have not found a way.






share|improve this answer





















  • thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
    – Asif Ali
    Nov 23 at 16:08















up vote
1
down vote



accepted










(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer)



Batch scripting language does not have any built-in means to calculate time difference. This problem may be solved through various means such as:




  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.

  • Download existing external program such as unix date utility from gnuwin32 coreutils (manual, recipe). Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.

  • Parse output from built-in date command or %date% variable. This is discussed in great detail elsewhere (A, B, C, D, E) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.


I'd like to share another trick: using file creation date difference. It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.



Here is an example, explanation below:



if not exist "c:somemarker.txt" goto RunTheJob
robocopy /minage:7 "c:somemarker.txt" "c:sometest.txt"
if exist "c:sometest.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:sometest.txt"
echo 1 >"c:somemarker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob


Whenever we run the job (i.e. once in 7 days) we also create (or update) a "marker" file (c:somemarker.txt in the example). Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME



This also neatly solves the problem of storing date information somewhere.



Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old. Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.



So we periodically try to copy the file and see if it succeeds. We do this by checking if copied file (c:sometest.txt) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL



Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME. /y overrides confirmations just in case and may normally be omitted.



If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away. If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.



Quirks:



If some program updates the modification date of a "marker" file for some reason, the job will be delayed. If this is undesired you should put this file somewhere out of reach, such as %AppData%RoamingYOUR_SCRIPT_NAME folder. Same thing may happen if the file gets restored from a backup.



To work around such a problem you may consider compressing the marker file into an archive such as zip, but this also requires external programs, VBS or PowerShell. It might be possible to somehow abuse built-in Windows Backup utility (sdclt) instead but I have not found a way.






share|improve this answer





















  • thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
    – Asif Ali
    Nov 23 at 16:08













up vote
1
down vote



accepted







up vote
1
down vote



accepted






(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer)



Batch scripting language does not have any built-in means to calculate time difference. This problem may be solved through various means such as:




  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.

  • Download existing external program such as unix date utility from gnuwin32 coreutils (manual, recipe). Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.

  • Parse output from built-in date command or %date% variable. This is discussed in great detail elsewhere (A, B, C, D, E) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.


I'd like to share another trick: using file creation date difference. It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.



Here is an example, explanation below:



if not exist "c:somemarker.txt" goto RunTheJob
robocopy /minage:7 "c:somemarker.txt" "c:sometest.txt"
if exist "c:sometest.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:sometest.txt"
echo 1 >"c:somemarker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob


Whenever we run the job (i.e. once in 7 days) we also create (or update) a "marker" file (c:somemarker.txt in the example). Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME



This also neatly solves the problem of storing date information somewhere.



Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old. Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.



So we periodically try to copy the file and see if it succeeds. We do this by checking if copied file (c:sometest.txt) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL



Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME. /y overrides confirmations just in case and may normally be omitted.



If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away. If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.



Quirks:



If some program updates the modification date of a "marker" file for some reason, the job will be delayed. If this is undesired you should put this file somewhere out of reach, such as %AppData%RoamingYOUR_SCRIPT_NAME folder. Same thing may happen if the file gets restored from a backup.



To work around such a problem you may consider compressing the marker file into an archive such as zip, but this also requires external programs, VBS or PowerShell. It might be possible to somehow abuse built-in Windows Backup utility (sdclt) instead but I have not found a way.






share|improve this answer












(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer)



Batch scripting language does not have any built-in means to calculate time difference. This problem may be solved through various means such as:




  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.

  • Download existing external program such as unix date utility from gnuwin32 coreutils (manual, recipe). Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.

  • Parse output from built-in date command or %date% variable. This is discussed in great detail elsewhere (A, B, C, D, E) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.


I'd like to share another trick: using file creation date difference. It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.



Here is an example, explanation below:



if not exist "c:somemarker.txt" goto RunTheJob
robocopy /minage:7 "c:somemarker.txt" "c:sometest.txt"
if exist "c:sometest.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:sometest.txt"
echo 1 >"c:somemarker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob


Whenever we run the job (i.e. once in 7 days) we also create (or update) a "marker" file (c:somemarker.txt in the example). Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME



This also neatly solves the problem of storing date information somewhere.



Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old. Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.



So we periodically try to copy the file and see if it succeeds. We do this by checking if copied file (c:sometest.txt) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL



Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME. /y overrides confirmations just in case and may normally be omitted.



If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away. If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.



Quirks:



If some program updates the modification date of a "marker" file for some reason, the job will be delayed. If this is undesired you should put this file somewhere out of reach, such as %AppData%RoamingYOUR_SCRIPT_NAME folder. Same thing may happen if the file gets restored from a backup.



To work around such a problem you may consider compressing the marker file into an archive such as zip, but this also requires external programs, VBS or PowerShell. It might be possible to somehow abuse built-in Windows Backup utility (sdclt) instead but I have not found a way.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 22:01









Jack White

25114




25114












  • thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
    – Asif Ali
    Nov 23 at 16:08


















  • thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
    – Asif Ali
    Nov 23 at 16:08
















thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
– Asif Ali
Nov 23 at 16:08




thanks for the detailed info. Thanks to you I got what I wanted when I posted my question on stackoverflow. File age! so simple, yet genius.
– Asif Ali
Nov 23 at 16:08












up vote
3
down vote













In one of your comments you say:




I have a bat file in my Startup folder that runs a process once a
week. It does this by checking the current day and running the process
if its monday if %date:~0,1%==M (). Problem is I need this process to
launch AT LEAST once every 7 days. But if say I forget to open my
laptop on a particular monday this it doesn't launch the process until
the next monday. So now I simply want to change the logic to launch
the process if it hasn't been launched for at least 7 days




That's what TaskScheduler is built to take care of. No need to mess with date calculations or even language dependend formats of %date%.



Start TaskScheduler

"Create Task" (do not "Create Basic Task")

in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday

in "Settings" check "Run task as soon as possible after a scheduled start is missed"

Fill also the "General", "Actions" and "Conditions" tabs to your needs.






share|improve this answer





















  • thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
    – Asif Ali
    Nov 23 at 16:10















up vote
3
down vote













In one of your comments you say:




I have a bat file in my Startup folder that runs a process once a
week. It does this by checking the current day and running the process
if its monday if %date:~0,1%==M (). Problem is I need this process to
launch AT LEAST once every 7 days. But if say I forget to open my
laptop on a particular monday this it doesn't launch the process until
the next monday. So now I simply want to change the logic to launch
the process if it hasn't been launched for at least 7 days




That's what TaskScheduler is built to take care of. No need to mess with date calculations or even language dependend formats of %date%.



Start TaskScheduler

"Create Task" (do not "Create Basic Task")

in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday

in "Settings" check "Run task as soon as possible after a scheduled start is missed"

Fill also the "General", "Actions" and "Conditions" tabs to your needs.






share|improve this answer





















  • thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
    – Asif Ali
    Nov 23 at 16:10













up vote
3
down vote










up vote
3
down vote









In one of your comments you say:




I have a bat file in my Startup folder that runs a process once a
week. It does this by checking the current day and running the process
if its monday if %date:~0,1%==M (). Problem is I need this process to
launch AT LEAST once every 7 days. But if say I forget to open my
laptop on a particular monday this it doesn't launch the process until
the next monday. So now I simply want to change the logic to launch
the process if it hasn't been launched for at least 7 days




That's what TaskScheduler is built to take care of. No need to mess with date calculations or even language dependend formats of %date%.



Start TaskScheduler

"Create Task" (do not "Create Basic Task")

in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday

in "Settings" check "Run task as soon as possible after a scheduled start is missed"

Fill also the "General", "Actions" and "Conditions" tabs to your needs.






share|improve this answer












In one of your comments you say:




I have a bat file in my Startup folder that runs a process once a
week. It does this by checking the current day and running the process
if its monday if %date:~0,1%==M (). Problem is I need this process to
launch AT LEAST once every 7 days. But if say I forget to open my
laptop on a particular monday this it doesn't launch the process until
the next monday. So now I simply want to change the logic to launch
the process if it hasn't been launched for at least 7 days




That's what TaskScheduler is built to take care of. No need to mess with date calculations or even language dependend formats of %date%.



Start TaskScheduler

"Create Task" (do not "Create Basic Task")

in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday

in "Settings" check "Run task as soon as possible after a scheduled start is missed"

Fill also the "General", "Actions" and "Conditions" tabs to your needs.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 at 9:09









Stephan

34.2k43153




34.2k43153












  • thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
    – Asif Ali
    Nov 23 at 16:10


















  • thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
    – Asif Ali
    Nov 23 at 16:10
















thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
– Asif Ali
Nov 23 at 16:10




thanks for your answer, although I used Jack White's way of doing it, yours probably also works. Hopefully it will help someone else reading this question in the future.
– Asif Ali
Nov 23 at 16:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413224%2fget-the-difference-between-current-date-and-a-previous-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo