hexagon logo

FILE/EXISTS strange behaviour

Hello guys, I had encountered strange FILE/EXISTS command behaviour. I want to load some data from text file, if this file exists. Quite simple:

SUBROUTINE/DATUMOVKA_LOADER,
                PARTNAME =  : JMÉNO PROGRAMU,
                PRODDATE =  : DATUMOVKA DÍLU,
                COILBATCH =  : ŠARŽE COILU,
                COILNO =  : ČÍSLO COILU,
                MSG = 0 : VÝSTUPNÍ ZPRÁVA V PŘÍPADĚ NENALEZENÍ SOUBORU S DATUMOVKOU,
                 =
            ASSIGN/DATUMOVKA_FILE="X:\\Datumovky\\"+PARTNAME+".txt"
FILEOK     =FILE/EXISTS,DATUMOVKA_FILE
            IF/FILEOK==0
              ASSIGN/MSG="File does not exist! " + DATUMOVKA_FILE
              GOTO/HANDLER_DATUMOVKA_LOADER
            END_IF/
FPTR       =FILE/OPEN,DATUMOVKA_FILE,READ
            ASSIGN/ENDF=0
            ASSIGN/PRODDATE=-1
            ASSIGN/COILBATCH=-1
            ASSIGN/COILNO=-1
ENDF       =FILE/READLINE,FPTR,{PRODDATE}+";"+{COILBATCH}+";"+{COILNO}
            FILE/CLOSE,FPTR,KEEP
            ASSIGN/HODNOTY=ARRAY(PRODDATE,COILBATCH,COILNO)
            ASSIGN/TESTHODNOT=MIN(HODNOTY)
            IF/TESTHODNOT<0
              ASSIGN/MSG="Improper values loaded, check file " + DATUMOVKA_FILE
            END_IF/
HANDLER_DATUMOVKA_LOADER=LABEL/
            ENDSUB/
DATELOAD   =GROUP/SHOWALLPARAMS=YES
              ASSIGN/PD=0
              ASSIGN/CB=0
              ASSIGN/CN=0
              ASSIGN/MG=0
              ASSIGN/ADD="atest"
CS1          =CALLSUB/DATUMOVKA_LOADER,X:\_CMM_Programy\_Subroutines\subroutines_002.PRG:ADD,PD,CB,CN,MG,,
              IF/MG<>0
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,OVC=NO,
                MG
              END_IF/
              ELSE/
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,OVC=NO,
                PD
                CB
                CN
              END_ELSE/
            ENDGROUP/ID=DATELOAD​


The problem is that FILEOK returns "1" even if the file does not exist and than instead of quitting the routine the program tries to read the data from file which lead to error, because the file is not there obviously Slight smile After hours of trying everything the code started to work after this modification:
FILEOK     =FILE/EXISTS,DATUMOVKA_FILE
            ASSIGN/FILEOK=FILEOK
            IF/FILEOK==0​
.
.
.

Everything else is exactly the same. You know, I can live with that, even if it does not make sense to me. But does anyone have an idea HOW IS THIS F...ING POSSIBLE? AngrySlight smile
  • I've only used the command once so far, but here's my code of it for reference.

    You might try using your file path and name instead of the variable.


    VEXIST1    =FILE/EXISTS,C:\\USERS\\PUBLIC\\DOCUMENTS\\HEXAGON\\PC-DMIS\\2021.2\\CSV_FILES\\PART#\\PART#_INSPECTION_DATA.CSV
                IF/VEXIST1==0
                  COMMENT/OPER,NO,FULL SCREEN=YES,AUTO-CONTINUE=NO,OVC=NO,
                  FILE WRITE ERROR
                  CREATE CSV FILE
                  AND REMEASURE PART
                  CLICK OK TO END PROGRAM
                  GOTO/END_PROGRAM
                END_IF/​
  • The thing is that the code is part of a subroutine which will be called by many routines and for each I'll have to use different file, so variable seems to be the only way. Nevertheless the address itself is evaluated correctly, problem is when you ask about the variable which holds the true/false value.
    FILEOK =FILE/EXISTS,DATUMOVKA_FILE
    IF/FILEOK==0​

    returns 1 even if the file does not exist while
    FILEOK =FILE/EXISTS,DATUMOVKA_FILE
    ASSIGN/FILEOK=FILEOK
    IF/FILEOK==0​​

    works correctly...Confused
  • Maybe under the first circumstance it returns TRUE because the subroutine ran
  • What happens when you try this?
    ASSIGN/DATUMOVKA_FILE="X:\\Datumovky\\"+PARTNAME+".txt"
    ASSIGN/FILEOK=0 <--- new line here! <---<---<---<---<---<---<---
    FILEOK =FILE/EXISTS,DATUMOVKA_FILE
    IF/FILEOK==0
      ASSIGN/MSG="File does not exist! " + DATUMOVKA_FILE
      GOTO/HANDLER_DATUMOVKA_LOADER
    END_IF/​
    


    You shouldn't have to do this but just curious...
  • It works just fine as well.. And suddenly even the first way without any unnecessary assignments works fine as well. I have no idea how it's possible, maybe "2" went to the CPU instead of 0 or 1... Nevertheless thanks for your input guys Slight smile
  • It works just fine as well.. And suddenly even the first way without any unnecessary assignments works fine as well. I have no idea how it's possible, maybe "2" went to the CPU instead of 0 or 1... Nevertheless thanks for your input guys Slight smile


    You're welcome! I first learned the term "shell" a program when working with Pc-Dmis I highly recommend using it. Copy all your code and paste it onto a new program and overwrite the old (once you confirm it works) and that my friend does wonders... sometimes.
  • ​ I love that piece of advice and sadly I'm not surprised by it at all! Wink

    ​ I see you're working with subroutines and the file's location is created from one of the parameters. In my experience, this can be tricky if the string doesn't evaluate to something useful in every possible scenario----including at the very time of writing the code for your subroutine. That's why I always make sure to have suitable default values for my subroutines even if they will never be used later on.
    Another piece of information that might be helpful is that I sometimes had to explicitly cast my parameters to what I wanted them to be. I suspect that your apparently useless line of assigning your FILEOK variable to itself might actually have triggered a type change underneath the hood, but of course that's just a guess.
  • Maybe you just need to initialize the variable first, like does in his example.