查找文件的语句
6 B+ H8 u! o4 j3 R/ ]2 J' ?) r6 B1 _1 O6 @. N8 y
CODE:# P2 @; M5 b' E: V7 f( r7 |
5 A! ? Q N# H( M+ F. S+ _drop table tmp;! q& ^" z1 R0 e; a- `
create table tmp5 T0 M0 H! `8 S
(( w# z7 n. H8 x$ J1 d
[id] [int] IDENTITY (1,1) NOT NULL,
- {9 Q4 O7 W" Z. j[name] [nvarchar] (300) NOT NULL,
" L' c% u: j, r[depth] [int] NOT NULL,
9 i6 X* o: L/ x! F, y[isfile] [nvarchar] (50) NULL1 V: }5 W/ y7 t6 Y
);
' k; ~' C5 t" V) Q" x9 r( X( t2 R: j, {0 L4 \+ C) I
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
% Z* w' k# n! q. \ ~+ C; Cset @root='f:\usr\' -- Start root2 t+ b! V7 t3 x" H, R7 e: {
set @name='cmd.exe' -- Find file
! K* C( E$ s+ W6 S* k; Ninsert into tmp exec master..xp_dirtree @root,0,1--
3 j6 A5 F. Y1 S. v5 C$ mset @id=(select top 1 id from tmp where isfile=1 and name=@name)
* y$ y6 [" }1 p2 g+ X3 bset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)- X# s9 o* M O. P/ k
while @depth<>1
3 a2 ] y' a+ S% {& B8 ?( f8 ~9 Gbegin ; Q8 e. L) F7 x, W0 W D
set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) ! y b, _3 p( T' r" L6 T, `$ P
set @depth=(select depth from tmp where id=@id) ) a# G. H6 a( F& Q3 C" g7 U
set @name=(select name from tmp where id=@id)+'\'+@name
- D6 g, f6 B! _; S7 k$ ]; p$ nend
2 J) P6 V# U O9 g2 e. R; {update tmp set name=@root+@name where id=1# \$ Y) B$ G! X5 R( R# v2 R3 K
select name from tmp where id=1
_9 o: ^, N* I/ i- q5 ^ o: c8 m) o% a7 g R, c# M) _
查找目录的语句: Q( u( A7 C/ {" A6 k) [2 S o9 ]4 g
8 f1 o# l% s5 ?9 ]4 U; L! x& Y& M( t
5 C! Y0 I3 O6 C( T8 j/ f) H/ zCODE:0 S; x) x! [# j; @% l2 `+ J4 O3 c
& _. h& l" S: D6 l1 n: J1 i3 d2 a
4 R6 Q. L3 S9 D0 Z7 n
drop table tmp;
. q% H5 H7 J3 _1 dcreate table tmp
- D; I7 `/ P" N7 | J i(+ T; T) w8 @: [. ^
[id] [int] IDENTITY (1,1) NOT NULL,
& c+ H2 j5 s) _. ^# u- r$ q) G[name] [nvarchar] (300) NOT NULL,
/ ^6 _- b* c4 E6 ?+ E" x+ b[depth] [int] NOT NULL4 L2 F+ M- `) r
);! ]: W2 F& a# A" v$ n
7 q, y& L; q1 v# W( @& K) k
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
+ i/ ~" L3 W8 n3 Qset @root='f:\usr\' -- Start root
) T# P: M# D4 `, W6 Tset @name='donggeer' -- directory to find3 @1 Y' ^# R- M5 v; ^
insert into tmp exec master..xp_dirtree @root,0,0- `' }2 b, B. l
set @id=(select top 1 id from tmp where name=@name)
& O) u% a4 t& U6 I: mset @depth=(select top 1 depth from tmp where name=@name)
. k4 C% ~2 n) u2 Gwhile @depth<>1 , U C+ ]5 M. f! [9 U9 o. ]
begin ) {7 h3 ^# Z! A% h- j$ j; D1 w
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)1 j) N3 }+ _% j9 H
set @depth=(select depth from tmp where id=@id)
m0 q$ k) j# j8 b# Rset @name=(select name from tmp where id=@id)+'\'+@name + k% _$ |' }# Z' Y* X- U
end update tmp set name=@root+@name where id=1
9 F) z+ }6 S6 L& C+ Yselect name from tmp where id=1 |