none
Easiest way to check if a directory exists? RRS feed

  • Question

  • I need to check if a given file path is a valid directory or not. However when using Folder.Files or Folder.Contents with a valid base directory that leads to a non-existent folder I get a "DataSource.NotFound: File or Folder: We couldn't find the folder '\\blah\blah\blah'" error. Using "try... otherwise" to check for this error does not seem to work, as it always displays the error message and does not go to the otherwise condition. If I put in a nonsense string that is not formatted like a file path, I get a different error that "try... otherwise" does catch correctly.

    Now weirdly if I put Folder.Files inside of a function that attempts to evaluate the contents of a table like Table.IsEmpty() I get the same error message, but "try... otherwise" functions as expected. However the "try" condition is now no longer the default result I want. I was able to get the functional results I want by doing this:

    if try Table.RowCount(Folder.Files(ExportDirectory)) > 0 otherwise false then Folder.Files(ExportDirectory) else #table({"Content"}, {})


    But I feel like I should just be able to do

    try Folder.Files(ExportDirectory) otherwise #table({"Content"}, {})



    Wednesday, September 18, 2019 5:57 PM

Answers

  • Folder.Files and Folder.Contents are like grids that get filled with data.

    In order to properly use a try with them, you'd need to force the evaluation by using Table.Buffer.

    try Table.Buffer(Folder.Files(path)) otherwise Folder.Files(anotherPath)
    I'd also recommend using something like Table.FirstN or Table.LastN to alliviate the whole buffering.
    • Marked as answer by pdelia Thursday, September 19, 2019 4:23 PM
    Wednesday, September 18, 2019 7:00 PM

All replies

  • Folder.Files and Folder.Contents are like grids that get filled with data.

    In order to properly use a try with them, you'd need to force the evaluation by using Table.Buffer.

    try Table.Buffer(Folder.Files(path)) otherwise Folder.Files(anotherPath)
    I'd also recommend using something like Table.FirstN or Table.LastN to alliviate the whole buffering.
    • Marked as answer by pdelia Thursday, September 19, 2019 4:23 PM
    Wednesday, September 18, 2019 7:00 PM
  • Folder.Files and Folder.Contents are like grids that get filled with data.

    In order to properly use a try with them, you'd need to force the evaluation by using Table.Buffer.

    try Table.Buffer(Folder.Files(path)) otherwise Folder.Files(anotherPath)
    I'd also recommend using something like Table.FirstN or Table.LastN to alliviate the whole buffering.
    Ah yeah, Table.Buffer makes sense as it just returns the table after evaluation. What do you mean by using FirstN to "alleviate" the buffering? I'm not sure what the impact of using Table.Buffer is in this situation; I assume it's just loading the information about the files into memory and not the data in the files themselves.
    Wednesday, September 18, 2019 9:14 PM