What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Tutorial: How to read/write using the FileSystem! :)

Tutorial: How to read/write using the FileSystem! :)
Author: Message:
Toneo
Junior Member
**

Avatar
Epic.

Posts: 35
Reputation: 2
28 / Male / Flag
Joined: Jul 2007
O.P. Grin  Tutorial: How to read/write using the FileSystem! :)
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

This post was edited on 12-01-2007 at 09:10 PM by Toneo.
[Image: signature-user=291&back=4&clr=12,102,237&size=80.png]
11-30-2007 09:15 PM
Profile E-Mail PM Find Quote Report
Deco
Full Member
***


Posts: 188
Reputation: 4
41 / Male / Flag
Joined: Aug 2006
RE: Tutorial: How to edit the FileSystem! :)
Nice work!
[Image: signature-user=31&back=4&clr=106,141,166&size=100.png]
11-30-2007 09:37 PM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Tutorial: How to edit the FileSystem! :)
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.
;)

This post was edited on 12-01-2007 at 12:32 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
12-01-2007 12:30 AM
Profile PM Find Quote Report
Toneo
Junior Member
**

Avatar
Epic.

Posts: 35
Reputation: 2
28 / Male / Flag
Joined: Jul 2007
O.P. RE: Tutorial: How to edit the FileSystem! :)
Okay. Thanks for the pointers, i'm glad you like the tutorial.
[Image: signature-user=291&back=4&clr=12,102,237&size=80.png]
12-01-2007 09:21 AM
Profile E-Mail PM Find Quote Report
vikke
Senior Member
****

Avatar

Posts: 900
Reputation: 28
31 / Male / Flag
Joined: May 2006
RE: Tutorial: How to edit the FileSystem! :)
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! ;)
12-01-2007 02:52 PM
Profile E-Mail PM Find Quote Report
absorbation
Elite Member
*****

Avatar

Posts: 3636
Reputation: 81
– / Male / Flag
Joined: Feb 2005
RE: Tutorial: How to read/write using the FileSystem! :)
Great tutorial. I suggest you add a link in the scripting tips thread :).
12-01-2007 04:52 PM
Profile PM Find Quote Report
Toneo
Junior Member
**

Avatar
Epic.

Posts: 35
Reputation: 2
28 / Male / Flag
Joined: Jul 2007
O.P. RE: Tutorial: How to read/write using the FileSystem! :)
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.
[Image: signature-user=291&back=4&clr=12,102,237&size=80.png]
12-01-2007 09:11 PM
Profile E-Mail PM Find Quote Report
Crazed
New Member
*

Avatar

Posts: 12
33 / Male / Flag
Joined: Nov 2007
RE: Tutorial: How to read/write using the FileSystem! :)
Nice tutorial. I'll find this comes in handy. :)
12-01-2007 10:16 PM
Profile PM Web Find Quote Report
vikke
Senior Member
****

Avatar

Posts: 900
Reputation: 28
31 / Male / Flag
Joined: May 2006
RE: Tutorial: How to read/write using the FileSystem! :)
Also, note: To get the path to the Windows folder, no matter what it's called, call the GetSystemWindowsDirectory  API using Interop.Call().
12-02-2007 10:56 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On