Okay. So this is a tutorial on how to edit the FileSystem. Some of your scripters out there will know how, but some of you don't, which is why i've made this tutorial for you.
I will explain...
- How to check for files and folders
- How to delete files and folders
- How to create files and folders
Useful resources:
- JScript Documentation [Link]
- Scripting Tips Thread [Link]
Believe it or not, it's actually very easy to access and edit the FileSystem.
Okay. First of all, we need to declare an ActiveXObject() to access the FileSystem...
code:
var fileSys = new ActiveXObject("Scripting.FileSystemObject");
Wow! Great! We've accessed the file system! Well, actually, all we've done is declared an object accessible via the variable
fileSys that doesn't do anything. Yet.
This code checks if the folder
WINDOWS exists in the C:\ directory and displays the result to the user through MsgPlus.DisplayToast().
Please be
very careful with this. If you edit this to create or delete files in the WINDOWS folder, you could damage your system!
code:
var fileSys = new ActiveXObject("Scripting.FileSystemObject");
MsgPlus.DisplayToast("FileSystem",fileSys.FolderExists("C:\\WINDOWS\\"));
If you try it and it says "false" then it can mean two things...
- You've entered the code incorrectly
OR
- The WINDOWS directory doesn't exist (It can have different names such as WINNT!)
Syntax for FolderExists:
Object.FolderExists([
string]Path);
Note: You can also have FileExists().
Both FolderExists() and FileExists() are both VERY useful in scripts, you can use them to detect whether a file already exists.
Note:
They also have bugs. They will not always return the proper boolean. Be very careful when you use those functions to check for empty directories, files with the same name as a directory, null-files, etc.
Okay. So, straight onto creating files!
code:
var fileSys = new ActiveXObject("Scripting.FileSystemObject");
var fileSysFile = fileSys.CreateTextFile("C:\\fileSys.txt",true);
fileSysFile.WriteLine("FileSystem!!!");
fileSysFile.Close();
Run this code, then have a look in your C:\ directory. You should see a file called
fileSys.txt. If not then you've entered the code incorrectly. Open it, and you should see "Filesystem!!!" written inside.
That's great. It means we can create files to store information!
Note: Use Close() to close the file, if you don't you might do something wrong. But nothing's gone wrong for me so far.
Syntax for CreateTextFile:
Object.CreateTextFile([
string]Path,[
boolean]overwrite);
Overwrite: True or false - If true, it will overwrite any file already there, if false, then if a file already exists then an error may occur.
Now onto deleting files. Please be careful with this method, in case you make a mistake and destroy half your filesystem.
code:
var fileSys = new ActiveXObject("Scripting.FileSystemObject");
fileSys.DeleteFile("C:\\fileSys.txt",false);
Nice and easy one. If you input it correctly you should no longer see the file
fileSys.txt we created in the section on creating files.
If you've added ReadOnly attributes to
fileSys.txt then it shouldn't be gone.
Syntax for DeleteFile:
Object.DeleteFile([
string]Path,[
boolean]force);
force: If true, files with ReadOnly attributes will be deleted, if false, then if the file is ReadOnly it will be left alone.
If you have anything to add to this tutorial please say so! I know that my tutorial isn't perfect.
I hope this tutorial helps you!