Home Web Front-end JS Tutorial Detailed explanation of how to operate JavaScript files

Detailed explanation of how to operate JavaScript files

Mar 14, 2017 pm 03:30 PM

可以通过浏览器在访问者的硬盘上创建文件,因为我开始试了一下真的可以,不信你把下面这段代码COPY到一个HTML文件当中再运行一下!

<script language="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
fso.DeleteFile("c:\\test.txt", true); //请注意啊!把test.txt改为你C盘中的其它文件名,你不改也可以!先把test.txt备份啊! 
--> 
</script>
Copy after login

是不是发现你C盘的test.txt文件不见呢?呵呵其实当那个文件运行时IE会提醒你当前使用的ActiveX控件安全,询问你是否运行,但由于你和我一样迫切想试试究竟效果如何,因此你会毫不犹疑的按下 [确定] …. 其实这是使用了FileSystemObject来实现的,要了解更详细的用法与例子的话,点这里下载 JScript的中文说明文档 或者买本 <<深入学习:JavaScript开发与实例>> 也可以看看无忧脚本整理的例子,让大家学习学习. 让我们先来看看有什么属性函数可用,后面会对部分函数做些小例子

CopyFile() 复制文件 
CopyFolder() 复制目录 
CreateFolder() 创建新目录 
CreateTextFile() 生成一个文件 
DeleteFile() 删除一个文件 
DeleteFolder() 删除一个目录 
DriveExists() 检验盘符是否存在 
Drives 返回盘符的集合 
FileExists() 检验文件是否存在 
FolderExists 检验一个目录是否存在 
GetAbsolutePathName() 取得一个文件的绝对路径 
GetBaseName() 取得文件名 
GetDrive() 取得盘符名 
GetDriveName() 取得盘符名 
GetExtensionName() 取得文件的后缀 
GetFile() 生成文件对象 
GetFileName() 取得文件名 
GetFolder() 取得目录对象 
GetParentFolderName 取得文件或目录的父目录名 
GetSpecialFolder() 取得特殊的目录名 
GetTempName() 生成一个临时文件对象 
MoveFile() 移动文件 
MoveFolder() 移动目录 
OpenTextFile()
Copy after login

打开一个文件流实例说明BuildPath(路径,文件名) //这个方法会对给定的路径加上文件,并自动加上分界符

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var newpath = fso.BuildPath("c:\\tmp", "51js.txt"); //生成 c:\tmp\51js.txt的路径 
alert(newpath); 
--> 
</SCRIPT>
Copy after login

CopyFile(源文件, 目标文件, 覆盖) //复制源文件到目标文件,当覆盖值为true时,如果目标文件存在会把文件覆盖

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var newpath = fso.CopyFile("c:\\test.txt", "d:\\autoexec.bak"); 
--> 
</SCRIPT>
Copy after login

CopyFolder(对象目录,目标目录 ,覆盖) //复制对象目录到目标目录,当覆盖为true时,如果目标目录存在会把文件覆盖

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\"); //把C盘的Desktop目录复制到D盘的根目录 
--> 
</SCRIPT>
Copy after login

CreateFolder(目录名) //创建一个新的目录

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var newFolderName = fso.CreateFolder("c:\\51JS"); //在C盘上创建一个51JS的目录 
--> 
</SCRIPT>
Copy after login

CreateTextFile(文件名, 覆盖) //创建一个新的文件,如果此文件已经存在,你需要把覆盖值定为true

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var newFileObject = fso.CreateTextFile("c:\\autoexec51JS.bat", true); //脚本将在C盘创建一个叫 autoexec51JS.bat的文件 
--> 
</SCRIPT>
Copy after login

DeleteFile(文件名, 只读?) //删除一个文件,如果文件的属性是只读的话,你需要把只读值设为true

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); //为了安全我先把要删除的test.txt备份到你的D盘 
var newpath = fso.CopyFile("c:\\test.txt", "d:\\test.txt"); //把C盘的test.txt文件删除掉 
fso.DeleteFile("c:\\test.txt", true); 
--> 
</SCRIPT>
Copy after login

DeleteFolder(文件名, 只读?)//删除一个目录,如果目录的属性是只读的话,你需要把只读值设为true

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\"); //为了安全我先把你C盘的Desktop目录复制到你D盘的根目录 
fso.DeleteFolder("c:\\WINDOWS\\Desktop", true); //把你的Desktop目录删除,但因为desktop是系统的东西,所以不能全部删除,但......... 
--> 
</SCRIPT>
Copy after login

