Macro to find all files created in a given version

Programming and macros
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 481
x 90

Macro to find all files created in a given version

Unread post by Tera »

I've been searching around for a while to find a macro that can crawl in a folder (and its sub folders) and find all Solidworks files created in a given version (2023).

Do you any know any macro that can do the job?

Thanks.
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to find all files created in a given version

Unread post by Stefan Sterk »

Hi Tera, Have a look at https://www.codestack.net/solidworks-api/document/versions-report/

@artem code used the solidworks application to get the version history. Which limits the speed.
A faster way is to get the file atribute 'Last Saved With SW' itself, see example code below

Code: Select all

Sub main()
    Debug.Print GetLastSavedWithSW("E:\Path\To\File\FileName.SLDPRT")
End Sub

Function GetLastSavedWithSW(sFullPathName As String) As String
    Dim objShell  As Object
    Dim objFolder As Object
    Dim objFolderItem As Object
    Dim sFolderPath As Variant 'Needs to be variant for .NameSpace
    Dim sFileName As String
    
    sFolderPath = Left(sFullPathName, InStrRev(sFullPathName, "\") - 1)
    sFileName = Right(sFullPathName, Len(sFullPathName) - InStrRev(sFullPathName, "\"))
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace(sFolderPath)
    
    If objFolder Is Nothing Then Exit Function
    
    Set objFolderItem = objFolder.ParseName(sFileName)

    GetLastSavedWithSW = objFolder.GetDetailsOf(objFolderItem, 336)

    Set objFolderItem = Nothing
    Set objFolder = Nothing
    Set objShell = Nothing
End Function
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 481
x 90

Re: Macro to find all files created in a given version

Unread post by Tera »

Stefan Sterk wrote: Thu Jan 11, 2024 4:59 am Hi Tera, Have a look at https://www.codestack.net/solidworks-api/document/versions-report/

@artem code used the solidworks application to get the version history. Which limits the speed.
A faster way is to get the file atribute 'Last Saved With SW' itself, see example code below
I haven't reviewed @artem code yet, but your code returns null values.
What does 336 stands for?

Thanks.
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Macro to find all files created in a given version

Unread post by gupta9665 »

Tera wrote: Thu Jan 11, 2024 8:09 pm What does 336 stands for?
Thanks.
I believe that is the number on which "SW last saved with" comes in the list. But I do not think it is still exposed to VBA.
Tera wrote: Thu Jan 11, 2024 8:09 pm I've been searching around for a while to find a macro that can crawl in a folder (and its subfolders) and find all Solidworks files created in a given version (2023).
You can use @artem code as that would work OK for previous version (when SW last saved with was not exposed) and also for the batch processing of the files.

For active file, you can try the following codes (used the ConvertFileVersionToSwMajorVersion function from @artem code)

Code: Select all

Option Explicit

Sub main()

Dim swApp                     As SldWorks.SldWorks
Dim Part                      As SldWorks.ModelDoc2
Dim vVerStr                   As Variant
Dim swVersion                 As Integer

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
       
vVerStr = swApp.VersionHistory(Part.GetPathName)
swVersion = Left(vVerStr(UBound(vVerStr)), InStr(vVerStr(UBound(vVerStr)), "[") - 1)

MsgBox ConvertFileVersionToSwMajorVersion(swVersion)

End Sub


Function ConvertFileVersionToSwMajorVersion(versNumber As Integer) As String
    
    Dim swVersMajor As String
    
    If versNumber >= 5000 Then
        swVersMajor = 2012 + (versNumber - 5000) / 1000
    Else
        Select Case versNumber
            Case 44
                swVersMajor = 95
            Case 243
                swVersMajor = 96
            Case 483
                swVersMajor = 97
            Case 629
                swVersMajor = "97Plus"
            Case 822
                swVersMajor = 98
            Case 1008
                swVersMajor = "98Plus"
            Case 1137
                swVersMajor = 99
            Case 1500
                swVersMajor = 2000
            Case 1750
                swVersMajor = 2001
            Case 1950
                swVersMajor = "2001Plus"
            Case 2200
                swVersMajor = 2003
            Case 2500
                swVersMajor = 2004
            Case 2800
                swVersMajor = 2005
            Case 3100
                swVersMajor = 2006
            Case 3400
                swVersMajor = 2007
            Case 3800
                swVersMajor = 2008
            Case 4100
                swVersMajor = 2009
            Case 4400
                swVersMajor = 2010
            Case 4700
                swVersMajor = 2011
        End Select
    End If
    
    ConvertFileVersionToSwMajorVersion = "Last major version is: SOLIDWORKS " & swVersMajor
    
End Function
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to find all files created in a given version

Unread post by Stefan Sterk »

Tera wrote: Thu Jan 11, 2024 8:09 pm I haven't reviewed @artem code yet, but your code returns null values.
What does 336 stands for?

Thanks.
336 is the index for 'Last Saved With SW' extended file attribute/detail. A value that can be shown in the file explorer. See image below. This is working for me
image.png
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 481
x 90

Re: Macro to find all files created in a given version

Unread post by Tera »

gupta9665 wrote: Fri Jan 12, 2024 2:19 am You can use @artem code as that would work OK for previous version
I'm trying to check for future version.
I'm on 2022 SP5. Downloaded and used 2023 SP1 for a while . After a while our company decided to end its subscription.
I want to see how many files we have on version 2023.
If the amount of files are high, or sensitive assemblies are saved with 2023, we will extend our subscription for another year. If not, then we will end it now.

We have been on subscription since I think 2009, so some of us downloaded the latest version as ever. But it turned out the management has something else in mind.

thanks.
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Macro to find all files created in a given version

Unread post by gupta9665 »

Tera wrote: Fri Jan 12, 2024 3:18 am I'm trying to check for future version.
I'm on 2022 SP5. Downloaded and used 2023 SP1 for a while . After a while our company decided to end its subscription.
I want to see how many files we have on version 2023.
@artem macro will create a CSV file and list out details for all the files in the desired folder.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to find all files created in a given version

Unread post by Stefan Sterk »

As gupta said, the code of artem will fit your need, straid out of the box.

Got some other info. The index for 'Last Saved With SW' doens't seems to be contant. On my Windows 11 system is is set on 330 instead of 336 (Windows 10). Not sure if it is Windows related or user setting.
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Macro to find all files created in a given version

Unread post by gupta9665 »

Stefan Sterk wrote: Sat Jan 13, 2024 12:42 pm As gupta said, the code of artem will fit your need, straid out of the box.

Got some other info. The index for 'Last Saved With SW' doesn't seems to be constant. On my Windows 11 system is is set on 330 instead of 336 (Windows 10). Not sure if it is Windows related or user setting.
For me on WIn 10, the 336 number is the width of the column for 'Last Saved With SW' . Change the width of the column and you can see the number changes again.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 695
x 346

Re: Macro to find all files created in a given version

Unread post by mp3-250 »

iirc the version number is calculated as the sum of major and minor (SP) version of solidworks. codestack macro should have the calculation explained somewhere.
Post Reply