【JavaScript】js操作ローカルファイル

8602 ワード

<html>   
<head>   
<title>   
</title>  
   <script type="text/javascript">       
        var   fso, f;   
        var ForWriting = 2; 
	var TriStateFalse = 0;
	fso = new   ActiveXObject("Scripting.FileSystemObject"); 
	//       
	f = fso.CreateTextFile("d:\\lcd_log.txt", true); 
	//        d    
	//f =fso.OpenTextFile("d:\\lcd_log.txt", ForWriting, true, TriStateFalse);
	//        d    
	//g = fso.GetFile("d:\\lcd_log.txt");  
	//f=g.OpenAsTextStream(ForWriting, true);   
	f.WriteLine("wang ba dan");   
	f.Close();  
   </script>   
</head>   
<body>   
</html>
JS   FSO   

<script language="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
fso.DeleteFile("c:\\autoexec.bat", true); //    ! autoexec.bat   C        ,      !  autoexec.bat   ! 
--> 
</script> 

      C  Autoexec.bat     ?   

          IE         ActiveX     ,       ,                   ,            [  ] ....        FileSystemObject    ,              ,      JScript             < <    :JavaScript     >>               ,       . 

                 ,              
          
BuildPath() 
         
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:\\autoexec.bat", "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"); //           autoexec.bat     D  
var newpath = fso.CopyFile("c:\\autoexec.bat", "d:\\autoexec.bat"); // C  autoexec.bat      
fso.DeleteFile("c:\\autoexec.bat", 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:\\autoexec.bat"); 
if (fileName) alert("  C   autoexec.bat  ,             !"); //    :) 
--> 
</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:\\autoexec.bat"); 
alert(pathName); 
--> 
</SCRIPT> 

GetBaseName(    ) //           
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
baseName = fso.GetBaseName("c:\\autoexec.bat"); //  autoexec.bat    autoexec 
alert(baseName); 
--> 
</SCRIPT> 

GetExtensionName(    ) //      
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
exName = fso.GetExtensionName("c:\\autoexec.bat"); //  autoexec.bat  bat 
alert(exName); 
--> 
</SCRIPT> 

GetParentFolderName(    ) //         
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
parentName = fso.GetParentFolderName("c:\\autoexec.bat"); //  autoexec.bat     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:\\autoexec.bat", "d:\\autoexec.bat"); // C  autoexec.bat      D  
--> 
</SCRIPT> 
<html>
<head>
<script>
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1, ForWriting = 2, ForAppending = 8;
function createFile(file){
   var tf = fso.CreateTextFile(file, true);
   tf.Close();
}
function readFileOnly(file){
   var ts = fso.OpenTextFile(file, ForReading);
   var s = ts.ReadAll();
   ts.Close();
   alert(s);
}
function readFileForWrite(file,content){
   var ts = fso.OpenTextFile(file, ForWriting);
   ts.Write(content);
   ts.Close();
}
function readFileForAppend(file,content){
   var ts = fso.OpenTextFile(file, ForAppending);
   ts.Write(content);
   ts.Close();
}
</script>
</head>
<body>
    <input type="button" onclick="createFile('d:\\test.txt');" value="    "/><br>
    <input type="button" onclick="readFileOnly('d:\\test.txt');" value="    "/><br>
    <input type="button" onclick="readFileForWrite('d:\\test.txt','write');" value="      "/><br>
    <input type="button" onclick="readFileForAppend('d:\\test.txt','append');" value="       "/>
</body>
</html>