Home AMX User Forum NetLinx Studio

File read/write/delete problems

Gday all,

I'm reading & writing to files on the local CF card (NI-3000). I seem to be having some difficulties with several of the file commands. These examples are taken straight from the help file.

1. The file_createdir('\CDLIST\TEMP\') function returns -4 (invalid path directory) when trying to create BOTH directories. If I create the '\CDLIST\' directory first, then the subsequent 'TEMP' directory is created okay. The help files indicate that the subdirectories will be created to complete the path...

Okay so the directory now exists. I create a file using file_open('\CDLIST\TEMP\test.txt', 3), which works fine. I then close the file... No dramas.

2. The file_delete('\CDLIST\TEMP\*.*') function returns -5 (disk I/O error). Try as I might, I can not seem to be able to delete the file. I noticed that file_delete() works fine if the target file is not in any subdirectory.

3. The all-dangerous file_removedir('\CDLIST\TEMP\') also has no effect. Another -5 (disk I/O error)

Am I missing something, or is there an issue with the way directories are dealt with? There is plenty of space on the CF. There doesn't appear to be an equivalent command to change the current working directory, to allow me to use relative path names.

I think the help files & NetLinx Programming Language pdf could be a little bit more descriptive in this area. It probably warrants a seperate chapter to the topic.

NLS v2.3, NI-3000 running v2.31.137

Yours,
Roger McLean.

Comments

  • DHawthorneDHawthorne Posts: 4,584
    I don't believe you can delete multiple files, or at least I never got it to work either. I have, instead, used the FILE_DIR function to iterate through a folder and delete files based on what it returns. I don't think (never verified it though) that the NetLinx file handling functions handle wildcard characters (? and *) either ... it has to be a fully qualified filename.
  • annuelloannuello Posts: 294
    Okay, I'll have to take the same approach as you Dave, with obtaining a dir listing then deleting files based on that listing. (Your logging module which you've posted here should be helpful - Thanks!)

    The help files claim that wild cards work (& multi-dir-depth file paths), but it certainly doesn't seem to in my case either. I guess there is a bug either in the file calls or in the associated documentation. :) I don't yet know how to post an official bug report, so I hope someone from AMX reads this. I'm sure they will... In the mean time, I'm off to manually remove all those files which I thought would have been deleted by now.

    Roger McLean
  • LegacyUserLegacyUser Posts: 0
    the all-powerful directory delete does work. i've used it a lot myself. it's effectively a remove directory command.. be sure you don't want any files below it.

    and file delete does usually work fine. i'd say there is a good chance that the file has not been closed.

    check the following..

    after rebooting, without doing any file creation.. can you delete the file you want to get rid of ? it should be easily removed if it is not being held open elsewhere (maybe even try deleting it from FTP).

    then...

    create the file once. then try to create it again, after you believe you've closed it. check the result code.

    as for the full path, i seem to recall netlinx will create the full path when you try to create the file. i haven't revisited that area since i've set it up in my core application and left it to deal with those issues.

    i hope that helps.
  • alexanboalexanbo Posts: 282
    One other thing that can cause problems is the current directory. You need to make sure that the current directory is what you think it is, using the FILE_SETDIR command.

    In the original post if the current directory wasn't root I think both 2 and 3 would happen.

    I've heard too that there are some modules by AMX that weren't rigourous with their file operations and leave the current directory in unexpected states.
  • DHawthorneDHawthorne Posts: 4,584
    That would explain it. I somehow missed that the SET_DIR function even existed. Apparently, the FILE functions don't handle pathing as part of the filename.
  • annuelloannuello Posts: 294
    Okay, I think I'm getting closer to figuring out what works and what doesn't. The following was tested as a stand-alone program with no modules, to ensure that there was nothing else interacting with the files.

    Problem #1. Initially, there are no dirs created. The following test tells me that, contrary to the help file, file_createdir() will NOT create the number of subdirs needed to complete the path. I have to create each dir myself.
    //Doesn't work if '\CDLIST' does not exist.  Returns a -4 (bad file path) error.
    theResult = file_createdir('\CDLIST\TEMP\')
    
    theResult = file_createdir('\CDLIST\')       //Does work.
    theResult = file_createdir('\CDLIST\TEMP\')  //Does work.
    

    Problem #2 The file_removedir() will only work if there are no files or subdirs remaining in the dirpath. This is contrary to the help file, which states that all files & subdirectories contained within the directory will also be deleted. It will only work if there is nothing contained in the specified directory.
    //Returns a -5 (disk I/O) if '\CDLIST\TEMP\' is not completly empty prior
    //to file_removedir() call, otherwise it works okay.
    theResult = file_removedir('\CDLIST\TEMP\')
    

    Problem #3. Wildcards do not seem work, contrary to the help file description of file_delete(). The only way I can see to clear out a directory is to first get the listing, then iterate through the listing deleting each file individually. (I have not yet tried that approach though.)
    theResult = file_setdir('\CDLIST\TEMP\')
    theResult = file_delete('*.*')  //Does not work.  Returns a -5 (disk I/O) error.
    
    theResult = file_setdir('\CDLIST\TEMP\')
    theResult = file_delete('*.txt')  //Does not work.  Returns a -5 (disk I/O) error.
    
    theResult = file_setdir('\CDLIST\TEMP\')
    theResult = file_delete('test.txt')  //Does work, provided file exists of course.
    
    theResult = file_setdir('\')
    theResult = file_delete('\CDLIST\TEMP\test.txt')  //Also works, provided the file exists.
    

    To ensure that my file ('\CDLIST\TEMP\test.txt') was always closed for these tests, I create it as follows:
    button_event[dvIO,1]{
     push:{
      theResult = file_createdir('\CDLIST\')
      theResult = file_createdir('\CDLIST\TEMP\')
      theResult = file_open('\CDLIST\TEMP\test.txt', 3)
     }
     release:{
      theResult = file_close(theResult)
     }
    }
    
    No other reading or writing to the file, just creating it (0 bytes long). theResult is of type SLONG, declared in the DEFINE_VARIABLE section.

    Can someone please confirm (or refute) my observations. I believe that I am using the functions as they were intended, but that they are not performing as they should.

    Oh, and I also missed the file_setdir(). Probably due to the mix of UPPER & lower case in the help file. The rest of the file functions are listed in UPPER case only. Thanks for pointing that one out.

    Yours,
    Roger McLean.
Sign In or Register to comment.