FileOps Javascript Module

Hey guys, I put together a little .uno file to allow the use of some System.IO functions in Javascript. It’s kind of like the FuseJS/Storage API but with a few extra functions. You can download it here, hopefully some people will find it useful.

Documentation

How to use

  • 1: Download the FileOps.uno file and place it in the root directory of your Fuse project (the one that contains your .unoproj file).
  • 2: Declare it somewhere in your Main .ux file like so:
<FileOps ux:Global='FileOps' />

  • 3: Reference it in your JS:
var FileOps = require('FileOps');

Available Functions

FileOps.FileWrite("path/to/file.txt", "text")

  • Filepath is a string which references the file to be made/written to. Can just be a filename if the file is in the current working directory.
  • Text is a string which will be written to the file.

FileOps.FileRead("file.txt")

  • Returns a string containing the contents of the specified file.

FileOps.GetUserDataDirectory()

  • Returns a string containing the path of the User Data Directory.
  • On Android this is the app’s data folder: /data/data/com.AppName/files
  • On Windows this is the Local App Data folder: C:\Users\Username\AppData\Local
  • I don’t know what it is on Mac/iOS, I don’t have a device to test it on.

FileOps.GetWorkingDirectory()

  • Returns a string containing the path of the current working directory.
  • By default on Android, this is the root of your phone so just: /
  • By default on Windows, this is the folder containing the SublimeText.exe file, in my case: D:\Program Files\Sublime Text 3

FileOps.ChangeDirectory("path")

  • Takes one argument; the path to which you want to change the current working directory (as a string)
  • Works like the commonly used cd command

FileOps.MakeDirectory("foldername")

  • Makes a new folder in the current working directory with foldername as its name
  • Takes one argument; foldername, which should be a string

FileOps.FileExists("path/to/file.txt")

  • Checks if the specified file exists
  • Returns the result as a boolean; either true or false
  • Takes one argument as a string; the path to a file. Can just be a filename if the file is in the current working directory.

FileOps.DirectoryExists("dirname")

  • Works the same as FileOps.FileExists, but for Directories instead

FileOps.DeleteFile("path/to/file.txt")

  • Deletes the specified file
  • Takes one argument as a string; the path to a file. Can just be a filename if the file is in the current working directory.
  • Returns true on successful deletion.
  • Returns false and logs to the console if the specified file does not exist.

FileOps.DeleteDirectory("dirname", recursive)

  • Deletes the specified directory
  • Takes two arguments; the directory name and recursive
  • recursive should be a string containing the values either "true" or "false". Take care not to specify it as a boolean. When true it deletes all directories, subdirectories and files within the specified directory.
  • Returns true on successful deletion.
  • Returns false and logs to the console if the specified directory does not exist.

FileOps.RenameFile("oldName", "newName", overwrite)

  • Renames a file oldName to newName
  • overwrite should be a string containing the values either "true" or "false". Take care not to specify it as a boolean. Defaults to false, so you don’t have to specify it unless you want it to be true.
  • When true, if newName already exists, it overwrites it when renaming and returns a boolean true on successful rename
  • When false, if newName already exists, it returns a boolean false and logs to the console. If newName doesn’t exist, it just returns a boolean true
  • If oldName doesn’t exist, returns boolean false and logs to the console

Very cool!!

This was a huge help, thanks a lot.

Cool !

Is it possible on ios and/or android to access the whole filesytem ? E.g. the photo libary ?

Hi there, I’ve followed your instructions on how to use it, but I get the following error while compiling my project for local preview:

C:\Dvp\App\tw\FileOps.uno(9,24): E3114: There is no identifier named 'NativeModu le' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(26,37): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(33,36): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(40,32): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(58,30): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(72,26): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(87,25): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(100,27): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(124,32): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(148,27): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(168,32): E3114: There is no identifier named 'Context' accessible in this scope. C:\Dvp\App\tw\FileOps.uno(203,27): E3114: There is no identifier named 'Context' accessible in this scope. Build ended Exception of type 'Outracks.CLI.ExitWithError' was thrown. Press any key to exit.

Have I missed anything?