accesso ai file

Dim objFileSystem Set objFileSystem = CreateObject("Scripting.FileSystemObject")

esistenza

if objFileSystem.FolderExists("nome") if objFileSystem.FileExists(Server.MapPath("nomefile"))

oggetto file

proprietà: Drive, Name, Path, ParentFolder, ShortName, ShortPath, DataCreated, DateLastAccessed, DateLastModified, Type, Size, Attributes

oggetto folder

proprietà: le stesse di file, più Files e Subfolders (collection), IsRootFolder (boolean), ParentFolder (istanza dell'oggetto folder)

... Set objFile = objFileSystem.GetFile(Server.MapPath("nomefile")) ResponseWrite "il file è stato modificato " & objFile.DateLastModified

file in una directory: la collezione Files

<%@ Language=VBscript %> <% Option Explicit %> <HTML><BODY> <% Dim objFileSystem, objFolder, objFile Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objFolder = objFileSystem.GetFolder(Server.MapPath("nomedir")) Response.Write "file in " & objFolder.Path & ":<BR>" For Each objFile in objFolder.Files Response.Write objFile.Nome & "<BR>" Next Set objFolder = Nothing Set objFileSystem = Nothing %>

creare file

Dim objFileSystem, objNewFile Set objFileSystem = CreateObject("Scripting.FileSystemObject") If Not objFileSystem.FileExist(Server.MapPath("nomefile")) then Set objNewFile = objFileSystem.CreateTextFile(Server.MapPath("nomefile"),False) End If

aprire file

Dim objFileSystem, objOpenFile, strPath Set objFileSystem = CreateObject("Scripting.FileSystemObject") strPath=Server.MapPath("nomefile") Set objOpenFile = objFileSystem.OpenTextFile(strPath,1) Dim objFileSystem, objFile, objOpenFile Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objFile = objFileSystem.GetFile(Server.MapPath("nomefile")) Set objOpenFile = objFile.OpenAsTextStream(ForReading)

leggere file

Read, ReadLine, ReadAll leggono caratteri, righe o tutto

... aprire il file Do While Not objOpenFile.AtEndOfStream ' legge 80 caratteri alla volta Response.Write objOpenFile.ReadLine & "<BR>" Loop ... chiudere il file e gli oggetti

visualizzare un file asp

<%@ Language=VBscript %> <% Option Explicit %> <HTML><BODY> <% Dim objFileSystem, objOpenFile, strPath, strText strPath=Request.QueryString("URL") strPath=Server.MapPath(strPath) Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objOpenFile = objFileSystem.OpenTextFile(strPath,1) ' 1=ForReading Response.Write "<PRE>" Do While Not objOpenFile.AtEndOfStream strText=objOpenFile.ReadLine Response.Write Server.HTMLEncode(strText) & "<BR>" Loop Response.Write "</PRE>" objOpenFile.close Set objOpenFile = Nothing Set objFileSystem = Nothing %>

scrivere file

Write, WriteLine

<%@ Language=VBscript %> <% Option Explicit %> <HTML><BODY> <% Dim objFileSystem, objOpenFile, strPath, strText strPath=Request.QueryString("URL") strPath=Server.MapPath(strPath) Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objOpenFile = objFileSystem.OpenTextFile(strPath,2) ' 2=ForWriting objOpenFile.WriteLine("abcdefg") objOpenFile.close Set objOpenFile = Nothing Set objFileSystem = Nothing %>

contatore n. di visite

<%@ Language=VBscript %> <% Option Explicit %> <HTML><BODY> <% Dim objFileSystem, objOpenFile, strPath, strText strPath=Request.QueryString("pagina") & ".txt" strPath=Server.MapPath(strPath) Set objFileSystem = CreateObject("Scripting.FileSystemObject") If objFileSystem.FileExists(strPath) then Set objOpenFile = objFileSystem.OpenTextFile(strPath,1) ' 1=ForReading iCount=Cint(objOpenFile.ReadLine)+1 objOpenFile.close Else iCount=1 End If Set objOpenFile = objFileSystem.CreateTextFile(strPath,True) ' True=sovrascrivi objOpenFile.WriteLine(iCount) objOpenFile.close Set objOpenFile = Nothing Set objFileSystem = Nothing Response.Redirect(Request.QueryString("URL")) %>

appendere a un file

Set objOpenFile = objFileSystem.OpenTextFile(strPath,ForAppending)