机器的文件太多,需要整理一下,该如何做呢?????
本文以整理图片文件为例,给大家一点思路
代码的运行环境:iis5.0+sql server2000
数据库脚本:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[insertpic]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[insertpic]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[showpage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[showpage]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[picpath]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[picpath]
GO
CREATE TABLE [dbo].[picpath] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[path] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
--作用:插入记录
CREATE PROCEDURE [insertpic]
(
--路径--
@path varchar(100)
)
AS
insert picpath(path) values(@path)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
/*
'# 过程:showpage
'# 描述:用来记录集分页
'# 参数: - pagenum (页码)
'# 返回:-两个记录集,第一个记录集包含两个字段(总页数),第二个记录集为数据库返回给程序要显示的数据
'# 作者:zhengs
'# 日期:2002-08-27
*/
CREATE PROCEDURE showpage
----页码
@PageNum int
AS
SET NOCOUNT ON
declare
@pagecount int,
@iFrom int,
@iRowCount int,
@dpicid int
----计算该页起始的偏移量
if @PageNum <= 0
set @PageNum = 1
set @iFrom = 10 * (@PageNum - 1) + 1
----判断传入的页码是否有效
select @iRowCount = count(id) from picpath ----取得图片数
set @PageCount = @iRowCount / 10 ----计算图片页数
if @iRowCount %10> 0
set @PageCount = @PageCount + 1
if @iRowCount < @iFrom
begin
set @iFrom = @iRowCount - 10
end
if @iFrom<0
select @iFrom=0
set rowcount @iFrom
select @dpicid = id from picpath order by id desc
set rowcount 0
----取得图片列表
select @pagecount as pagecount
select top 10 * from picpath Where id <= @dpicid order by id desc
SET NOCOUNT off
SP_END:
select @pagecount as pagecount
SET NOCOUNT off
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
搜索并存储到数据库
search.asp
<%@LANGUAGE="VBSCRIPT" %>
<%
'***********************************************************************************
' 文件名.........: search.asp
' 作者...........: cxb
' 说明...........: 搜索并存储到数据库
' 注意...........:
' 版权...........: Copyright (c) 2000, NetDragon Software.
' 修改记录.......: 时间 人员 备注
' --------- ------- -------------------------------------------
' 2003-09-26 陈兴柏 创建文件
'***********************************************************************************
Server.ScriptTimeOut=500
dim a,b
'检测时间参数
a=timer
dim conn,strconn
Set conn = Server.CreateObject("ADODB.Connection")
strconn = "Provider=SQLOLEDB;Data Source=127.0.0.1;Initial Catalog=search;User ID=sa;Password=196881"
conn.open strconn
Const adCmdStoredProc = &H0004
Const adParamInput = &H0001
Const adVarChar = 200
||||||'# --------------------------------------------------------------------------
'# 函数:getFileExtName
'# 描述:获得文件是否为图片文件
'# 参数:--fName
'# 返回:--true or false
'# 作者:cxb
'# 日期:2003-9-26
'#--------------------------------------------------------------------------
function getFileExtName(fName)
if instr(fname,".gif") or instr(fname,".GIF") or instr(fname,".jpg") or instr(fname,".JPG") or instr(fname,".bmp") or instr(fname,".BMP") then
getFileExtName=true
else
getFileExtName=false
end if
end function
'# --------------------------------------------------------------------------
'# 函数:insertfilepath
'# 描述:将路径信息插入数据库
'# 参数:--filename
'# 返回:--
'# 作者:cxb
'# 日期:2003-9-26
'#--------------------------------------------------------------------------
function insertfilepath(filename)
dim picCommand,filepath
set picCommand=Server.CreateObject("ADODB.command")
set picCommand.ActiveConnection=conn
picCommand.CommandType=adCmdStoredProc
picCommand.CommandText="insertpic"
set filepath=picCommand.CreateParameter("path",advarchar,adparaminput,100)
picCommand.parameters.append filepath
picCommand("path")=filename
picCommand.Execute
set picCommand=nothing
end function
'# --------------------------------------------------------------------------
'# 函数:ShowFiles
'# 描述:搜索文件夹和文件
'# 参数:objfolder
'# 作者:cxb
'# 返回:所有的文件名和所在的目录
'# 日期:2003-9-26
'#--------------------------------------------------------------------------
function ShowFiles(byref objfolder)
dim folder,filename
'递归获得所有的文件
for each folder in objfolder.subfolders
ShowFiles folder
next
for each filename in objfolder.files '搜索所有的文件
if getFileExtName(filename.name) then '检查是否为图片
insertfilepath(filename) '存储到数据库
end if
next
end function
dim objfolder,objrootfolder
set objfolder=Server.createobject("Scripting.FileSystemObject")
set objrootfolder=objfolder.GetFolder("C:\Inetpub\wwwroot\zy")
'这里的路径可以改变
if request.QueryString("action")="search" then
ShowFiles objrootfolder
b=timer '程序结束时间
response.Write("共运行"&b-a&"秒")
'搜索并存储
end if
set conn=nothing
%><title>搜索并存储到数据库</title>
<div align="center"><a href=search.asp?action=search>搜索并存储到数据库</a></div>
整理页面
show.asp
<%@LANGUAGE="VBSCRIPT" %>
<%
'***********************************************************************************
' 文件名.........: show.asp
' 作者...........: cxb
' 说明...........:
' 注意...........:
' 版权...........: Copyright (c) 2000, NetDragon Software.
' 修改记录.......: 时间 人员 备注
' --------- ------- -------------------------------------------
' 2003-09-26 陈兴柏 创建文件
'***********************************************************************************
Server.ScriptTimeOut=100
dim conn,strconn
Set conn = Server.CreateObject("ADODB.Connection")
strconn = "Provider=SQLOLEDB;Data Source=192.168.1.221;Initial Catalog=search;User ID=sa;Password=196881"
conn.open strconn
Const adCmdStoredProc = &H0004
Const adParamInput = &H0001
Const adInteger = 3
page=replace(request("page"),"'","")
if page="" then page=1
page=clng(page)
if request("action")="wl" then
'完成删除物理图片
delwl(Request("checkbox"))
end if
dim news
pic=createpic(page) '显示页面
conn.close
set conn=nothing
'# ----------------------------------------------------------------------------
'# 函数:delwl(id)
'# 描述:完成删除物理图片
'# 参数:id
'# 返回:--
'# 作者:陈兴柏
'# 日期:2003.09.26
'#-----------------------------------------------------------------------------
||||||function delwl(id)
if id <>"" then
dim fso,skyfile,rs,sql ,AdoCmd_del
Set fso = CreateObject("Scripting.FileSystemObject")
Set rs= Server.CreateObject("ADODB.Recordset")