Shoutbox

Tutorial: How to read/write using the FileSystem! :) - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Tutorial: How to read/write using the FileSystem! :) (/showthread.php?tid=79504)

Tutorial: How to read/write using the FileSystem! :) by Toneo on 11-30-2007 at 09:15 PM

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! :D
RE: Tutorial: How to edit the FileSystem! :) by Deco on 11-30-2007 at 09:37 PM

Nice work!


RE: Tutorial: How to edit the FileSystem! :) by CookieRevised on 12-01-2007 at 12:30 AM

Excellent to the point tutorial (y)...

Some pointers though:

quote:
Originally posted by Toneo
This code checks if the folder WINDOWS exists in the C:\WINDOWS\ Directory...
The code checks if the folder WINDOWS exists in the C:\ directory/folder/map/whatever.

quote:
Originally posted by Toneo
If you try it and it says "false" then something must be wrong because Windows requires the WINDOWS folder to function.
Actually, the name can be anything. It is not always "Windows" (eg: "WinNT", "Win", "WinXP", etc). So, if it returned false, it only means the user has another name for his main Windows folder.

quote:
Originally posted by Toneo
Both FolderExists() and FileExists() are both VERY useful in scripts, you can use them to detect whether a file already exists.
Although true, they also have bugs. They will not always return the proper boolean. Be very carefull when you use those functions to check for empty directories, files with the same name as a directory, nul-files, etc.... Somewhere on the forums here there is a more detailed post about it. You're better of using the Windows APIs directly (though, this goes beyond the extent of your otherwise excellent tutorial).

PS: also add a link to the JScript documentation where all this is also explained with examples for each function.
;)
RE: Tutorial: How to edit the FileSystem! :) by Toneo on 12-01-2007 at 09:21 AM

Okay. Thanks for the pointers, i'm glad you like the tutorial.


RE: Tutorial: How to edit the FileSystem! :) by vikke on 12-01-2007 at 02:52 PM

I think the phrase "How to edit the File System" sounds pretty wierd.  The File System type (on Windows NT and above) is NTFS (NT File System), you're not changing it, you're reading and writing from it.
A good title would be "How to use the FileSystemObject to Read/Write to the harddrive.".

Good tutorial otherwise! ;)


RE: Tutorial: How to read/write using the FileSystem! :) by absorbation on 12-01-2007 at 04:52 PM

Great tutorial. I suggest you add a link in the scripting tips thread :).


RE: Tutorial: How to read/write using the FileSystem! :) by Toneo on 12-01-2007 at 09:11 PM

Thanks. I've changed it from how to "edit the filesystem" to how to "read/write using the filesystem" which makes much more sense, and i've also added the link to the scripting tips thread.


RE: Tutorial: How to read/write using the FileSystem! :) by Crazed on 12-01-2007 at 10:16 PM

Nice tutorial. I'll find this comes in handy. :)


RE: Tutorial: How to read/write using the FileSystem! :) by vikke on 12-02-2007 at 10:56 AM

Also, note: To get the path to the Windows folder, no matter what it's called, call the GetSystemWindowsDirectory  API using Interop.Call().