0

Here is an example (works on playground) of reading and writing a file in Tabris. (this may be a good snippet to aid in understanding of “paths” on iOS/Android/Windows)

If you try to read the file which doesn’t exist a general error is reported back.

How to I test to see if a file exists?

I’ve tried some Nodejs methods that didn’t work.

Thanks

CODE

const {fs, Button, TextView, TextInput, ui} = require('tabris')

const FILENAME = 'hello.txt'
const FILEPATH = fs.filesDir + '/' + FILENAME

console.log(FILEPATH)

let btnReadFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Read File: ' + FILENAME
}).appendTo(ui.contentView)

btnReadFile.on('select', () => {
  fs.readFile(FILEPATH, 'utf-8')
    .then(text => txiFile.text = text)
    .catch(err => console.error(err))
})

let btnWriteFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Write File: ' + FILENAME
}).appendTo(ui.contentView)

let btnRemoveFile = new Button({
  centerX: 0,  top: 'prev() 10', width: 200,
  text: 'Remove File: ' + FILENAME
}).appendTo(ui.contentView)

btnWriteFile.on('select', () => {
  fs.writeFile(FILEPATH, txiFile.text, 'utf-8')
    .then(() => console.log('file written:', FILEPATH))
    .catch(err => console.error(err))
})

btnRemoveFile.on('select', () => {
  txiFile.text = ''
  fs.removeFile(FILEPATH)
    .then(() => console.log('file removed:', FILEPATH))
    .catch(err => console.error(err))
})

let txiFile = new TextInput({
  top: 'prev() 20', left: '20%', right: '20%', height: 100,
  type: 'multiline'
}).appendTo(ui.contentView)

SCREENSHOT screenshot on iOS


Updated code with fs.readDir solution NOT working...

function always returns false - yet inside async function() I see it is working, and filesDir lists file correctly.

const FILENAME = 'helloxxppp.txt'
const FILEPATH = fs.filesDir
const FULLFILEPATH = FILEPATH + '/' + FILENAME

console.log('FILENAME: \n ' + FILENAME)
console.log('FILEPATH: \n ' + FILEPATH)
console.log('FULLFILEPATH \n: ' + FULLFILEPATH)

// this ALWAYS is false
if (fileExist(FILEPATH, FILENAME)) {
  console.log('File NOT exists\n')
} else {
  console.log('File exists\n')
}

async function fileExist (path, file) {
  let files
  try {
    files = await fs.readDir(path)
    console.log(files)
  } catch (err) {
    return false
  }
  return files.indexOf(file) > -1
}
mrmccormack
  • 143
  • 1
  • 10

1 Answers1

1

Async/Await version:

const {fs} = require('tabris')

async function fileExist(path, file) {
  let files
  try {
    files = await fs.readDir(path)
  } catch (err) {
    return false
  }
  return files.indexOf(file) > -1
}

Promise version:

const {fs} = require('tabris')

function fileExist(path, file) {
  return new Promise((resolve, reject) => {
    fs.readDir(path)
      .then(files => resolve(files.indexOf(file) > -1))
      .catch(err => resolve(false)) // Error is ignored intentionally
  })
}

Usage:

1) Using then:

const FILENAME = 'helloxxppp.txt'
const FILEPATH = fs.filesDir

fileExist(FILEPATH, FILENAME).then((exist) => {
  if (exist) {
    console.log('File NOT exists\n')
  } else {
    console.log('File exists\n')
  }
})

2) Using async/await:

async myFunction() {
  // code here
  let exist = await fileExist(FILEPATH, FILENAME)
    if (exist) {
    console.log('File NOT exists\n')
  } else {
    console.log('File exists\n')
  }
}
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • I agree with this answer. Tabris.js does not yet have a method for determining the existence of a file so you'll have to implement it yourself. It's a good candidate for a feature request. – Cookie Guru Jul 17 '18 at 18:33
  • Thanks, I see, get a list of files, and if indexOf > -1, file exist. I'm trying to put that in my example, but function always returns `false` - can you see where I'm going wrong, (works in Playground) – mrmccormack Jul 18 '18 at 13:34
  • I added the function to my code, see my editted POST above "Updated code with fs.readDir solution NOT working..." I must be missing something... – mrmccormack Jul 18 '18 at 13:44
  • I don't have development tools installed. Can you step into function with debugger and check what happens? – Xaqron Jul 18 '18 at 19:56
  • Not sure about "development tools" - my code will run in https://tabrisjs.com/playground - here is complete code as a GIST https://gist.github.com/mrmccormack/fdef86b4a710db08b019ab93f44d5de5 - Hope that helps... – mrmccormack Jul 18 '18 at 22:08
  • `async` functions return promise. use `then` or `await` inside an `async` function. See my updated answer. – Xaqron Jul 18 '18 at 22:56
  • thanks for update... works on iPhone now... but I just learned _Android does not currently support async/await._ So, I must look for another solution... (ref: https://github.com/eclipsesource/tabris-js/issues/1703 ) - for such a simple thing (i.e. does a file exist?) it's sure taking me a long time... appreciate your help... – mrmccormack Jul 19 '18 at 21:13
  • @mrmccormack: Promise version of function included in answer. – Xaqron Jul 19 '18 at 22:12