tutorial 04: Files
Welcome to this tutorial on handling files and paths. Handling of files and paths is fundamental to creating most recipes.
In this tutorial you will learn to:
- Create cross platform paths (win/macos)
- File dialog open/save
- Get file properties
We will be using the base code from the previous tutorial (tutorial 03) as a starting point. If you have added any content to the main function, you may remove it now.
createFile
First, we will create a function called CreateFile()
which will create a new file. Before creating the file, we will create a folder for it using the makeFolder()
function and the folders.temp
variable, which is a path to a magnetron temp folder and a subfolder with the name of the recipe. You can add echo(folders.temp)
to see the path.
Next, we will create a full path to the new file by adding a filename. We will use the global variable gvar.pss
, which gives us the correct separator character for the current platform, to create valid cross-platform paths.
var startpath = folders.temp + gvar.pss + "scribble.txt";
We will then use this path to open a file save dialog, allowing the user to confirm or customize the file name using the saveFileDialog()
function. This function requires a title, start path, and extension(s) as arguments.
var savePath = saveFileDialog("create a file", startpath, "*.txt");
Finally, we will create and add some text to the file using the writeFile()
function.
writeFile(savePath, "testing 123 !");
To use this function, simply call CreateFile() in the main function.
FindFile
Next, we will create a function called FindFile()
which will automatically find files. We will start by using the openFolderDialog()
function to allow the user to select a folder to start searching in. We will use the folders.temp
variable as the start path, since our target is the scribble file we created earlier.
function FindFile()
{
var startfolder = openFolderDialog("select a folder to start searching in", folders.temp);
}
We will then use the findFile()
function to search for the scribble file, using the start path and a wildcard (*) at the end of the file name as arguments.
var r = findFile(folders.temp, "scri*");
This function returns an array of path results, which we will loop through using a for loop.
For each path in the array, we will use the getPathInfo and getFileBytes functions to get extended path information and the file size, respectively. We will then use the echo function to print this information to the console.
for(var i = 0; i < r.length; i++)
{
echo(objectToString(getPathInfo(r[i])));
echo(bytesToDescription(getFileBytes(r[i])));
}
To use this function, simply call FindFile()
in the main function.
DeleteFile
Finally, we will create a function called DeleteFile()
which will allow the user to select and delete a file. We will use the openFileDialog()
function to allow the user to select the scribble file, and then use the fileExists()
function to check if the file exists. If it does, we will use the deleteFile()
function to delete it.
function DeleteFile()
{
if (fileExists(filepath))
deleteFile(filepath);
}
To use this function, simply call DeleteFile()
in the main function.
function main()
{
CreateFile();
FindFile();
DeleteFile();
}
To finish, add all three of these functions to the main function and run the recipe. Note that for security reasons, you may receive debug dialogs when changing files and folders in the development environment. In the app version of magnetron, these dialogs are optional and turned off by default.
Congratulations, you now know how to handle files and paths!
complete example code:
// Create the CreateFile function
function CreateFile() {
// Create a path to the temp folder and a subfolder with the name of the recipe
var folders = {temp: "C:\\Temp\\RecipeName"};
// Use the global variable gvar.pss to get the correct separator character for the current platform
var startpath = folders.temp + gvar.pss + "scribble.txt";
// Open a file save dialog using the saveFileDialog function
var savePath = saveFileDialog("create a file", startpath, "*.txt");
// Write some text to the file using the writeFile function
writeFile(savePath, "testing 123 !");
}
// Create the FindFile function
function FindFile() {
// Use the openFolderDialog function to allow the user to select a folder to start searching in
var startfolder = openFolderDialog("select a folder to start searching in", folders.temp);
// Use the findFile function to search for the scribble file
var r = findFile(folders.temp, "scri*");
// Loop through the array of path results
for(var i = 0; i < r.length; i++) {
// Use the getPathInfo and getFileBytes functions to get extended path information and the file size
echo(objectToString(getPathInfo(r[i])));
echo(bytesToDescription(getFileBytes(r[i])));
}
}
// Create the DeleteFile function
function DeleteFile() {
// Use the openFileDialog function to allow the user to select the scribble file
var filepath = openFileDialog("select a file to delete", "", "*.txt");
// Check if the file exists using the fileExists function
if (fileExists(filepath)) {
// Delete the file using the deleteFile function
deleteFile(filepath);
}
}
// Add all three functions to the main function
function main() {
CreateFile();
FindFile();
DeleteFile();
}