Write batch file to delete all but latest X files of each type
I am trying to create a batch file which should be run once a day, which is supposed to delete all backup files other than the latest, lets say 2 of each type. So I am having files such as:
- backup-DAILY-2018-11-13_000000.bak
- backup-DAILY-2018-11-12_000000.bak
backup-DAILY-2018-11-11_000000.bak
backup-HOURLY-2018-11-13_110000.bak
- backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
etc.
So my script looks as follows currently
@ECHO off
SET frequency=%*
for %%x in (%frequency%) do (
for /f "*-%%x*" %%a in ('dir /ad /b /o-d^|more +2') do echo @file
)
but I am getting the error *-%x*" was unexpected at this time.
What the script should achieve is that once it is run each day I should have the latest 2 backup files of each type(DAILY, HOURLY). Right now I am just trying to print the files that need to be kept.
So what I should be left with is
- backup-DAILY-2018-11-13_000000.bak
backup-DAILY-2018-11-12_000000.bak
backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
batch-file
add a comment |
I am trying to create a batch file which should be run once a day, which is supposed to delete all backup files other than the latest, lets say 2 of each type. So I am having files such as:
- backup-DAILY-2018-11-13_000000.bak
- backup-DAILY-2018-11-12_000000.bak
backup-DAILY-2018-11-11_000000.bak
backup-HOURLY-2018-11-13_110000.bak
- backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
etc.
So my script looks as follows currently
@ECHO off
SET frequency=%*
for %%x in (%frequency%) do (
for /f "*-%%x*" %%a in ('dir /ad /b /o-d^|more +2') do echo @file
)
but I am getting the error *-%x*" was unexpected at this time.
What the script should achieve is that once it is run each day I should have the latest 2 backup files of each type(DAILY, HOURLY). Right now I am just trying to print the files that need to be kept.
So what I should be left with is
- backup-DAILY-2018-11-13_000000.bak
backup-DAILY-2018-11-12_000000.bak
backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
batch-file
1
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
3
The only fault I see is that you misplaced the"*-%%x*"it now looks like afor /r. Put it into the dir command. And I'd favor the"skip=2"over themore +2as the latter is another .exe to load versus the internal for command.
– LotPings
Nov 13 '18 at 15:15
2
Afterfor /F(and also afterfor /R) you cannot use a meta-variable like%%x(neither can you use delayed expansion there), only normal%-variables work here...
– aschipfl
Nov 13 '18 at 16:42
add a comment |
I am trying to create a batch file which should be run once a day, which is supposed to delete all backup files other than the latest, lets say 2 of each type. So I am having files such as:
- backup-DAILY-2018-11-13_000000.bak
- backup-DAILY-2018-11-12_000000.bak
backup-DAILY-2018-11-11_000000.bak
backup-HOURLY-2018-11-13_110000.bak
- backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
etc.
So my script looks as follows currently
@ECHO off
SET frequency=%*
for %%x in (%frequency%) do (
for /f "*-%%x*" %%a in ('dir /ad /b /o-d^|more +2') do echo @file
)
but I am getting the error *-%x*" was unexpected at this time.
What the script should achieve is that once it is run each day I should have the latest 2 backup files of each type(DAILY, HOURLY). Right now I am just trying to print the files that need to be kept.
So what I should be left with is
- backup-DAILY-2018-11-13_000000.bak
backup-DAILY-2018-11-12_000000.bak
backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
batch-file
I am trying to create a batch file which should be run once a day, which is supposed to delete all backup files other than the latest, lets say 2 of each type. So I am having files such as:
- backup-DAILY-2018-11-13_000000.bak
- backup-DAILY-2018-11-12_000000.bak
backup-DAILY-2018-11-11_000000.bak
backup-HOURLY-2018-11-13_110000.bak
- backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
etc.
So my script looks as follows currently
@ECHO off
SET frequency=%*
for %%x in (%frequency%) do (
for /f "*-%%x*" %%a in ('dir /ad /b /o-d^|more +2') do echo @file
)
but I am getting the error *-%x*" was unexpected at this time.
What the script should achieve is that once it is run each day I should have the latest 2 backup files of each type(DAILY, HOURLY). Right now I am just trying to print the files that need to be kept.
So what I should be left with is
- backup-DAILY-2018-11-13_000000.bak
backup-DAILY-2018-11-12_000000.bak
backup-HOURLY-2018-11-13_130000.bak
- backup-HOURLY-2018-11-13_150000.bak
batch-file
batch-file
asked Nov 13 '18 at 14:28
YantesYantes
12211
12211
1
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
3
The only fault I see is that you misplaced the"*-%%x*"it now looks like afor /r. Put it into the dir command. And I'd favor the"skip=2"over themore +2as the latter is another .exe to load versus the internal for command.
– LotPings
Nov 13 '18 at 15:15
2
Afterfor /F(and also afterfor /R) you cannot use a meta-variable like%%x(neither can you use delayed expansion there), only normal%-variables work here...
– aschipfl
Nov 13 '18 at 16:42
add a comment |
1
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
3
The only fault I see is that you misplaced the"*-%%x*"it now looks like afor /r. Put it into the dir command. And I'd favor the"skip=2"over themore +2as the latter is another .exe to load versus the internal for command.
– LotPings
Nov 13 '18 at 15:15
2
Afterfor /F(and also afterfor /R) you cannot use a meta-variable like%%x(neither can you use delayed expansion there), only normal%-variables work here...
– aschipfl
Nov 13 '18 at 16:42
1
1
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
3
3
The only fault I see is that you misplaced the
"*-%%x*" it now looks like a for /r. Put it into the dir command. And I'd favor the "skip=2" over the more +2 as the latter is another .exe to load versus the internal for command.– LotPings
Nov 13 '18 at 15:15
The only fault I see is that you misplaced the
"*-%%x*" it now looks like a for /r. Put it into the dir command. And I'd favor the "skip=2" over the more +2 as the latter is another .exe to load versus the internal for command.– LotPings
Nov 13 '18 at 15:15
2
2
After
for /F (and also after for /R) you cannot use a meta-variable like %%x (neither can you use delayed expansion there), only normal %-variables work here...– aschipfl
Nov 13 '18 at 16:42
After
for /F (and also after for /R) you cannot use a meta-variable like %%x (neither can you use delayed expansion there), only normal %-variables work here...– aschipfl
Nov 13 '18 at 16:42
add a comment |
1 Answer
1
active
oldest
votes
I am not sure what it is you are trying to achieve with the frequency, but nevertheless, I will focus on what you actualy want to achieve:
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *DAILY*') do echo "%%i"
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *HOURLY*') do echo "%%i"
So what happens is, we sort by date in reverse order, we then skip the first 2 files (being latest) and delete the rest, we just run 2 seperate loops for each type, hourly and daily. It can be incorporated into a single for loop, but I think this is simple enough.
Obviously replace echo with del once you confirm it does what you want..
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace theechowithdel. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that thedircommand is executed first and then the rest upon the results from that, and I could not find what/b /o-dmeant, I was looking in the documentation for theforcmd whereas I should have looked in the documentation for thedircmd instead. But it is more clear for me now so thank a lot.
– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283237%2fwrite-batch-file-to-delete-all-but-latest-x-files-of-each-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am not sure what it is you are trying to achieve with the frequency, but nevertheless, I will focus on what you actualy want to achieve:
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *DAILY*') do echo "%%i"
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *HOURLY*') do echo "%%i"
So what happens is, we sort by date in reverse order, we then skip the first 2 files (being latest) and delete the rest, we just run 2 seperate loops for each type, hourly and daily. It can be incorporated into a single for loop, but I think this is simple enough.
Obviously replace echo with del once you confirm it does what you want..
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace theechowithdel. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that thedircommand is executed first and then the rest upon the results from that, and I could not find what/b /o-dmeant, I was looking in the documentation for theforcmd whereas I should have looked in the documentation for thedircmd instead. But it is more clear for me now so thank a lot.
– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
add a comment |
I am not sure what it is you are trying to achieve with the frequency, but nevertheless, I will focus on what you actualy want to achieve:
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *DAILY*') do echo "%%i"
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *HOURLY*') do echo "%%i"
So what happens is, we sort by date in reverse order, we then skip the first 2 files (being latest) and delete the rest, we just run 2 seperate loops for each type, hourly and daily. It can be incorporated into a single for loop, but I think this is simple enough.
Obviously replace echo with del once you confirm it does what you want..
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace theechowithdel. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that thedircommand is executed first and then the rest upon the results from that, and I could not find what/b /o-dmeant, I was looking in the documentation for theforcmd whereas I should have looked in the documentation for thedircmd instead. But it is more clear for me now so thank a lot.
– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
add a comment |
I am not sure what it is you are trying to achieve with the frequency, but nevertheless, I will focus on what you actualy want to achieve:
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *DAILY*') do echo "%%i"
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *HOURLY*') do echo "%%i"
So what happens is, we sort by date in reverse order, we then skip the first 2 files (being latest) and delete the rest, we just run 2 seperate loops for each type, hourly and daily. It can be incorporated into a single for loop, but I think this is simple enough.
Obviously replace echo with del once you confirm it does what you want..
I am not sure what it is you are trying to achieve with the frequency, but nevertheless, I will focus on what you actualy want to achieve:
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *DAILY*') do echo "%%i"
for /F "skip=2 eol=: delims=" %%i in ('dir /b /o-d *HOURLY*') do echo "%%i"
So what happens is, we sort by date in reverse order, we then skip the first 2 files (being latest) and delete the rest, we just run 2 seperate loops for each type, hourly and daily. It can be incorporated into a single for loop, but I think this is simple enough.
Obviously replace echo with del once you confirm it does what you want..
answered Nov 13 '18 at 14:41
Gerhard BarnardGerhard Barnard
6,96731131
6,96731131
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace theechowithdel. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that thedircommand is executed first and then the rest upon the results from that, and I could not find what/b /o-dmeant, I was looking in the documentation for theforcmd whereas I should have looked in the documentation for thedircmd instead. But it is more clear for me now so thank a lot.
– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
add a comment |
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace theechowithdel. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that thedircommand is executed first and then the rest upon the results from that, and I could not find what/b /o-dmeant, I was looking in the documentation for theforcmd whereas I should have looked in the documentation for thedircmd instead. But it is more clear for me now so thank a lot.
– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace the
echo with del. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that the dir command is executed first and then the rest upon the results from that, and I could not find what /b /o-d meant, I was looking in the documentation for the for cmd whereas I should have looked in the documentation for the dir cmd instead. But it is more clear for me now so thank a lot.– Yantes
Nov 14 '18 at 7:26
Thanks a lot this seems to work, I will run it a few times to make sure and of course replace the
echo with del. I am a newbi in scripting, so I think I was pretty confused with how it all was working together. Like that the dir command is executed first and then the rest upon the results from that, and I could not find what /b /o-d meant, I was looking in the documentation for the for cmd whereas I should have looked in the documentation for the dir cmd instead. But it is more clear for me now so thank a lot.– Yantes
Nov 14 '18 at 7:26
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
No problems, just shout if you need help.
– Gerhard Barnard
Nov 14 '18 at 7:28
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283237%2fwrite-batch-file-to-delete-all-but-latest-x-files-of-each-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
I think you are misunderstanding quite a few things here...
– Gerhard Barnard
Nov 13 '18 at 14:34
3
The only fault I see is that you misplaced the
"*-%%x*"it now looks like afor /r. Put it into the dir command. And I'd favor the"skip=2"over themore +2as the latter is another .exe to load versus the internal for command.– LotPings
Nov 13 '18 at 15:15
2
After
for /F(and also afterfor /R) you cannot use a meta-variable like%%x(neither can you use delayed expansion there), only normal%-variables work here...– aschipfl
Nov 13 '18 at 16:42