# FileOps Javascript Module

**URL:** <https://forums.fusetools.com/t/fileops-javascript-module/4727>\
**Category:** Show and Tell\
**Created:** [October 19, 2015, 6:35am UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727 "2015-10-19T06:35:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [October 19, 2015, 6:35am UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727/1 "2015-10-19T06:35:17Z")

</div>

Hey guys, I put together a little .uno file to allow the use of some [System.IO](http://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](http://jmp.sh/jMVxhxN), 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:

```auto
<FileOps ux:Global='FileOps' />

```

- 3: Reference it in your JS:

```auto
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

---

<div class="post-metadata">

**Author:** ![Kristian\_Hasselknipp](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/kristian_hasselknipp/32/549_2.png) [@Kristian\_Hasselknipp](https://forums.fusetools.com/u/Kristian_Hasselknipp)\
**Post date:** [October 19, 2015, 1:59pm UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727/2 "2015-10-19T13:59:09Z")

</div>

Very cool!!

---

<div class="post-metadata">

**Author:** ![Fahad](https://avatars.discourse-cdn.com/v4/letter/f/ecccb3/32.png) [@Fahad](https://forums.fusetools.com/u/Fahad)\
**Post date:** [December 30, 2015, 11:05pm UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727/3 "2015-12-30T23:05:36Z")

</div>

This was a huge help, thanks a lot.

---

<div class="post-metadata">

**Author:** ![ud1](https://avatars.discourse-cdn.com/v4/letter/u/f07891/32.png) [@ud1](https://forums.fusetools.com/u/ud1)\
**Post date:** [January 21, 2016, 10:14pm UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727/4 "2016-01-21T22:14:32Z")

</div>

Cool !

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

---

<div class="post-metadata">

**Author:** ![Bertrand](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/bertrand/32/1035_2.png) [@Bertrand](https://forums.fusetools.com/u/Bertrand)\
**Post date:** [February 8, 2016, 2:49pm UTC](https://forums.fusetools.com/t/fileops-javascript-module/4727/5 "2016-02-08T14:49:26Z")

</div>

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?
