Mari kita perkenalkan fungsi operasi file FSO di ASP.
Atribut Objek File FSO (File)
Digatedeated Mengembalikan Tanggal Pembuatan dan Waktu Folder
DatelaastAccessed mengembalikan tanggal dan waktu kunjungan terakhir ke file
DatelaastModify Mengembalikan tanggal dan waktu modifikasi terakhir dari file tersebut
Drive mengembalikan objek drive drive tempat file berada
Pengaturan Nama atau Mengembalikan Nama File
Parentfolder Mengembalikan objek folder dari folder induk file
Path Mengembalikan jalur absolut file, Anda dapat menggunakan nama file panjang
Shortname Mengembalikan Bentuk Nama File 8.3
ShortPath Mengembalikan Bentuk Dos -Style 8.3 dari File Absolute Path
Ukuran Mengembalikan ukuran file (byte)
Jika ketik mungkin, kembalikan string instruksi jenis file
Metode Objek File (File) FSO
Penggunaan Metode Objek File FSO
CopyFile Salin satu atau lebih file ke jalur baru
CreateTextFile membuat file dan mengembalikan objek TextStream
DeleteFile Hapus file
OpenTextFile membuka file dan mengembalikan objek TextStream untuk dibaca atau ditambahkan
File Nama Rena:
Kodenya adalah sebagai berikut:
Fungsi Ganti Nama (Sourcename, DestName)
redup, ofile
atur offso = server.createObject (scripting.filesystemObject)
set ofile = offso.getFile (server.mappath (sumber))))
Ofile.name = DestName
Atur offso = tidak ada
Set ofile = tidak ada
Fungsi akhir
Hapus file:
Kodenya adalah sebagai berikut:
Fungsi fsodel (nama file)
redup fso, f
atur fso = server.createObject (scripting.filesystemObject)
F = server.mappath (nama file)
Jika fso.fileexist (f) maka
Fso.deletefile f, true
akhiri jika
set f = tidak ada
atur fso = tidak ada
Fungsi akhir
Ubah string di file:
Kodenya adalah sebagai berikut:
Fungsi fsoreplace (nama file, target, repString)
Redup objfso, objcountfile, filetempdata
Set ObjFSO = Server.CreateObject (Scripting.FilesystemObject)
Setel objcountfile = objfso.opentextFile (server.mappath (nama file), 1, true)
FiletempData = objcountfile.readall
objcountfile.close
FiletempData = ganti (filetempdata, target, repString)
Set ObjCountFile = objfso.createTextFile (server.mappath (nama file), true)
objcountfile.writ filetempdata
objcountfile.close
Atur objcountfile = tidak ada
Atur objfso = tidak ada
Fungsi akhir
<%
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: CreateFolder (SPAD)
'Fungsi: Buat direktori
'Parameter: Spath: jalur direktori relatif dibuat
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Response.Write CreateFolder (/DGSunShine/UneadFile/Demo1/)
Function CreateFolder (SPI)
Pada kesalahan resume berikutnya
Redup fso, arrolder, folder, i, j
Jika ruang = lalu
CreateFolder = false
Fungsi keluar
Akhiri jika
Jika dibiarkan (spath, 1) = / lalu
Folder = /
Spath = Mid (Spath, 2, Len (Spath))))
Kalau tidak
Folder = ./
Akhiri jika
Jika kanan (spath, 1) = / lalu spath = kiri (spad, len (spad) -1)
Arrolder = split (spath,/)
Atur fso = server.createObject (scripting.filesystemObject)
Untuk i = 0 ke ubound (arrolder)
Jika i = 0 lalu
Folder = folder & arrolder (i) & /
Kalau tidak
Folder = folder & arrolder (i) & /
Akhiri jika
Jika fso.FoldRexists (server.mappath (folder)) = false maka
response.write server.mappath (folder)
Fso.createFolder (server.mappath (folder))
Akhiri jika
Berikutnya
Atur fso = tidak ada
Jika err.number <> 0 lalu
Err.clear ()
CreateFolder = false
Kalau tidak
CreateFolder = true
Akhiri jika
Fungsi akhir
Fungsi getFile (paramfilePath)
Atur fso = server.createObject (scripting.filesystemObject)
Atur fso_read = fso.opentextFile (server.mappath (paramfilePath), 1, false, -2)
getFile = fso_read.readall
Atur fso_read = tidak ada
Atur fso = tidak ada
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Fungsi Nama: CreateFile (ParamfileContent, ParamfilePath)
'Fungsi: Buat file
'Parameter: ParamfileContent' Konten File
Nama file 'ParamfilePath' (tidak termasuk jalur)
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi CreateFile (ParamfileContent, ParamfilePath)
Pada kesalahan resume berikutnya
Redup, fwrite
Atur fso = server.createObject (scripting.filesystemObject)
Atur fwrite = fso.createTextFile (server.mappath (paramfilePath), true)
fwrite.write ParamfileContent
fwrite.close ()
Atur fwrite = tidak ada
Atur fso = tidak ada
Jika err.number <> 0 lalu
Err.clear ()
CreateFile = false
Kalau tidak
CreateFile = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: Delfile (FilePath)
Fungsi: Hapus file
Parameter: FilePath 'Path File Beberapa file untuk beberapa file |
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi delfile (filepath)
Pada kesalahan resume berikutnya
Redup fso, arrfile, i
Jika getsafestr (filepath,) = lalu
CreateFolder = false
Fungsi keluar
Akhiri jika
Arrfile = split (filepath, | |)
Atur fso = server.createObject (scripting.filesystemObject)
untuk i = 0 ke ubound (arrfile)
Filepath = arrfile (i)
Jika fso.fileexist (server.mappath (filepath) lalu) lalu
Fso.deleteFile server.mappath (filepath)
Akhiri jika
Berikutnya
Atur fso = tidak ada
Jika err maka
Err.clear ()
Delfile = false
Kalau tidak
Delfile = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: Delfolder (FolderPath)
'Fungsi: Hapus Direktori
'Parameter: FolderPath' Directory Paths 'Multi -Direktori Pemisahan |
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi Delfolder (FolderPath)
Pada kesalahan resume berikutnya
Redup fso, arrolder, i
Jika getsafestr (folderpath,) = lalu
Delfolder = false
Fungsi keluar
Akhiri jika
Arrolder = split (folderpath, |)
Atur fso = server.createObject (scripting.filesystemObject)
Untuk i = 0 ke ubound (arrolder)
FolderPath = arrolder (i)
Jika FSO.FoldRexists (Server.Mappath (FolderPath) lalu) lalu
Fso.deleteFolder server.mappath (folderpath)
Akhiri jika
Berikutnya
Jika err maka
Err.clear ()
Delfolder = false
'Showerror Hapus Direktori Gagal,
kalau tidak
Delfolder = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: IsExistFile (FilePath)
'Fungsi: Tentukan apakah ada file atau direktori
Parameter: FilePath 'Path File Beberapa file untuk beberapa file |
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi isexistfile (filepath)
Pada kesalahan resume berikutnya
Redup fso, arrfile, i
Jika getsafestr (filepath,) = lalu
IsExistFile = false
Akhiri jika
Arrfile = split (filepath, | |)
Atur fso = server.createObject (scripting.filesystemObject)
untuk i = 0 ke ubound (arrfile)
Filepath = arrfile (i)
Jika fso.fileexist (server.mappath (filepath) lalu) lalu
IsExistFile = true
Akhiri jika
Jika fso.FoldRexist (server.mappath (filepath)) lalu
IsExistFile = true
Akhiri jika
Berikutnya
Atur fso = tidak ada
Jika err maka
Err.clear ()
IsExistFile = false
'Showerror menilai apakah file atau direktori gagal,
kalau tidak
IsExistFile = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: Delfile (FilePath)
'Fungsi: Hapus file atau direktori
Parameter: FilePath 'Path File Beberapa file untuk beberapa file |
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi delfile (filepath)
Pada kesalahan resume berikutnya
Redup fso, arrfile, i
Jika getsafestr (filepath,) = lalu
CreateFolder = false
Akhiri jika
Arrfile = split (filepath, | |)
Atur fso = server.createObject (scripting.filesystemObject)
untuk i = 0 ke ubound (arrfile)
Filepath = arrfile (i)
Jika fso.fileexist (server.mappath (filepath) lalu) lalu
Fso.deleteFile server.mappath (filepath)
Akhiri jika
Jika fso.FoldRexist (server.mappath (filepath)) lalu
Fso.deletefolder server.mappath (filepath)
Akhiri jika
Berikutnya
Atur fso = tidak ada
Jika err maka
Err.clear ()
Delfile = false
'Showerror Delete File atau Direktori Gagal,
kalau tidak
Delfile = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Fungsi Nama: RenameFile (OldName, Newname)
Fungsi: Ganti nama file atau direktori
'Parameter: Stroldname' Nama File Asli Multi -Use |
'StrnewName' nama baru multi -Use |
'Harap simpan dua parameter yang sama konsisten
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi RenameFile (Stroldname, StrnewName)
Pada kesalahan resume berikutnya
Redup, arrold, arrnew, i, oldname, newname
Lama = getsafestr (stroldname,)
Newfile = getsafestr (strnewName,)
Jika lama = atau newfile = lalu
RenameFile = false
Fungsi keluar
Akhiri jika
arold = split (stroldname, |)
arrnew = split (strnewName, |)
Jika ubound (arrold) <> ubound (arrnew)
RenameFile = false
Fungsi keluar
Akhiri jika
Atur fso = server.createObject (scripting.filesystemObject)
untuk i = 0 ke ubound (panah)
OldName = server.mappath (arrold (i))
newname = server.mappath (arrnew (i))
Jika fso.fileexist (OldName) dan bukan FSO.FileExists (newname)
fso.movefile OldName, newname
'RenameFile = true
Akhiri jika
Berikutnya
Atur fso = tidak ada
Jika err.number <> 0 lalu
Err.clear ()
RenameFile = false
Kalau tidak
RenameFile = true
Akhiri jika
Fungsi akhir
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
'Nama Fungsi: CopyFiles (Tempsource, Tempend)
'File: Salin file atau direktori
Parameter: Tempsource 'Sumber File Nama Multi -Purpose |
'Tempend' nama file yang dapat dihancurkan multi -purpose |
'Catatan: Harap simpan dua parameter yang sama di atas, dan keduanya adalah jalur lengkap,
'Metode server.mappath telah diproses
'Nilai Pengembalian: Sukses Benar, Gagal Salah
'*********************************************** ********************************************** ********************************************** ********************************************** ********************************************** **************, ******
Fungsi CopyFiles (Tempsource, Tempend)
Pada kesalahan resume berikutnya
Redup copyfso, arrsource, arrend
CopyFiles = false
Set copyfso = server.createObject (scripting.filesystemObject)
Jika Tempsource = atau Tempend = lalu
File atau direktori copy errraise, kondisinya kosong
CopyFiles = false
Fungsi keluar
Akhiri jika
Arrsource = split (Tempsource, |)
Arrend = split (Tempend, |)
Jika ubound (arrsource) <> ubound (arbit)
CopyFiles = false
Fungsi keluar
Akhiri jika
untuk i = 0 ke ubound (arrsource)
srcname = arrsource (i)
tarname = arrend (i)
Jika copyfso.filexists (srcname) dan tidak copyfso.filexist (tarname) maka
Copyfso.copyfile srcname, tarname
CopyFiles = true
Akhiri jika
Jika copyfso.FoldREXIST (SRCNAME) dan tidak copyfso.Foldrexist (Tarname)
Copyfso.copyfolder srcname, tarname
CopyFiles = true
Akhiri jika
Berikutnya
Atur copyfso = tidak ada
Jika err maka
'Err.clear ()
CopyFiles = false
Akhiri jika
Fungsi akhir
%>