10 Replies Latest reply on Jun 20, 2018 2:29 AM by Ivana Kolin

    Searching a txt file for a certain variant

    Robert Voogt

      Hi all,

       

      I use this code to look for the value of LogFile inside a txt file:

       

       

                  LogFile = fso.GetBaseName(DrawFile)
      
      
                  Dim strFilename As String: strFilename = "D:\Spare Part Generator\Log\Processed Assemblies.txt"
                  Dim strTextLine As String
                  Dim iFile As Integer: iFile = FreeFile
                  Open strFilename For Input As #iFile
                  Debug.Print strFilename & " Opened."
                  Do Until EOF(1)
                      Line Input #1, strTextLine
                      Dim TempString As String
                      TempString = UCase(Trim(strTextLine))
                  
                      If Left(TempString, 8) <> LogFile Then
                                  'Start of opening drawing and running Macro
                                  Set doc = swApp.OpenDoc6(DrawFile, swDocDRAWING, 2, "", fileerror, filewarning) '(FileName, Type, Options (2 = read only), Configuration, Errors, Warnings)
                                  boolstatus = swApp.RunMacro2("D:\Spare Part Generator\Macro\MARS RDC.swp", "RDC", "main", swRunMacroDefault, False)
                                  
                                  'Writing name of assembly to Log
                                  Set fso = CreateObject("Scripting.FileSystemObject")
                                  Set oFile = fso.OpenTextFile("D:\Spare Part Generator\Log\Processed Assemblies.txt", 8, True) '8 = ForAppending
                                  oFile.WriteLine LogFile & " - Date: " & Format(Date, "ddmmmyyyy") & " - Time: " & Format(Time, "HH:MM:SS")
                                  oFile.Close
                                  Set fso = Nothing
                                  Set oFile = Nothing
                      End If
                  Loop
                  Close #iFile
      

       

      I do not get errors, but it does not look correctly in the txt file. the value of LogFile is present in the txt file but is not seen. Tempstring shows only the first sentence I added tot the file Processed Assemblies.txt.

        • Re: Searching a txt file for a certain variant
          Alex Burnett

          It is very difficult to see what's going on here without an example file/macro combination. I'll take a guess at what might be going on.

           

          You are converting the file name to uppercase and then getting an 8 character substring to compare to LogFile. The only issues that I can guess are that either LogFile is not uppercase or that LogFile is not 8 characters in length.

           

          Can you add a Debug.Print statement to output the values of each to the debug window before you compare them so that you can ensure that the formats are correct?

            • Re: Searching a txt file for a certain variant
              Robert Voogt

              Hi Alex,

               

              I actually do not want to use that '8'

               

              LogFile is always a file name, for example: 00005417.

              It needs to look for 00005417 in the txt file to know if it has been processed before. If it finds 00005417 it stops and continues other code. If it doesn't find 00005417 it will open the file and proces another macro.

               

              The problem is, is that Tempstring doesn't look at the complete document. I placed 00005417 in the file so it should see it.

              • Re: Searching a txt file for a certain variant
                Robert Voogt

                here the complete code:

                 

                ' *******************************************************************************
                ' Manual Access & Retrieval System --- MARS ---
                ' *******************************************************************************
                ' Control --- RDC
                ' *******************************************************************************
                '
                '
                ' *What does it do?*
                '
                ' Checks given location in main sub for all assemblies
                ' Retrieves the drawings of those assemblies
                ' Runs macro MARS RDC for each drawing
                '
                ' *******************************************************************************
                
                
                Dim AsmFile         As Variant
                Dim DrawFile        As Variant
                Dim FolderPath      As String
                Dim fileerror       As Long
                Dim filewarning     As Long
                Dim fso             As Object
                Dim LogFile         As Variant
                Dim subFolder       As Variant
                Dim swApp           As SldWorks.SldWorks
                
                
                
                
                
                
                Sub main()
                
                
                
                
                
                
                '   Setting processing folder
                
                
                        Set fso = CreateObject("Scripting.FileSystemObject")
                        
                        'Folder Location
                        FolderPath = "D:\Spare Part Generator\Test drawings\" 'Test environment
                        'FolderPath = "C:\PDMWorks View\Foodmate\Sub samenstellingen\"
                        
                        ProcessFolder fso.GetFolder(FolderPath)
                
                
                
                
                End Sub
                
                
                Sub ProcessFolder(folder)
                    
                   
                    
                '   Find and process all assembly drawings found in FolderPath
                        
                        For Each subFolder In folder.SubFolders
                    
                            ProcessFolder subFolder
                    
                        Next
                        
                        For Each AsmFile In folder.Files
                    
                        If InStr(LCase(AsmFile), "sldasm") > 0 Then         'sldasm has to be in lowercase
                            
                            DrawFile = Replace(AsmFile, "SLDASM", "SLDDRW") 'Make the link go from an assembly file to a drawing file  //  SLDASM and SLDDRW need to be in capitals
                            Set swApp = Application.SldWorks
                            swApp.Visible = False
                            
                            'See if log contains assembly
                            LogFile = fso.GetBaseName(DrawFile)
                
                
                            Dim strFilename As String: strFilename = "D:\Spare Part Generator\Log\Processed Assemblies.txt"
                            Dim strTextLine As String
                            Dim iFile As Integer: iFile = FreeFile
                            Open strFilename For Input As #iFile
                            Do Until EOF(1)
                                Line Input #1, strTextLine
                                Dim TempString As String
                                TempString = UCase(Trim(strTextLine))
                                If Left(TempString, 8) <> LogFile Then
                                            'Start of opening drawing and running Macro
                                            Set doc = swApp.OpenDoc6(DrawFile, swDocDRAWING, 2, "", fileerror, filewarning) '(FileName, Type, Options (2 = read only), Configuration, Errors, Warnings)
                                            boolstatus = swApp.RunMacro2("D:\Spare Part Generator\Macro\MARS RDC.swp", "RDC", "main", swRunMacroDefault, False)
                                            
                                            'Writing name of assembly to Log
                                            Set fso = CreateObject("Scripting.FileSystemObject")
                                            Set oFile = fso.OpenTextFile(strFilename, 8, True) '8 = ForAppending
                                            oFile.WriteLine LogFile & " - Date: " & Format(Date, "ddmmmyyyy") & " - Time: " & Format(Time, "HH:MM:SS")
                                            oFile.Close
                                            Set fso = Nothing
                                            Set oFile = Nothing
                                End If
                            Loop
                            Close #iFile
                
                
                        End If
                    
                        Next
                    
                        MsgBox "Finished Processing, Solidworks will exit. Have a nice day."
                    
                    
                '   Shut down Solidworks
                
                
                        'swApp.ExitApp
                        'Set swApp = Nothing
                    
                    
                End Sub
                
                
              • Re: Searching a txt file for a certain variant
                Ivana Kolin

                usage of filenumber is wrong, but I don't think that it causes your error. Are you sure that you are reading correct logfile? Because you are writing to same file when macro processed.

                 

                Dim iFile As Integer: iFile = FreeFile

                Open strFilename For Input As #iFile

                Debug.Print strFilename & " Opened."

                Do Until EOF(iFile)

                    Line Input #iFile, strTextLine

                 

                nederlands is iets makkelijker ook al is het niet mijn moedertaal.

                Wees blij dat het alleen voor de eerste regel werkt, anders ga je macro uitvoeren voor elke regel in de log, die anders is dan je drawing

                LogFile = fso.GetBaseName(DrawFile)
                  
                Dim gevonden As Boolean
                Dim strFilename As String: strFilename = "D:\Spare Part Generator\Log\Processed Assemblies.txt"
                Dim strTextLine As String
                Dim iFile As Integer: iFile = FreeFile
                Open strFilename For Input As #iFile
                Debug.Print strFilename & " Opened."
                Do Until EOF(iFile)
                    Line Input #iFile, strTextLine
                    Dim TempString As String
                    TempString = UCase(Trim(strTextLine))
                  
                    If Left(TempString, 8) = upper(LogFile) Then
                        gevonden = True
                        Exit Do
                    End If
                Loop
                Close #iFile
                
                
                If NOT gevonden Then
                    'Start of opening drawing and running Macro
                    Set doc = swApp.OpenDoc6(DrawFile, swDocDRAWING, 2, "", fileerror, filewarning) '(FileName, Type, Options (2 = read only), Configuration, Errors, Warnings)
                    boolstatus = swApp.RunMacro2("D:\Spare Part Generator\Macro\MARS RDC.swp", "RDC", "main", swRunMacroDefault, False)
                      
                    'Writing name of assembly to Log
                    Set fso = CreateObject("Scripting.FileSystemObject")
                    Set oFile = fso.OpenTextFile("D:\Spare Part Generator\Log\Processed Assemblies.txt", 8, True) '8 = ForAppending
                    oFile.WriteLine LogFile & " - Date: " & Format(Date, "ddmmmyyyy") & " - Time: " & Format(Time, "HH:MM:SS")
                    oFile.Close
                    Set fso = Nothing
                    Set oFile = Nothing
                End If
                
                  • Re: Searching a txt file for a certain variant
                    Robert Voogt
                    LogFile = fso.GetBaseName(DrawFile)
                    
                    
                                Dim strFilename As String: strFilename = "D:\Spare Part Generator\Log\Processed Assemblies.txt"
                                Dim strTextLine As String
                                Dim iFile As Integer: iFile = FreeFile
                                Open strFilename For Input As #iFile
                                Do Until EOF(1)
                                    Line Input #1, strTextLine
                                Loop
                                    If Not InStr(LCase(strTextLine), LogFile) > 0 Then
                                    'Start of opening drawing and running Macro
                                    Set doc = swApp.OpenDoc6(DrawFile, swDocDRAWING, 2, "", fileerror, filewarning) '(FileName, Type, Options (2 = read only), Configuration, Errors, Warnings)
                                    boolstatus = swApp.RunMacro2("D:\Spare Part Generator\Macro\MARS RDC.swp", "RDC", "main", swRunMacroDefault, False)
                                    
                                    'Writing name of assembly to Log
                                    Set fso = CreateObject("Scripting.FileSystemObject")
                                    Set oFile = fso.OpenTextFile(strFilename, 8, True) '8 = ForAppending
                                    oFile.WriteLine LogFile & " - Date: " & Format(Date, "ddmmmyyyy") & " - Time: " & Format(Time, "HH:MM:SS")
                                    oFile.Close
                                    Set fso = Nothing
                                    Set oFile = Nothing
                                    End If
                                Close #iFile
                    

                     

                    I solved it with this

                    • Re: Searching a txt file for a certain variant
                      Robert Voogt

                      upper (Logfile) geeft een error. (sub or function not defined)

                       

                      Zoekt de macro met deze code nog steeds het gehele document naar de waarde van LogFile? De waarde die hij moet zoeken kan overal staan in de processed assemblies txt

                       

                      Heb upper verwijderd, lijkt nu te werken.

                    • Re: Searching a txt file for a certain variant
                      Amen Allah Jlili

                      I was going to answer but I saw Ivana so I stopped...