Clean up 'OldVersions' using Powershell RegEx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
My apologies if this is duplicative; I could not find another mention of it.
The backup files in the OldVersions folders in any project may be either very useful or alternatively unwanted. If you wish to get rid of those backup files automatically, then copy and paste the following into a PowerShell window:
$filter = [regex] "\.{1}\d{4}\.{1}[A-Za-z]{3}$"
((Get-ChildItem $srcFolder -recurse) -match $filter) | remove-item -Force -Debug
This will find (and ask you if you want to delete) only those files that match the $filter RegEx expression, which parses to
\.{1} one and only one period, followed by
\d{4} four and only four digits, followed by
\.{1} one and only one period, followed by
[A-Za-z]{3} three and only three letters,
$ ...at the end of the line (filename)
Thus this instructs Powershell (in its current directory and in all directories which are children of the current directory), to delete files that end in strings such as "AnyString.2960.iam". Now of course if you name your files with embedded periods followed by four digits followed by the filetype, then this will not work well for you, but as you know, filenames which match this pattern are those which Inventor creates and puts in the OldVersions directory.
Since the command sequence will ask you about every file, you can try it out and simply type "n" (and Enter) if something is not as you would wish. If it has identified a file you want to eliminate, then type "y" and Enter. If it proves, after the first 10 or 50 files to be targeting only your backup files, then you can type "a" and Enter, and it will delete all remaining files it identifies.
I keep a text file with the two Powershell RegEx lines in it where I can find it, then after invoking Powershell, I open the text file, copy the two lines, return to the Powershell window and paste the two lines.
Finally, I would mention that on any Windows File Explorer window, if you type "powershell" in the address bar, it will open a powershell window using the directory being displayed as the current directory. (If you type "cmd" you'll get the old DOS shell.)