List of .txt (or mp3, etc) files stored locally

Hey guys,
Been playing around with the read/write files and have them working great. Thank you!

One thing that I'd like to do is get a list of all the txt files stored in file manager and be able to select them using an input statement...

ie. input "theTXTs", "enum", title: "All txt files stored locally", options: theTXTfiles, multiple:true ... etc

Was thinking the fileExists code would be the best way to do it but it doesn't seem that wildcards can be used in the file name (ie. *.txt) Was hoping that it would scroll through the files and be able to save the filename to a list.

Any ideas?

Thanks

File Exists Code
@Field static String fName = "*.txt"

Boolean fileExists(fName){
    if(logEnable) log.debug "Checking to see if File Exists - ${fName}"
    uri = "http://${location.hub.localIP}:8080/local/${fName}";
    def params = [
        uri: uri
    ]
    try {
        httpGet(params) { resp ->
            if (resp != null){
                if(logEnable) log.debug "File Exists! ${fName}"
               //  Add it to a file...
            } else {
                // do nothing
            }
        }
    } catch (e) {
        log.error e
    }
}

You could change the uri to:

http://${location.hub.localIP}:8080/hub/fileManager/json

Then iterate the returned json to filter based on the extension (or really any matching criteria you want)

3 Likes

Thank you very much!

1 Like

In case anyone else who might need something like this... Could be used for txt, mp3, etc.

section() {
    getFileList()
    input "fileToUse", "enum", title: "List of Files", options: fileList, multiple:false, submitOnChange:true
    paragraph "File selected: $fileToUse"
}
Boolean getFileList(){
    if(logEnable) log.debug "Getting list of files"
    uri = "http://${location.hub.localIP}:8080/hub/fileManager/json";
    def params = [
        uri: uri
    ]
    try {
        fileList = []
        httpGet(params) { resp ->
            if (resp != null){
                if(logEnable) log.debug "Found the files"
                def json = resp.data
                for (rec in json.files) {
                    fileType = rec.name[-3..-1]
                    if(fileType == "txt") {
                        fileList << rec.name
                    }
                }
            } else {
                //
            }
        }
        return fileList
    } catch (e) {
        log.error e
    }
}
3 Likes