Testing if file and directories exist RRS feed

  • Question

  • I needed quickly a routine testing if a file and his directories exist - and characteristiques staying accessible through an array

    program is saved as RKD630

    It's a quick and "very dirty" program - documenting lines I used to debug are in place. 

    Perhaps some other programs do this kind of test much more efficiently - at least limiting the number of variables. 

    • Edited by Eddy Abts Saturday, March 9, 2019 4:31 PM
    Saturday, March 9, 2019 9:54 AM


  • I'm probably missing something, but to check if a file or folder exists:

    filePath = "D:\Data\Small-Basic\GPXCleaner-165\Route-WP-TR-RT.gpx"

    Thursday, March 14, 2019 10:09 PM

All replies

  • The first part of your program can be shortened if you use the LitDev extension:

    inputfile= "      D:\Data\Small-Basic\GPXCleaner-165\Route-WP-TR-RT.gpx"
    for i=1 to Array.GetItemCount(arr)

    Jan [ WhTurner ] The Netherlands

    Saturday, March 9, 2019 12:33 PM
  • OK - To use the LD extensions I was going to the site of LitDev

    It's not complicated ... but "great stuff"  ( I think not part of the standard Small basic so I have to study the new functions ! )

    I just clicked on the banner « click to download litdev extension »

    and downloaded a zip-file  « Litdev_v1.2 ». Extract it to a directory such as « D:\small-basic\Litdev »

    There you will find  a  readme.txt file 

    Just copy/paste two files « LitDev.dll »  and  « litdev.xml » to the directory   

     C:\Program Files\Microsoft\Small Basic\lib


    C:\Program Files (x86)\Microsoft\Small Basic\lib

    Dépending of the version of Windows you are using - That's all !

    There are some examples - just look in the code of the file  ./graph_samples/

    and see how litdev is setting up the inputbuttons of the graphics window !

    litdev it's really great !!! I was using the same technique but working with arrays isn't evident. 

    Drawing the function is just bringing me back to the seventies  and the HP-Rocky-Mountain-technical Basic with functions as : plotter is, xaxes, yaxes and a for nextloop to draw the function (6 lines !). Few people know the progress in science made by this machines and softwares such as basic and visicalc (one of the first spreadsheetprograms). Small basic and the development is just amazing.

    thank you for all this work

    • Edited by Eddy Abts Saturday, March 16, 2019 8:37 AM
    Saturday, March 9, 2019 4:55 PM
  • Hello Jan,

    I did some testing

    I Couldn't find the syntaxe working in the array "Arr"  generated by the LDText.split  command.

    So I transferred the information in another array "NewArr" to add some other information in a for next loop from 1 to  Array.GetItemCount(arr)     (for fun)



      TextWindow.WriteLine("newarr i=1 "+ NewArr[1]["Name"]+"= "+NewArr[1]["Dir"])
      TextWindow.WriteLine("               Color = " + NewArr[1]["Color"])


    the complete code is in KZX695

    thanks for the information (hartelijk bedankt ?)


    • Edited by Eddy Abts Wednesday, March 13, 2019 11:14 AM
    Wednesday, March 13, 2019 9:06 AM
  • If you have a string "aaa\bbb\ccc\ddd" split ..."\") givres an array:

    arr[1]=aaa , arr[2]=bbb , arr[3]=ccc , arr[4]=ddd

    So the final directory is  arr[1]+"\"+arr[2]+"\"+arr[3]   and the filemame is arr[4]

    to find the last part for the file use Array.GetItemCount(arr)

    Hope this helps.

    Jan [ WhTurner ] The Netherlands

    Wednesday, March 13, 2019 11:37 AM
  • I wanted to see how I would solve your problem so I made a little program. I tested with a path\file in my directory trees. By changing a character in the path I could make all tests for existing and non existing directories. I then inserted your path, which obviously do not exist om my computer. The complete program is given below.

    inputfile= "      D:\Data\Small-Basic\GPXCleaner-165\Route-WP-TR-RT.gpx"
    For i=1 To Array.GetItemCount(arr)-1
      If File.GetFiles(dir)<>"FAILED" Then
        TextWindow.WriteLine(dir+"    OK")
        TextWindow.WriteLine(dir+"   does NOT EXIST")
    If i<99 Then
      If Array.ContainsValue(fils,fil) Then
        TextWindow.WriteLine(inputfile+"  exists")

    Jan [ WhTurner ] The Netherlands

    Wednesday, March 13, 2019 6:36 PM
  • Hello Jan ,

    Emulating the dir command of msdos (cmd.exe in windows)

    So I tried as inputfile in the KZX695 program and in your program (and also some nonexisting directories or files) 

    inputfile= " D:\Data\Small-Basic\GPXCleaner-165\Route-WP-TR-RT.gpx"

    inputfile= " .\Route-WP-TR-RT.gpx"

    inputfile= " Route-WP-TR-RT.gpx"

    The idea was to follow the list of the path's to find out the one who was  FAILING.  (Jan in your programlist you forget to copy the line 2 " inputfile= LDText.trim(inputfile) ")

    I wqs rewriting the KZX695  including the LDT functions you suggested but didn't found time to use the  TextWindow.WriteLine("newarr i=1 "+ NewArr[1]["Name"]+"= "+NewArr[1]["Dir"]) idea.

    I wille try to find some time this weekend

    I couldn't find how to use and the syntaxe of the LDT functions 

    the only list I found was

    Thursday, March 14, 2019 9:39 PM
  • I'm probably missing something, but to check if a file or folder exists:

    filePath = "D:\Data\Small-Basic\GPXCleaner-165\Route-WP-TR-RT.gpx"

    Thursday, March 14, 2019 10:09 PM
  • Yes, you are right. But the TS wanted to know which part of the path fails if the file is not found, so the given solutions indicate which subdirectory is not found.

    Jan [ WhTurner ] The Netherlands

    Friday, March 15, 2019 9:30 AM
  • Yes, you are right. But the TS wanted to know which part of the path fails if the file is not found, so the given solutions indicate which subdirectory is not found.

    Jan [ WhTurner ] The Netherlands

    Makes total sense, thanks.
    Friday, March 15, 2019 6:32 PM
  • OH YES !!!

    I started with the “RKD630” subroutine – In fact I suppressed some lines – The real subroutine was only setting a flag.  If there was a problem the program was “ showing the path and showing where to find the problem” as I lost a lot of time just due to some hidden characters in a directoryname.

    So I tried some testing with the different solutions.

    My working directory is : D:\Data\Small-Basic\GPXCleaner-173

    I was creating two files in this directory : “"   and “Array” with a space !

    Then a subdirectory “Temp “ with one file “configfile.txt”

    And another subdirectory “New Temp” (with a space) and a file “File” (with a space)

    For testing I was using an Array


    file[2]="      D:\Data\Small-Basic\GPXCleaner-173\Array"         

    file[3]="  .\"                                            

    file[4]= "  .\temp\configfile.txt"                                        

    file[5]= "  .\new temp\File"                                     

    file[6]= "      D:\Data\Small-Basic\GPXCleaner_173\new temp\file"


    The reason to post this challenge was mainly  I didn’t understood why an existfile command was not part of small-basic and I was in search how to shorten the subroutine … I didn’t imagine there was a ‘single line’ solution from LitDev (the big challenge in unix).

    So Jan was giving some extremely useful hints  … using some other  LitDev-extensions … but I didn’t found a real tutorial so I got a lot of syntax problems using the link from the litdev site with

    In fact I had to use another link !!!, where I could find a complete list.

    BUT – no work is done without testing

    It’s clear you have first to adapt and create the files, etc.

    First I tried my subroutine (the quick and dirty program)  to be imported with “PBG244

    - It runs correctly giving an error for file[7] on the GPXCleaner_173 directory because there is an underscore in stead of the hyphen.

    On file[8] the program couldn’t isolate the directory and file because there is no backslash.


    Next I tried the program from WH-Turner to be imported with “FSN021”.

    You have probably to uncomment line 25 and 37 with the File.GetFiles(dir) instruction. It’s running fine for file[1] but is going in trouble for the other files with the leading blanks 

    So I was trying to include the LDText.Trim function in line 20 by removing the comment apostrophe from  ‘inputfile= LDText.trim(inputfile) but it's clear I got problems with the spaces in the filenames - but it's still giving problems with the .\ pointing to the current program directory. 


    Last program to test was the single liner from litdev.  To be imported as “JVR542

    You have probably to uncomment line 21 -   TextWindow.WriteLine(LDFile.Exists(filePath))

    There are some unexpected problems with  home directories   .\ and leading spaces,  but spaces inside directorynames, etc seems to be  interpreted correctly. There is no test to find back the problem in a failing directorychain.

    So I need to do some more tests with the LDFile.Exist command such the ones on the screen.

    The LD-functions will  be extremely useful when rewriting the program and the LDFile.Exists(filePath) function a quick way to test a "complete" filepath as it seems you can use it also with a File.


    Conclusion : The program RKD630 is  using the standard small-basic functions but the powerful  LD functions opens a new world and other programming techniques. Rewriting the program will be a topic for the next weeks.

    Cleaning a cupboard in the classroom I found an old camera, completely outdated. Connected with an USB-converter  and another single liner-command “webcam = LDWebCam.Start(320,240)”  - I got the image on the blackboard - another surprise with LitDev !

    PS : Hello Jan and litdev - I just finished a "dry testrun" - Tomorrow I will add some djingles and bells and finalise the program - but I learned a lot with your help, having the same programming-feeling and satisfaction  of  50 years ago. The test program to import FZK933   (you have to uncomment some lines)

    • Edited by Eddy Abts Sunday, March 17, 2019 8:14 AM
    Friday, March 15, 2019 8:41 PM