DriveExists(盘符) //检查一个盘是否存在,如果存在就返会真,不存在就返回…….

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
hasDriveD = fso.DriveExists("d"); //检查系统是否有D盘存在 
hasDriveZ = fso.DriveExists("z"); //检查系统是否有Z盘存在 
if (hasDriveD) alert("你的系统内有一个D盘"); 
if (!hasDriveZ) alert("你的系统内没有Z盘"); 
--> 
</SCRIPT>
Copy after login

FileExists(文件名) //检查一个文件是否存在,如果存在就返会真,不存在就返回…….

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
fileName = fso.FileExists("c:\\test.txt"); 
if (fileName) alert("你在C盘中有test.txt文件,按下确定后这个文件将被删除!"); //开个玩笑:) 
--> 
</SCRIPT>
Copy after login

FolderExists(目录名) //检查一个目录是否存在,如果存在就返会真,不存在就返回…….

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
folderName = fso.FolderExists("c:\\WINDOWS\\Fonts"); 
if (folderName) alert("按下确定后系统的字库将被删除!"); //开个玩笑:) 
--> 
</SCRIPT>
Copy after login

GetAbsolutePathName(文件对象) //返回文件对象在系统的绝对路径

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
pathName = fso.GetAbsolutePathName("c:\\test.txt"); 
alert(pathName); 
--> 
</SCRIPT>
Copy after login

GetBaseName(文件对象) //返回文件对象的文件名

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
baseName = fso.GetBaseName("c:\\test.txt"); //取得test.txt的文件名autoexec 
alert(baseName); 
--> 
</SCRIPT>
Copy after login

GetExtensionName(文件对象) //文件的后缀

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
exName = fso.GetExtensionName("c:\\test.txt"); //取得test.txt后缀bat 
alert(exName); 
--> 
</SCRIPT>
Copy after login

GetParentFolderName(文件对象) //取得父级的目录名

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
parentName = fso.GetParentFolderName("c:\\test.txt"); //取得test.txt的父级目录C盘 
alert(parentName); 
--> 
</SCRIPT>
Copy after login

GetSpecialFolder(目录代码) //取得系统中一些特别的目录的路径,目录代码有3个分别是 0:安装Window的目录 1:系统文件目录 2:临时文件目录

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
tmpFolder = fso.GetSpecialFolder(2); //取得系统临时文件目录的路径如我的是 C:\windows\temp 
alert(tmpFolder); 
--> 
</SCRIPT>
Copy after login

GetTempName() //生成一个随机的临时文件对象,会以rad带头后面跟着些随机数,就好象一些软件在安装时会生成*.tmp

<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
tmpName = fso.GetTempName(); //我在测试时就生成了radDB70E.tmp 
alert(tmpName); 
--> 
</SCRIPT>
Copy after login

MoveFile(源文件, 目标文件) //把源文件移到目标文件的位置

<SCRIPT LANGUAGE="JavaScript">
 <!--
 var fso = new ActiveXObject("Scripting.FileSystemObject");
 var newpath = fso.MoveFile("c:\\test.txt", "d:\\test.txt"); //把C盘的test.txt文件移移动到D盘
 -->
 </SCRIPT>
Copy after login

To Be Continue! 还有几个属性没写例子,迟点就有了,是否觉得每次都会问你是否运行很麻烦?或者……..,想知道如何不用询问就运行?(不要用脚本来破坏别人的系统啊!)

用JavaScript操作文件系统创建快捷方式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>用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 = "打开一个程序的快捷方式"; 
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>
Copy after login

The above is the detailed content of Detailed explanation of how to operate JavaScript files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

Can I delete gho files? Can I delete gho files? Feb 19, 2024 am 11:30 AM

A gho file is an image file created by NortonGhost software and used to back up and restore the operating system and data. In some cases, you can delete gho files, but do so with caution. This article will introduce the role of gho files, precautions for deleting gho files, and how to delete gho files. First, let's understand the role of gho files. A gho file is a compressed system and data backup file that can save an image of an entire hard disk or a specific partition. This kind of backup file is usually used for emergency recovery

Go Programming Tips: Deleting Contents from a File Go Programming Tips: Deleting Contents from a File Apr 04, 2024 am 10:06 AM

The Go language provides two methods to clear file contents: using io.Seek and io.Truncate, or using ioutil.WriteFile. Method 1 involves moving the cursor to the end of the file and then truncating the file, method 2 involves writing an empty byte array to the file. The practical case demonstrates how to use these two methods to clear content in Markdown files.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles