Removing old folders and keeping the most recent
up vote
0
down vote
favorite
I have been working on a script of late and have come across a snag. I am in the process of removing folders which are automatically created. I want to delete the older versions of those files whilst keeping the new folders untouched, for example:
- 18.212.1021.0008 //Created on the 19/11/2018 12:12
- 18.212.1021.0008_1 //Created on the 19/11/2018 12:23
- 18.212.1021.0008_2 //Created on the 19/11/2018 12:27
- 18.212.1021.0008_3 //Created on the 19/11/2018 12:32
I would want to keep 18.212.1021.008_3 so I guess I would need to keep the folder with the most recent creation date.
Please see the code below:
$Versionarray = 13..20
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Recurse | Where-Object {
# Recusivly deletes OneDrive version folders within
# Appdatalocal which build up everytime OneDrive
# is installed/script is run
$item = $_
$item -is [System.IO.DirectoryInfo] -and (
$Versionarray | Where-Object { $item.Name.Contains($_) }
)
} | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
powershell folder
add a comment |
up vote
0
down vote
favorite
I have been working on a script of late and have come across a snag. I am in the process of removing folders which are automatically created. I want to delete the older versions of those files whilst keeping the new folders untouched, for example:
- 18.212.1021.0008 //Created on the 19/11/2018 12:12
- 18.212.1021.0008_1 //Created on the 19/11/2018 12:23
- 18.212.1021.0008_2 //Created on the 19/11/2018 12:27
- 18.212.1021.0008_3 //Created on the 19/11/2018 12:32
I would want to keep 18.212.1021.008_3 so I guess I would need to keep the folder with the most recent creation date.
Please see the code below:
$Versionarray = 13..20
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Recurse | Where-Object {
# Recusivly deletes OneDrive version folders within
# Appdatalocal which build up everytime OneDrive
# is installed/script is run
$item = $_
$item -is [System.IO.DirectoryInfo] -and (
$Versionarray | Where-Object { $item.Name.Contains($_) }
)
} | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
powershell folder
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have been working on a script of late and have come across a snag. I am in the process of removing folders which are automatically created. I want to delete the older versions of those files whilst keeping the new folders untouched, for example:
- 18.212.1021.0008 //Created on the 19/11/2018 12:12
- 18.212.1021.0008_1 //Created on the 19/11/2018 12:23
- 18.212.1021.0008_2 //Created on the 19/11/2018 12:27
- 18.212.1021.0008_3 //Created on the 19/11/2018 12:32
I would want to keep 18.212.1021.008_3 so I guess I would need to keep the folder with the most recent creation date.
Please see the code below:
$Versionarray = 13..20
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Recurse | Where-Object {
# Recusivly deletes OneDrive version folders within
# Appdatalocal which build up everytime OneDrive
# is installed/script is run
$item = $_
$item -is [System.IO.DirectoryInfo] -and (
$Versionarray | Where-Object { $item.Name.Contains($_) }
)
} | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
powershell folder
I have been working on a script of late and have come across a snag. I am in the process of removing folders which are automatically created. I want to delete the older versions of those files whilst keeping the new folders untouched, for example:
- 18.212.1021.0008 //Created on the 19/11/2018 12:12
- 18.212.1021.0008_1 //Created on the 19/11/2018 12:23
- 18.212.1021.0008_2 //Created on the 19/11/2018 12:27
- 18.212.1021.0008_3 //Created on the 19/11/2018 12:32
I would want to keep 18.212.1021.008_3 so I guess I would need to keep the folder with the most recent creation date.
Please see the code below:
$Versionarray = 13..20
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Recurse | Where-Object {
# Recusivly deletes OneDrive version folders within
# Appdatalocal which build up everytime OneDrive
# is installed/script is run
$item = $_
$item -is [System.IO.DirectoryInfo] -and (
$Versionarray | Where-Object { $item.Name.Contains($_) }
)
} | Remove-Item -Recurse -Confirm:$false -ErrorAction SilentlyContinue
powershell folder
powershell folder
edited Nov 19 at 13:01
marsze
4,19531640
4,19531640
asked Nov 19 at 12:39
Marshal
53031527
53031527
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14
add a comment |
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
If the newest folder you want to keep is also the one with the newest creation time, you can use this simple one-liner:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
If you want to filter out only a specific type of folders by name, you could use a simple regex match. I cannot help you with the exact regex (since I would have to know your folder naming pattern) but it would look something like this:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | where Name -match 'dd+' | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
(Note that this is syntax might not work if you use an old Powershell version. If that's the case, let me know and I will provide a compatible fallback solution.)
UPDATE
In response to your comment: Your requirements are still a bit unclear, but here is something to get you started:
If you want to make sure to only delete folders that "look like" version folders, you can adjust the regex in the where-filter. _d+$
will match anything with an underscore and numbers at the end:
where $_.Name -match '_d+$'
If you also want to make sure, that this is actually a versioned copy of another existing folder, you could check that too:
where { $_.FullName -match '^(?<OriginalPath>.+)_d+$' -and (Test-Path $Matches.OriginalPath) }
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If the newest folder you want to keep is also the one with the newest creation time, you can use this simple one-liner:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
If you want to filter out only a specific type of folders by name, you could use a simple regex match. I cannot help you with the exact regex (since I would have to know your folder naming pattern) but it would look something like this:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | where Name -match 'dd+' | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
(Note that this is syntax might not work if you use an old Powershell version. If that's the case, let me know and I will provide a compatible fallback solution.)
UPDATE
In response to your comment: Your requirements are still a bit unclear, but here is something to get you started:
If you want to make sure to only delete folders that "look like" version folders, you can adjust the regex in the where-filter. _d+$
will match anything with an underscore and numbers at the end:
where $_.Name -match '_d+$'
If you also want to make sure, that this is actually a versioned copy of another existing folder, you could check that too:
where { $_.FullName -match '^(?<OriginalPath>.+)_d+$' -and (Test-Path $Matches.OriginalPath) }
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
add a comment |
up vote
1
down vote
If the newest folder you want to keep is also the one with the newest creation time, you can use this simple one-liner:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
If you want to filter out only a specific type of folders by name, you could use a simple regex match. I cannot help you with the exact regex (since I would have to know your folder naming pattern) but it would look something like this:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | where Name -match 'dd+' | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
(Note that this is syntax might not work if you use an old Powershell version. If that's the case, let me know and I will provide a compatible fallback solution.)
UPDATE
In response to your comment: Your requirements are still a bit unclear, but here is something to get you started:
If you want to make sure to only delete folders that "look like" version folders, you can adjust the regex in the where-filter. _d+$
will match anything with an underscore and numbers at the end:
where $_.Name -match '_d+$'
If you also want to make sure, that this is actually a versioned copy of another existing folder, you could check that too:
where { $_.FullName -match '^(?<OriginalPath>.+)_d+$' -and (Test-Path $Matches.OriginalPath) }
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
add a comment |
up vote
1
down vote
up vote
1
down vote
If the newest folder you want to keep is also the one with the newest creation time, you can use this simple one-liner:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
If you want to filter out only a specific type of folders by name, you could use a simple regex match. I cannot help you with the exact regex (since I would have to know your folder naming pattern) but it would look something like this:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | where Name -match 'dd+' | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
(Note that this is syntax might not work if you use an old Powershell version. If that's the case, let me know and I will provide a compatible fallback solution.)
UPDATE
In response to your comment: Your requirements are still a bit unclear, but here is something to get you started:
If you want to make sure to only delete folders that "look like" version folders, you can adjust the regex in the where-filter. _d+$
will match anything with an underscore and numbers at the end:
where $_.Name -match '_d+$'
If you also want to make sure, that this is actually a versioned copy of another existing folder, you could check that too:
where { $_.FullName -match '^(?<OriginalPath>.+)_d+$' -and (Test-Path $Matches.OriginalPath) }
If the newest folder you want to keep is also the one with the newest creation time, you can use this simple one-liner:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
If you want to filter out only a specific type of folders by name, you could use a simple regex match. I cannot help you with the exact regex (since I would have to know your folder naming pattern) but it would look something like this:
Get-ChildItem "$env:LOCALAPPDATAMicrosoftOneDrive" -Directory | where Name -match 'dd+' | sort CreationTime | select -SkipLast 1 | Remove-Item -Recurse -Force
(Note that this is syntax might not work if you use an old Powershell version. If that's the case, let me know and I will provide a compatible fallback solution.)
UPDATE
In response to your comment: Your requirements are still a bit unclear, but here is something to get you started:
If you want to make sure to only delete folders that "look like" version folders, you can adjust the regex in the where-filter. _d+$
will match anything with an underscore and numbers at the end:
where $_.Name -match '_d+$'
If you also want to make sure, that this is actually a versioned copy of another existing folder, you could check that too:
where { $_.FullName -match '^(?<OriginalPath>.+)_d+$' -and (Test-Path $Matches.OriginalPath) }
edited Nov 20 at 8:01
answered Nov 19 at 13:01
marsze
4,19531640
4,19531640
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
add a comment |
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
That looks great, the only issue is there are other folders in there like: - OneDrive.exe - Logs - Help This is why I have an array for looking for folders with numbers in them. :)
– Marshal
Nov 19 at 13:02
1
1
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Marsze, that is one sexy one-liner. I love it.
– Marshal
Nov 19 at 13:42
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
Hi Marsze, looking at my response to AnsgarWiechers is there anyway to target only those version folders? If I run my full script over and over eventually it breaks down, it could be removing other folders.
– Marshal
Nov 19 at 14:05
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
@Marshal I've extended my answer. I hope that helps you to get started and create your final solution yourself.
– marsze
Nov 20 at 8:02
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.
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.
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%2f53374855%2fremoving-old-folders-and-keeping-the-most-recent%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
The sample code you posted is broken. Please create a Minimal, Complete, and Verifiable example, then edit your question and post that code along with all errors that code throws. Also, do the names all folders you want processed have the same format? Like, some text optionally followed by an underscore and a number?
– Ansgar Wiechers
Nov 19 at 12:53
Apologies, I have removed the curly bracket below it. This should now work. If the folder is locked by the system you might see a few errors but this would be the folder I would want to avoid and keep.
– Marshal
Nov 19 at 13:00
Please also answer the question from my previous comment.
– Ansgar Wiechers
Nov 19 at 13:05
@AnsgarWiechers Your question is an interesting on. Sooo, the folder is randomly generated. The folder relates to the version so if the folder name for the original version is free then OneDrive will use that. If it's not then it will add an underscore and increment the number. This means that if you have 18.212.1021.0008, 18.212.1021.0008_1, 18.212.1021.0008_2 and delete "18.212.1021.0008" then the next time OneDrive re-creates the folder it will use 18.212.1021.0008. Hence why I want the others removed to stop the folder getting bunged up with lots of folders.
– Marshal
Nov 19 at 13:14