Summary of javascript file operations

Source: Internet
Author: User

You can create a file on the visitor's hard disk through a browser, because I started to try it. I believe you can COPY the following code to an HTML file and run it again!
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Fso. DeleteFile ("c: \ autoexec. bat", true); // please note! Change autoexec. bat to another file name in your drive C. You can do it without changing it! Back up autoexec. bat first!
-->
</Script>

Did you find that the Autoexec. bat file on drive C is missing? In fact, when the file is running, IE will remind you that the ActiveX control you are currently using is not safe and ask you if you are running, but you are as eager as you are to try out the effect, therefore, you will not hesitate to press [OK]... in fact, this is implemented using FileSystemObject. To learn more about the usage and examples, click here to download the JScript Chinese instruction document or buy a book. <learn more: javaScript development and examples> You can also take a look at examples of carefree script sorting for everyone to learn. let's take a look at what attributes and functions are available. Some functions will be used as small examples later.
Method or attribute description
BuildPath ()
Generate a file path
Copy codeThe Code is as follows:
Copy file ()
CopyFolder () Copy directory
CreateFolder () create a new directory
CreateTextFile () to generate a file
DeleteFile () deletes an object.
DeleteFolder () deletes a directory
DriveExists () checks whether the drive letter exists
Drives returns a collection of drive letters.
FileExists () checks whether a file exists
FolderExists checks whether a directory exists
GetAbsolutePathName () gets the absolute path of a file
GetBaseName () Get the file name
GetDrive () gets the drive letter name
GetDriveName () gets the drive letter name
GetExtensionName () gets the file suffix
GetFile () generate a file object
GetFileName () Get the file name
GetFolder () to get the directory object
GetParentFolderName: Get the parent directory name of the file or directory
GetSpecialFolder () Get the special directory name
GetTempName () generates a temporary file object
MoveFile () Move a file
MoveFolder () Move directory
OpenTextFile ()

Open a file stream instance description BuildPath (path, file name) // This method will add a file to the given path, and automatically add the delimiter
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var newpath = fso. BuildPath ("c: \ tmp", "51js.txt"); // generate the path of c: \ tmp \ 51js.txt
Alert (newpath );
-->
</SCRIPT>

CopyFile (source file, target file, overwrite) // copy the source file to the target file. When the overwrite value is true, if the target file exists, the file will be overwritten.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var newpath = fso. CopyFile ("c: \ autoexec. bat", "d: \ autoexec. bak ");
-->
</SCRIPT>

CopyFolder (Object directory, target directory, overwrite) // copy the object directory to the target directory. If it is set to true, the file will be overwritten if the target directory exists.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Fso. CopyFolder ("c :\\ WINDOWS \ Desktop", "d :\\"); // copy the Desktop directory of drive c to the root directory of drive d.
-->
</SCRIPT>

CreateFolder (directory name) // create a new directory
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var newFolderName = fso. CreateFolder ("c: \ 51JS"); // create a 51JS directory on drive c.
-->
</SCRIPT>

CreateTextFile (file name, overwrite) // create a new file. If the file already exists, set the overwrite value to true.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var newFileObject = fso. CreateTextFile ("c: \ autoexec51JS. bat", true); // The Script creates a file named autoexec51JS. bat on drive c.
-->
</SCRIPT>

DeleteFile (file name, read-only ?) // Delete an object. If the object attribute is read-only, set the read-only value to true.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject"); // to ensure security, I first back up autoexec. bat to your D disk.
Var newpath = fso. CopyFile ("c: \ autoexec. bat", "d: \ autoexec. bat"); // Delete the autoexec. bat file of drive c.
Fso. DeleteFile ("c: \ autoexec. bat", true );
-->
</SCRIPT>

DeleteFolder (file name, read-only ?) // Delete a directory. If the Directory attribute is read-only, set the read-only value to true.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Fso. copyFolder ("c :\\ WINDOWS \ Desktop", "d :\\"); // to ensure security, copy the Desktop directory of your drive c to the root directory of your drive d.
Fso. deleteFolder ("c: \ WINDOWS \ Desktop", true); // delete your Desktop directory. However, because desktop is a system item, you cannot delete it all, but .........
-->
</SCRIPT>

