Skip to content

Instantly share code, notes, and snippets.

@mrmccormack
Created July 18, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmccormack/fdef86b4a710db08b019ab93f44d5de5 to your computer and use it in GitHub Desktop.
Save mrmccormack/fdef86b4a710db08b019ab93f44d5de5 to your computer and use it in GitHub Desktop.
Tabrisjs - test if file exists
const {fs, Button, TextView, TextInput, ui} = require('tabris')
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
}
let btnReadFile = new Button({
centerX: 0, top: 'prev() 10', width: 200,
text: 'Read File: ' + FILENAME
}).appendTo(ui.contentView)
btnReadFile.on('select', () => {
fs.readFile(FULLFILEPATH, '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(FULLFILEPATH, txiFile.text, 'utf-8')
.then(() => console.log('file written:', FULLFILEPATH))
.catch(err => console.error(err))
})
btnRemoveFile.on('select', () => {
txiFile.text = ''
fs.removeFile(FULLFILEPATH)
.then(() => console.log('file removed:', FULLFILEPATH))
.catch(err => console.error(err))
})
let txiFile = new TextInput({
top: 'prev() 20', left: '20%', right: '20%', height: 100,
type: 'multiline'
}).appendTo(ui.contentView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment