hexagon logo

Read data out of a text file

I have a file named OPER.txt. It has an operation number in it. I would like my PCDMIS program to open this file and read the number inside, and assign the variable “operation” to this value. Could someone post some code that would accomplish this.

Thank you.
  • Already posted in this subforum:

    ' Takes definitions from a textfile and adds these as ASSIGNMENT
    ' commands in the activepartprogram.
    '
    ' The format of the definitions in the textfile:
    '
    ' <VARIABLE_NAME>$<VARIABLE_VALUE>
    '
    ' Ex.
    '
    ' V1$"this is a string"
    ' V2$123.456
    ' V3$CIR1.X
    ' MY_VAR$"my variable"
    '
    '
    ' (c) vpt.se 2009-27-08
    '
    
    sub Main
    
    Dim PCDApp, PCDPartPrograms, PCDPartProgram, PCDCommands, PCDCommand
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Set PCDCommands = PCDPartProgram.Commands
    
    Dim CMDline, CMDvar, CMDval, CMDfile, delimpos, LineCnt
    
    ' the cmds.txt must reside in the activepartprograms folder!
    CMDfile = PCDApp.ActivePartProgram.Path & "cmds.txt"
    ' a linecounter for navigation in the cmds.txt file (if errors occurs)
    LineCnt = 0
    
    Open CMDfile for input as #1
    do while not EOF(1)
    line input #1, CMDline
    LineCnt = LineCnt + 1 ' we have read a line, increment the counter
    delimpos = instr(1, CMDline, "$") ' find the delimiter
    if delimpos then ' delimiter found
      CMDvar = left(CMDline, delimpos - 1) ' get text left of delimiter
      CMDval = right(CMDline, len(CMDline) - delimpos) ' get text right of delimiter
      CMDval = left(CMDval, len(CMDval)-1) ' get rid of the carriage return at the end
        set PCDCommand = PCDCommands.Add(195, True) ' add an assignment command to the program
        retval = PCDCommand.PutText(CMDvar, DEST_EXPR, 0) ' set the variable name
        retval = PCDCommand.PutText(CMDval, SRC_EXPR, 0) ' set the variable value
        PCDCommand.ReDraw
        PCDPartProgram.Refresh   
    else
      MsgBox "Delimiter not found!" & Chr(13) & "Stopped at line " & LineCnt & "." ' what line lacks the delimiter?
      Exit do ' don't loop through the entire file if delimiter not found
    end if
    loop
    close #1
    end sub
  • Why complicate things with a Basic script - this can easily be done with file operations in plain PC-DMIS:

                ASSIGN/OPERATION=0
    FPTR       =FILE/OPEN,"c:\temp\opnr.txt",READ
    V1         =FILE/READLINE,FPTR,{OPERATION}
                FILE/CLOSE,FPTR,KEEP
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
                OPERATION
  • Thank you Anders. That worked perfect.
    Thanks to all for help