DriveExists (drive letter) // check whether a disk exists. If it exists, it returns true. If it does not exist, it returns .......
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
HasDriveD = fso. DriveExists ("d"); // check whether the system has a d Disk
HasDriveZ = fso. DriveExists ("z"); // check whether the system has a Z Disk
If (hasDriveD) alert ("your system has a ddisk ");
If (! HasDriveZ) alert ("your system has no Z disk ");
-->
</SCRIPT>

FileExists (File Name) // check whether a file exists. If it exists, it returns true. If it does not exist, it returns .......
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
FileName = fso. FileExists ("c: \ autoexec. bat ");
If (fileName) alert ("You have an autoexec. bat file in drive C. Press OK and the file will be deleted! "); // Joke :)
-->
</SCRIPT>

FolderExists (directory name) // check whether a directory exists. If it exists, it returns true. If it does not exist, it returns .......
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
FolderName = fso. FolderExists ("c: \ WINDOWS \ Fonts ");
If (folderName) alert ("after you press OK, the font of the system will be deleted! "); // Joke :)
-->
</SCRIPT>

GetAbsolutePathName (file object) // returns the absolute path of the file object in the system.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
PathName = fso. GetAbsolutePathName ("c: \ autoexec. bat ");
Alert (pathName );
-->
</SCRIPT>

GetBaseName (file object) // returns the file name of the object.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
BaseName = fso. GetBaseName ("c: \ autoexec. bat"); // get the autoexec. bat file name autoexec
Alert (baseName );
-->
</SCRIPT>

GetExtensionName (file object) // file suffix
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
ExName = fso. GetExtensionName ("c: \ autoexec. bat"); // obtain the suffix bat
Alert (exName );
-->
</SCRIPT>

GetParentFolderName (file object) // get the parent directory name
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
ParentName = fso. GetParentFolderName ("c: \ autoexec. bat"); // obtain the parent directory c of autoexec. bat.
Alert (parentName );
-->
</SCRIPT>

GetSpecialFolder (directory code) // obtain the path of some special directories in the system. The directory code has three directories: 0: the directory where the Window is installed; 1: The system file directory; 2: the temporary file directory.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
TmpFolder = fso. GetSpecialFolder (2); // obtain the path of the temporary system file directory. For example, if my path is C: \ windows \ temp
Alert (tmpFolder );
-->
</SCRIPT>

GetTempName () // generate a random temporary file object. The random number is followed by a random number using rad, as if some software will generate *. tmp during installation.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
TmpName = fso. GetTempName (); // I generated radDB70E. tmp during the test.
Alert (tmpName );
-->
</SCRIPT>

MoveFile (source file, target file) // move the source file to the location of the target file
<Script language = "JavaScript">
<! --
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var newpath = fso. MoveFile ("c: \ autoexec. bat", "d: \ autoexec. bat"); // move the autoexec. bat file of drive c to drive d.
-->
</SCRIPT> To Be Continue! There are still a few examples of attributes that are not written, so they will be available later. Do you think that every time you will ask if you are in trouble? Or..., want to know how to run without asking? (Do not use scripts to damage other people's systems !)
Create shortcuts using JavaScript to operate the File System
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> create shortcuts with JavaScript </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
</HEAD>
<Script language = "javascript">
Function createLink (){
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var shell = new ActiveXObject ("WScript. Shell ");
Var tagFolder = "c: \ link ";
If (! Fso. FolderExists (tagFolder ))
{
Fso. CreateFolder (tagFolder );
Alert ("Create success! ");
}
If (! Fso. FileExists (tagFolder + "\ eip. lnk "))
{
Var link = shell. CreateShortcut (tagFolder + "\ eip. lnk ");
Link. Description = "opening a program shortcut ";
Link. TargetPath = "C: \ Program Files \ FlashFXP \ flashfxp.exe ";
Link. WindowStyle = 3;
Link. WorkingDirectory = "C: \ Program Files \ FlashFXP ";
Link. Save ();
}
}
</Script>
<BODY>
<Input type = "button" value = "click me" onclick = "createLink ();"/>
</BODY>
</HTML>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.