Clean up 'OldVersions' using Powershell RegEx

Clean up 'OldVersions' using Powershell RegEx

DavidWHouse
Enthusiast Enthusiast
780 Views
6 Replies
Message 1 of 7

Clean up 'OldVersions' using Powershell RegEx

DavidWHouse
Enthusiast
Enthusiast

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.)

781 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Actually an easier way is to open a window and select the drive where your files are. Since all the old versions are stored in a folder named OldVersions you can simply search for "oldversions" and all the folders will be listed. Select them and delete. Done.

0 Likes
Message 3 of 7

DavidWHouse
Enthusiast
Enthusiast

It's a welcome addition to dealing with the problem, at least in general. Thank you.

However, given that the Powershell micro-program is provided here, neither solution is 'easier'. The Powershell solution is (was) indeed rather more complex to develop, but once in hand, then either solution becomes equally easy to use.

The real difference between the two is that the 'search folders' solution assumes (what may be true for many, which is) that all files in the OldVersions folders are available to be deleted. However, in my case, I sometimes find myself generating files that I wish to save (for whatever reason), but which I do not want to leave in the working folder. Sometimes I put them in a new sub-folder or elsewhere, and sometimes rather than taking the time to do something else, I put them in OldVersions. As such, I prefer to have more control over what is deleted and what is not, as explained in the initial post.

To say it in other words, the Powershell solution deletes files based on name. (Targeting only those files that match the 'backup' naming pattern used by Inventor;) The 'search folders'solution deletes all files based on location. (Targeting all files in those folders.) It was because I wanted better granularity that I developed the Powershell solution.

Regardless, again, good to have this additional tool. Let a thousand flowers bloom.

0 Likes
Message 4 of 7

mcgyvr
Consultant
Consultant

@DavidWHouse David are you aware that you can change project file settings to not save old versions or only keep 1 copy or just a few or none at all?

I'm assuming you need to do this due to hard drive space issues?

 

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 5 of 7

DavidWHouse
Enthusiast
Enthusiast

I did not know about that setting; or if I did, had forgotten. Appreciated.

As far as the why of clean up, on the one hand I want the safety of having previous versions available (at least until I've done sufficient back up of the given project), and on the other, yes, to a degree, keeping things clean as a result of a disk space issue: I keep my most active project(s) on a solid-state drive (half terabyte), which I find makes Inventor more responsive. 

But as well, my approach to such issues arises largely because I believe in being paranoid and compulsive-- or perhaps partly because of a soupçon of ADHD-- at least as far as work product is concerned. (That approach is a poor fit for raising children, in a counter-example.)

Good backup procedures (for me, the new version of Google Drive helps, as well as SyncThing), consistent file naming (critical for avoiding duplication of effort and dealing with multi-meg file collections), good folder structure (tree-structured, in my case, according to the nature of the part or assembly, and where jigs, for example, have their own folders vs. folders for manufactured items, et al); and so on.

Again, thanks for the mention. I have learned a lot from your posts over the past few years.

0 Likes
Message 6 of 7

mcgyvr
Consultant
Consultant

I figured it was ADHD in there somewhere Smiley Tongue

 

IMO hard drive space is cheap.. If I need to do anything to "cleanup" then my time/money is better spent just getting more HD space..

 

 

FYI..

In your project file options folder...A setting of

-1 = keep all old version

0 = keep none

1 = keep one

2 = keep two

etc...

 

I personally set mine to 1 as you can see

I have backups done nightly to multiple onsite and off site backup locations though

If a hard drive fails no amount of old versions on that drive are going to matter..

I also have only needed to recover from an old versions file once in my Inventor career of easily 10+ years

 oldversions.PNG

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 7 of 7

Anonymous
Not applicable

I said you can select the drive but you actually can be much more specific so as not to delete files that you want to keep. I always keep older versions until I am satisfied that the project is complete. Then I can open the particular project folder and search "oldversions" in that main project folder and it will show all the subfolders. At that point you can delete them and it will only delete the folders you selected there by cleaning up that project and not affecting others. I am sure powershell is a useful tool, as in most CAD programs there are many ways to do the same task. Thanks for responding.

0 Likes