查找文件的语句
' w3 T5 N0 ?5 ^; m& x3 T) p$ P+ w3 s# S; g
CODE:) K+ V/ X7 k1 j* w0 j
8 ?) y2 `. Q ^7 w; G+ B3 U* ?drop table tmp;
$ ]2 H, e% \% {! W5 M/ Ccreate table tmp7 D$ t. F; `& q( [# W. X" G: s
(
{3 e% L5 E. L& w% x3 n[id] [int] IDENTITY (1,1) NOT NULL,& T+ U- V! y8 K9 w5 N5 {: K2 u
[name] [nvarchar] (300) NOT NULL,8 k1 c+ M% S( x- V2 r% C
[depth] [int] NOT NULL,$ _! C3 d0 n8 a6 h4 `. L; p# \
[isfile] [nvarchar] (50) NULL% N- T/ b$ {. k4 Q7 x8 k% [
);& q1 j! ]/ W( ^$ J! _1 d& h
8 L8 u+ Z9 l* vdeclare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
3 r2 W2 {9 p8 nset @root='f:\usr\' -- Start root5 O* V0 w% ~' y Y) Q, r9 ?- U
set @name='cmd.exe' -- Find file
# H% s6 x3 L' s; n, V- L; a0 N0 ?( winsert into tmp exec master..xp_dirtree @root,0,1--
9 J- i4 ]7 K7 a: X4 Jset @id=(select top 1 id from tmp where isfile=1 and name=@name)
& |1 a3 D( ^# [5 R% g$ rset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)( F; x4 T2 A7 K! w, w9 i( p2 _5 Y; ^8 N
while @depth<>1 ) P7 D, ~$ K+ [7 Q% j1 K9 ~0 |
begin
: H. Q5 z, b( |4 yset @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc)
, X/ A" y2 n. Z$ a+ G& Qset @depth=(select depth from tmp where id=@id)
9 L! s, c6 D! H$ [set @name=(select name from tmp where id=@id)+'\'+@name
6 X* U. C$ c7 q) p9 Eend
$ |$ i7 l1 L8 D5 Q/ G/ Kupdate tmp set name=@root+@name where id=1
3 P+ r8 x) V- D( a! q$ R) D& `; cselect name from tmp where id=1
2 o {7 [; P& I; o D9 j7 A7 n0 p, D0 x( w# w
查找目录的语句2 @5 K1 t; j+ ]4 H2 s3 Q" o3 W; M; {
x# u1 m3 c* m5 J+ ]
7 V: ?, M1 H' d1 x- D; i5 xCODE:) ], s$ W( ^' q. E
. C7 l3 d7 ^6 Y3 U; C
. x. B; X- g: M- F& {
drop table tmp;
, M: ]( j3 r; O- N; p( H. wcreate table tmp
; J5 U' q& d) Y D& E( R* `& c: W(
. W' G2 G- w' |% j7 l( n' B6 m. \[id] [int] IDENTITY (1,1) NOT NULL,
- d; i D [8 G8 m[name] [nvarchar] (300) NOT NULL,
6 O+ U) }, y8 K$ J2 x) U1 b/ {[depth] [int] NOT NULL
1 A# a$ W, b* W7 v( Y! T3 m);
( W) W# _% p6 ^) O6 u H7 ?2 k& h: F1 f) J5 w5 k; a+ X
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
) z$ T( b. C9 f7 xset @root='f:\usr\' -- Start root
$ E- ?+ \3 N9 K5 N+ K( m+ W9 |set @name='donggeer' -- directory to find5 a9 m. U! Y2 ~
insert into tmp exec master..xp_dirtree @root,0,04 r* d! b' W# g4 a! ?
set @id=(select top 1 id from tmp where name=@name)
5 k9 Y6 T9 O0 d% H3 \- ?0 rset @depth=(select top 1 depth from tmp where name=@name) ?7 G! Z4 m, u) s2 e9 w; k3 {. `( N% |
while @depth<>1 ( B4 V! N% r% Q7 A
begin $ Q* B3 J0 o. r0 E) A! c6 ]2 B# V
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
5 H' g) E3 W/ E+ ^set @depth=(select depth from tmp where id=@id)
; L5 R0 ~7 S2 T9 wset @name=(select name from tmp where id=@id)+'\'+@name
2 Y; K, g! l% ^' k- U: T( Dend update tmp set name=@root+@name where id=1
, X+ }0 V( Y7 }, uselect name from tmp where id=1 |