O FSO é um controle para operações de arquivo no Microsoft ASP. Portanto, o editor do novo canal errado compartilhará com você uma análise detalhada do código FSO.
Funções que modificam o conteúdo específico do arquivo usando o FSO
functionFSoChange (nome do arquivo, destino, string)
DIMOBJFSO, OBJCOUNTFILE, FILETEMPDATA
Setobjfso = server.createObject ("scripting.filesystemoBject")
SetObjCountFile = objfso.opentExtFile (Server.mappath (nome do arquivo), 1, true)
FileTempData = objCountFile.readall
objCountFile.Close
FileTempData = Substitua (FileTempData, Target, String)
SetObJCountFile = objfso.createTextFile (Server.mappath (nome do arquivo), true)
objCountFile.WriteFilETEMPData
objCountFile.Close
SetoBJCountFile = nada
Setobjfso = nada
Função final
Funções que usam FSO para ler o conteúdo do arquivo
functionfsofileRead (nome do arquivo)
DIMOBJFSO, OBJCOUNTFILE, FILETEMPDATA
Setobjfso = server.createObject ("scripting.filesystemoBject")
SetObjCountFile = objfso.opentExtFile (Server.mappath (nome do arquivo), 1, true)
FsofileRead = objCountFile.readall
objCountFile.Close
SetoBJCountFile = nada
Setobjfso = nada
Função final
Funções que usam FSO para ler uma linha de um arquivo
functionFSolinedit (nome do arquivo, linho)
iflinenum
Dimfso, F, Temparray, Tempcnt
setfso = server.createObject ("scripting.filesystemobject")
ifnotfso.fileExists (Server.mappath (nome do arquivo)) ThenExitFunction
setf = fso.opentExtFile (Server.mappath (nome do arquivo), 1)
ifnotf.atendofstreamthen
tempcnt = f.readall
F.Close
setf = nada
modelo = divisão (tempcnt, chr (13) e chr (10))
iflinenum> ubund (temparray)+1hen
saída de saída
outro
Fsolinedit = temparray (linho-1)
endif
endif
Função final
Funções que usam o FSO para escrever uma certa linha de um arquivo
functionFSolineWrite (nome do arquivo, linho, linecontent)
iflinenum
Dimfso, F, Temparray, Tempcnt
setfso = server.createObject ("scripting.filesystemobject")
ifnotfso.fileExists (Server.mappath (nome do arquivo)) ThenExitFunction
setf = fso.opentExtFile (Server.mappath (nome do arquivo), 1)
ifnotf.atendofstreamthen
tempcnt = f.readall
F.Close
modelo = divisão (tempcnt, chr (13) e chr (10))
iflinenum> ubund (temparray)+1hen
saída de saída
outro
Modelo (linho-1) = LineContent
endif
tempcnt = junção (Temparray, Chr (13) e Chr (10))
setf = fso.createTextFile (server.mappath (nome do arquivo), true)
F.WRITETEMPCNT
endif
F.Close
setf = nada
Função final
Funções para adicionar novas linhas de um arquivo usando FSO
functionFSOAPLINE (nome do arquivo, linecontent)
dimfso, f
setfso = server.createObject ("scripting.filesystemobject")
ifnotfso.fileExists (Server.mappath (nome do arquivo)) ThenExitFunction
setf = fso.opentExtFile (Server.mappath (nome do arquivo), 8,1)
F.Writechr (13) e Chr (10) e LineContent
F.Close
setf = nada
Função final
Funções que leem a última linha do arquivo
functionfsolastline (nome do arquivo)
Dimfso, F, Temparray, Tempcnt
setfso = server.createObject ("scripting.filesystemobject")
ifnotfso.fileExists (Server.mappath (nome do arquivo)) ThenExitFunction
setf = fso.opentExtFile (Server.mappath (nome do arquivo), 1)
ifnotf.atendofstreamthen
tempcnt = f.readall
F.Close
setf = nada
modelo = divisão (tempcnt, chr (13) e chr (10))
Fsolastline = temparray (ubund (temparray))
endif
Função final
FSO substitui o caráter do arquivo especificado
Código do programa:
'FSO substitui o caráter do arquivo especificado
Functionfsolineedit (nome do arquivo, destino, string)
DIMOBJFSO, OBJCOUNTFILE, FILETEMPDATA
Setobjfso = server.createObject ("scripting.filesystemoBject")
SetObjCountFile = objfso.opentExtFile (Server.mappath (nome do arquivo), 1, true)
FileTempData = objCountFile.readall
objCountFile.Close
FileTempData = Substitua (FileTempData, Target, String)
SetObJCountFile = objfso.createTextFile (Server.mappath (nome do arquivo), true)
objCountFile.WriteFilETEMPData
objCountFile.Close
SetoBJCountFile = nada
Setobjfso = nada
Função final
'Response.WritefSolineEdit ("test.txt", "mundo", "amanhã é um bom dia para ir")
Excluir arquivos
Código do programa:
'Exclua o arquivo
FunctionDelfile (nome do arquivo)
IffileName "" então
Setfso = server.createObject ("scripting.filesystemobject")
Iffso.fileExists (nome do arquivo) Então
FSO.DeleteFileFilename
Endif
Setfso = nada
Endif
Função final
Determinar se o arquivo existe
Código do programa:
'Apenas determine se o arquivo existe
FunctionReportFileStatus (FileSecec)
Dimfso, msg
Setfso = createObject ("script.filesystemoBject")
If (fso.fileExists (FileSeC)) então
msg = FileSpec & "Existe".
Outro
msg = FileSpec & "Não existe".
Endif
Relatóriofilestatus = Msg
Função final
Funções que modificam o conteúdo específico do arquivo usando o FSO
Código do programa:
'Use o FSO para modificar funções de conteúdo específicas de arquivo
FunctionFSoChange (nome do arquivo, destino, string)
DIMOBJFSO, OBJCOUNTFILE, FILETEMPDATA
Setobjfso = server.createObject ("scripting.filesystemoBject")
SetObjCountFile = objfso.opentExtFile (Server.mappath (nome do arquivo), 1, true)
FileTempData = objCountFile.readall
objCountFile.Close
FileTempData = Substitua (FileTempData, Target, String)
SetObJCountFile = objfso.createTextFile (Server.mappath (nome do arquivo), true)
objCountFile.WriteFilETEMPData
objCountFile.Close
SetoBJCountFile = nada
Setobjfso = nada
Função final
Funções que usam o FSO para escrever uma certa linha de um arquivo
Código do programa:
'Use o FSO para escrever uma função que tenha uma certa linha de um arquivo
FunctionFSolineWrite (nome do arquivo, linho, linecontent)
Iflinenum
Dimfso, F, Temparray, Tempcnt
Setfso = server.createObject ("scripting.filesystemobject")
Ifnotfso.fileExists (Server.mappath (nome do arquivo)) ThenExitFunction
Setf = fso.opentExtFile (Server.mappath (nome do arquivo), 1)
Ifnotf.atendofstreamthen
tempcnt = f.readall
F.Close
Temparray = Split (Tempcnt, Chr (13) e Chr (10))
Iflinenum> ubound (temparray)+1hen
Saída de saída
Outro
Modelo (linho-1) = LineContent
Endif
tempcnt = junção (Temparray, Chr (13) e Chr (10))
Setf = fso.createTextFile (server.mappath (nome do arquivo), true)
F.WRITETEMPCNT
Endif
F.Close
Setf = nada
Função final
O programa que cria um diretório. Se houver vários níveis de diretórios, crie um nível de cada vez.
Código do programa:
'O programa que cria um diretório. Se houver vários níveis de diretórios, cria um nível de cada vez.
FunctionCreateRir (ByVallocalPath)
OnErrorReSumenext
LocalPath = Substitua (LocalPath, "/", "/")
SetFileObject = server.createObject ("scripting.filesystemoBject")
PATHARR = SPLIT (LOCALPATH, "/")
path_level = ubound (patharr)
Fori = 0topath_level
Ifi = 0ThenPathtmp = patharr (0) e "/" elsepathtmp = pathtmp & patr (i) & "/"
cpath = esquerda (pathtmp, len (pathtmp) -1)
IfnotFileObject.Polderexists (cpath) thenfileObject.createFoldercPath
Próximo
SetFileObject = nada
Iferr.Number0Then
CreateDir = false
Err.clear
Outro
CreativeIr = true
Endif
Função final
Aqui estão alguns desses recursos incomuns, mas muito legais:
Recursos FSO que raramente são entendidos
GetSpecialFoldermethod Retorna o caminho de uma pasta Windows específica: Diretório de instalação do Windows; Diretório do sistema Windows; Diretório temporário do Windows FSO.GetSpecialFolder ([0,1, OR2])
GetTempNameMethod Retorna um arquivo ou nome de diretório gerado aleatoriamente, que é usado quando os dados temporários são necessários.
GetAbsolutepathnamemethod Retorna o caminho absoluto para a pasta (semelhante ao server.mappath).
Por exemplo, fso.getabsolutepathname ("região") retornará um resultado semelhante ao seguinte: "C: MyDocsMyFoldEregion"
GetExtensionNineMethod Retorna a extensão da última parte do caminho
(Por exemplo: fso.getextensionName ("c: docsest.txt") retornará o txt)
GetBasenameAndParentFoldermethods Retorna a pasta pai da última parte do caminho
(Por exemplo: fso.getParentFolder ("C: DocsMydocs") retornará 'Docs')
O DrivesProperty retorna uma coleção de todas as unidades disponíveis localmente para construir interfaces de usuário do tipo navegador de recursos.
Ao usar as funções acima, é melhor criar um código para manipulação de erros. Porque se os parâmetros necessários não existirem, informações problemáticas serão geradas.
O acima é uma análise detalhada do código FSO. Eu acredito que você aprendeu tudo. Para mais conteúdo relacionado, continue prestando atenção ao canal de tecnologia Wuxin.