search
HomeWeb Front-endJS TutorialDetailed explanation of how to operate JavaScript files

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

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

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

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

打开一个文件流实例说明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>

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

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

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

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

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

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

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

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

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>

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>

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>

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

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

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

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

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

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

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

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

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

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

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

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

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>

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

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

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>

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>

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
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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.