hexagon logo

Script to make a change in all programs?

All of our programs are set up to automatically save reports to a specific directory based on part number. IT has decided they need to re-map our entire network which means I need to edit all of our programs to save reports according to the new network mapping scheme. Is it possible to use a script for this task? If so, does anyone have an example of how to do this?
Parents
  • If you can just remap your J: to the location of your new S: drive, that would be the easiest way to go. If your J: drive is a network share that you still need access to, you may need to map it to something else, but then your PC will probably be different from all the others in your company.

    If you want to script a batch conversion, you can paste this into a new VB.NET console application. You'll need to change the constant 'ProgramDir' value (right at the top) to whatever root directory you want to start the conversion in (maybe "J:\Quality\" based on your example.) It would probably be wise to copy a few programs to another location to test this, and then copy all of your programs to another location for doing a full run.

    Imports System.Threading
    Imports System.IO
    
    Module Module1
    
        Const ProgramDir = "J:\TEMP\"
        Const ProgramFileExt = "*.PRG"
        Const ChangeFromPath = "J:\"
        Const ChangeToPath = "S:\"
    
        Private pcdApp As PCDLRN.Application
        Private pcdParts As PCDLRN.PartPrograms
        Private pcdActivePart As PCDLRN.PartProgram
        Private pcdCommands As PCDLRN.Commands
        Private pcdCommand As PCDLRN.Command
    
        Sub Main()
    
            If ConnectPCD() = True Then
                Dim tmpFiles() As String = GetFiles(ProgramDir)
                Try
                    For Each tmpFile As String In tmpFiles
                        If ConvertPrintPath(tmpFile, ChangeFromPath, ChangeToPath) = False Then Exit For
                    Next
                Catch ex as Exception
                    Console.Writeline(ex.Message)
                End Try
                Console.WriteLine("Complete!")
            End If
    
            DisconnectPCD()
    
            Console.ReadLine()
    
        End Sub
    
        Function GetFiles(ByVal RootDir As String) As String()
    
            Return Directory.GetFiles(RootDir, ProgramFileExt, SearchOption.AllDirectories)
    
        End Function
    
        Function ConnectPCD() As Boolean
    
            Try
                pcdApp = GetObject("", "PCDLRN.Application")
                pcdApp.WaitUntilReady(60)
                Thread.Sleep(2000)
                pcdApp.Visible = True
                pcdApp.Maximize()
                pcdParts = pcdApp.PartPrograms
                If pcdParts.Count <> 0 Then
                    Throw New Exception("Please close all open PC-DMIS part programs.")
                End If
                Return True
            Catch ex As Exception
                Console.WriteLine("ConnectPCD : " & ex.Message)
                Return False
                pcdParts = Nothing
                pcdApp = Nothing
                GC.Collect()
            End Try
    
        End Function
    
        Sub DisconnectPCD()
    
            pcdCommand = Nothing
            pcdCommands = Nothing
            pcdActivePart = Nothing
            pcdParts = Nothing
            pcdApp = Nothing
            GC.Collect()
    
        End Sub
    
        Function ConvertPrintPath(ByVal FilePath As String, ByVal ChangeFrom As String, ByVal ChangeTo As String) As Boolean
    
            Console.WriteLine("Converting file: " & FilePath)
    
            Try
                pcdParts.Open(FilePath, "CMM1")
                pcdActivePart = pcdApp.ActivePartProgram
                pcdCommands = pcdActivePart.Commands
                For i As Integer = 0 To pcdCommands.Count
                    pcdCommand = pcdCommands.Item(i)
                    If pcdCommand Is Nothing Then Continue For
                    If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For
                    Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1)
                    If tmpText.Contains(ChangeFrom) Then
                        pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1)
                    End If
                Next
                pcdActivePart.Close()
                Return True
            Catch ex As Exception
                Console.WriteLine("ConvertPrintPath : " & ex.Message)
                Return False
            End Try
    
        End Function
    
    End Module
    
Reply
  • If you can just remap your J: to the location of your new S: drive, that would be the easiest way to go. If your J: drive is a network share that you still need access to, you may need to map it to something else, but then your PC will probably be different from all the others in your company.

    If you want to script a batch conversion, you can paste this into a new VB.NET console application. You'll need to change the constant 'ProgramDir' value (right at the top) to whatever root directory you want to start the conversion in (maybe "J:\Quality\" based on your example.) It would probably be wise to copy a few programs to another location to test this, and then copy all of your programs to another location for doing a full run.

    Imports System.Threading
    Imports System.IO
    
    Module Module1
    
        Const ProgramDir = "J:\TEMP\"
        Const ProgramFileExt = "*.PRG"
        Const ChangeFromPath = "J:\"
        Const ChangeToPath = "S:\"
    
        Private pcdApp As PCDLRN.Application
        Private pcdParts As PCDLRN.PartPrograms
        Private pcdActivePart As PCDLRN.PartProgram
        Private pcdCommands As PCDLRN.Commands
        Private pcdCommand As PCDLRN.Command
    
        Sub Main()
    
            If ConnectPCD() = True Then
                Dim tmpFiles() As String = GetFiles(ProgramDir)
                Try
                    For Each tmpFile As String In tmpFiles
                        If ConvertPrintPath(tmpFile, ChangeFromPath, ChangeToPath) = False Then Exit For
                    Next
                Catch ex as Exception
                    Console.Writeline(ex.Message)
                End Try
                Console.WriteLine("Complete!")
            End If
    
            DisconnectPCD()
    
            Console.ReadLine()
    
        End Sub
    
        Function GetFiles(ByVal RootDir As String) As String()
    
            Return Directory.GetFiles(RootDir, ProgramFileExt, SearchOption.AllDirectories)
    
        End Function
    
        Function ConnectPCD() As Boolean
    
            Try
                pcdApp = GetObject("", "PCDLRN.Application")
                pcdApp.WaitUntilReady(60)
                Thread.Sleep(2000)
                pcdApp.Visible = True
                pcdApp.Maximize()
                pcdParts = pcdApp.PartPrograms
                If pcdParts.Count <> 0 Then
                    Throw New Exception("Please close all open PC-DMIS part programs.")
                End If
                Return True
            Catch ex As Exception
                Console.WriteLine("ConnectPCD : " & ex.Message)
                Return False
                pcdParts = Nothing
                pcdApp = Nothing
                GC.Collect()
            End Try
    
        End Function
    
        Sub DisconnectPCD()
    
            pcdCommand = Nothing
            pcdCommands = Nothing
            pcdActivePart = Nothing
            pcdParts = Nothing
            pcdApp = Nothing
            GC.Collect()
    
        End Sub
    
        Function ConvertPrintPath(ByVal FilePath As String, ByVal ChangeFrom As String, ByVal ChangeTo As String) As Boolean
    
            Console.WriteLine("Converting file: " & FilePath)
    
            Try
                pcdParts.Open(FilePath, "CMM1")
                pcdActivePart = pcdApp.ActivePartProgram
                pcdCommands = pcdActivePart.Commands
                For i As Integer = 0 To pcdCommands.Count
                    pcdCommand = pcdCommands.Item(i)
                    If pcdCommand Is Nothing Then Continue For
                    If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For
                    Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1)
                    If tmpText.Contains(ChangeFrom) Then
                        pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1)
                    End If
                Next
                pcdActivePart.Close()
                Return True
            Catch ex As Exception
                Console.WriteLine("ConvertPrintPath : " & ex.Message)
                Return False
            End Try
    
        End Function
    
    End Module
    
Children
No Data