JavaScript operation file Implementation method summary _javascript Tips

Source: Internet
Author: User
Tags parent directory
You can create a file on the visitor's hard disk by using the browser, because I started to try it really, I do not believe you to copy the following code into an HTML file and then run it!
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Fso. DeleteFile ("C:\\autoexec.bat", true); Attention, please! Change the Autoexec.bat to your other file name in C disk, you can not change it! Back up the Autoexec.bat first!
-->
</script>

Did you find your C-disk Autoexec.bat file missing? Oh actually when the file is running IE will remind you that the current use of ActiveX control is unsafe, ask you whether to run, but because you and I like the urgent want to try how the effect, so you will not hesitate to press [OK] .... In fact, this is the use of FileSystemObject to achieve, to learn more detailed usage and examples, click here to download the Chinese documentation of JScript or buy a << in-depth study: JavaScript development and Instance >> You can also look at the case of worry-free scripting, so that you learn to learn. Let's take a look at what attributes and functions are available, followed by a few examples of some of the functions
Method or attribute Description
BuildPath ()
Generate a file path
Copy Code code as follows:

CopyFile () Copy files
CopyFolder () Copy directory
CreateFolder () Create a new directory
CreateTextFile () generates a file
DeleteFile () Deletes a file
DeleteFolder () Deletes a directory
Driveexists () Check whether the letter exists
Drives a collection of returned disk characters
FileExists () Verify that the file exists
FolderExists Check if a directory exists
Getabsolutepathname () Gets the absolute path of a file
Getbasename () Get filename
Getdrive () Get the letter name
GetDriveName () Get the letter name
Getextensionname () to obtain the suffix of the file
GetFile () Generating File objects
GetFileName () Get filename
GetFolder () Get directory Object
Getparentfoldername gets the parent directory name of the file or directory
GetSpecialFolder () Get a special directory name
GetTempName () generates a temporary file object
MoveFile () Moving files
MoveFolder () Mobile Directory
OpenTextFile ()

Open a File Flow instance description BuildPath (path, filename)//This method adds a file to the given path and automatically adds a delimiter
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var NewPath = fso. BuildPath ("C:\\tmp", "51js.txt"); Path to generate C:\tmp\51js.txt
alert (NewPath);
-->
</SCRIPT>

CopyFile (source file, target file, overwrite)//copy source file to target file, when the overwrite value is true, if the target file exists will overwrite the file
Copy Code code 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 object directory to target directory, when overridden to true, if target directory exists will overwrite file
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Fso. CopyFolder ("C:\\windows\\desktop", "d:\\"); Copy the Desktop directory of C disk to the root of D disk
-->
</SCRIPT>

CreateFolder (directory name)//Create a new directory
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var newfoldername = fso. CreateFolder ("C:\\51js"); Create a 51JS directory on the C disk
-->
</SCRIPT>

CreateTextFile (filename, overwrite)//Create a new file, if this file already exists, you need to set the override value to True
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var newfileobject = fso. CreateTextFile ("C:\\autoexec51js.bat", true); The script will create a file called Autoexec51js.bat on the C disk
-->
</SCRIPT>

DeleteFile (filename, read-only?) //Delete a file, if the file's properties are read-only, you need to set the read-only value to True
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject"); To be safe, I'm going to back up the Autoexec.bat you want to delete to your D disk.
var NewPath = fso. CopyFile ("C:\\autoexec.bat", "D:\\autoexec.bat"); Remove the Autoexec.bat file from the C-disk
Fso. DeleteFile ("C:\\autoexec.bat", true);
-->
</SCRIPT>

DeleteFolder (filename, read-only?) //Delete a directory, if the directory's properties are read-only, you need to set the read-only value to True
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Fso. CopyFolder ("C:\\windows\\desktop", "d:\\"); For security, I'm going to copy your C-Disk desktop directory to the root of your D disk.
Fso. DeleteFolder ("C:\\windows\\desktop", true); Remove your desktop directory, but because desktop is something of the system, it can't be deleted, but ...
-->
</SCRIPT>

Driveexists (letter)//check whether a disk exists, if the existence of the return will be true, do not exist on the back ....
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
hasdrived = fso. Driveexists ("D"); Check if the system has D disk presence
Hasdrivez = fso. Driveexists ("Z"); Check if the system has a Z disk present
if (hasdrived) alert ("There is a D disk in your system");
if (!hasdrivez) alert ("There is no Z-disk in your system");
-->
</SCRIPT>

FileExists (filename)//Check whether a file exists, if there is a return to true, does not exist on the return ....
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
FileName = fso. FileExists ("C:\\autoexec.bat");
if (fileName) alert ("You have Autoexec.bat file in C disk, press OK after this file will be deleted!") "); A joke:
-->
</SCRIPT>

FolderExists (directory name)//check whether a directory exists, if there is a return to true, does not exist to return ....
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
FolderName = fso. FolderExists ("c:\\windows\\fonts");
if (folderName) alert ("Press OK after the system's font will be deleted!") "); A joke:
-->
</SCRIPT>

Getabsolutepathname (File object)//returns the absolute path of the file object in the system
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
PathName = fso. Getabsolutepathname ("C:\\autoexec.bat");
alert (pathName);
-->
</SCRIPT>

Getbasename (File object)//return filename of File object
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
BaseName = fso. Getbasename ("C:\\autoexec.bat"); Gets the filename of the Autoexec.bat AutoExec
alert (baseName);
-->
</SCRIPT>

Getextensionname (File object)//file suffix
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Exname = fso. Getextensionname ("C:\\autoexec.bat"); Get Autoexec.bat suffix Bat
alert (exname);
-->
</SCRIPT>

Getparentfoldername (File object)//Get Parent directory Name
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
ParentName = fso. Getparentfoldername ("C:\\autoexec.bat"); Get Autoexec.bat's parent directory C-Disk
alert (ParentName);
-->
</SCRIPT>

GetSpecialFolder (Directory code)//Get the path of some special directories in the system, the directory code has 3 is 0: Install Windows directory 1: System files directory 2: Temporary files directory
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Tmpfolder = fso. GetSpecialFolder (2); Get the path to the system temp file directory as mine is C:\windows\temp.
alert (Tmpfolder);
-->
</SCRIPT>

GetTempName ()//generates a random temporary file object that follows the RAD lead followed by random numbers as if some software were to be generated during installation *.tmp
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var fso = new ActiveXObject ("Scripting.FileSystemObject");
Tmpname = fso. GetTempName (); I created a raddb70e.tmp when I was testing it.
alert (tmpname);
-->
</SCRIPT>

MoveFile (source file, target file)//move the source file to the location of the destination 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 in C to disk D
-->
</script>to be continue! There are several attributes have not written examples, later on, whether you feel that every time you are running a very troublesome? Or ..., wondering how to run without asking? (Don't use your feet to destroy other people's systems!) )
use JavaScript to manipulate file system create shortcuts
Copy Code code 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 = "Open a shortcut to a program";
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>

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.