查找文件的语句) ] U, O$ K$ [# L7 w" g& A
v) g7 Z+ m, {! E% `1 f' S
CODE:; E$ B# J- J# P$ a- @9 O1 a
+ T! [ [$ p: S: \! v( \6 V9 Hdrop table tmp;
. t% {. i) {2 n! t/ O7 ?create table tmp
" N7 ? G0 b, U+ ~(/ D4 \$ d% p7 q* e+ U; U- e
[id] [int] IDENTITY (1,1) NOT NULL,
! q& x$ q( I. d/ W[name] [nvarchar] (300) NOT NULL,
- I8 S6 b" B3 u7 Y) z" A2 @+ ^, k[depth] [int] NOT NULL,
2 G0 ]4 Y7 f4 j4 t0 A! N- N[isfile] [nvarchar] (50) NULL$ v1 V( z- D; x! Q9 l+ |
);
$ {( U' S' Q7 v7 I9 Z% ^' N/ n$ C8 P# s9 ~
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)6 l. E6 w. o, f4 Y9 f# Q4 R; c% {
set @root='f:\usr\' -- Start root+ Y" w8 |; s5 I: Z: w" T' p
set @name='cmd.exe' -- Find file! S1 e+ N- x1 A$ S0 f& X5 `6 m
insert into tmp exec master..xp_dirtree @root,0,1--
3 W. p% ]7 V3 R; J0 Q% F' Dset @id=(select top 1 id from tmp where isfile=1 and name=@name)
' y3 k ?: o6 }; B) mset @depth=(select top 1 depth from tmp where isfile=1 and name=@name)# A9 @& T% [7 \; f- b
while @depth<>1 2 m$ R; O3 Y: f+ g) g8 I
begin
" i: ^; X u6 h3 I6 \5 ]set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) 4 n3 _1 m/ Z6 A% n( W
set @depth=(select depth from tmp where id=@id)
$ Z' h+ M, n3 o" A$ Uset @name=(select name from tmp where id=@id)+'\'+@name0 u2 q- _& P# F n0 q1 c# W1 ~
end" |# i" ]$ M; P2 f- I( G# v! }
update tmp set name=@root+@name where id=1; T' Q, v9 n5 [. I: y
select name from tmp where id=1
; j2 v$ ~. c3 M( l3 f3 i8 }' N7 U: s/ @
查找目录的语句( {* J* U: a& b: m. E
5 |. |, a2 q# W1 c7 `6 }( z
9 k) Q+ C( p s. v: fCODE:7 j7 W& i3 R% M( T: y' @
: r H6 ^( H" N/ i) g( |( Z* ?* t+ F7 q3 i; o
drop table tmp;
6 `' y F% K) H# Fcreate table tmp8 l5 P) H: ?9 K0 l; b
(7 e/ _# \; Z2 p4 j% B
[id] [int] IDENTITY (1,1) NOT NULL,1 t% W! s7 m# l
[name] [nvarchar] (300) NOT NULL,: x% _* a4 O' }+ G) ~- t Z
[depth] [int] NOT NULL
7 z4 t1 z; q }! p4 b9 E/ S);9 p& I' A7 e. J# p
; x, P8 k: M: `" L7 I
declare @id int, @depth int, @root nvarchar(300), @name nvarchar(300)
, G0 P- x+ v6 C. R: Mset @root='f:\usr\' -- Start root
6 x* z; i1 F* ?) O0 I. ?. u2 [9 T0 hset @name='donggeer' -- directory to find
2 b4 k- m+ d' ^8 l% h+ ginsert into tmp exec master..xp_dirtree @root,0,0
# J/ N" `# h' W$ iset @id=(select top 1 id from tmp where name=@name)
5 `' \& A; L1 O; p* {set @depth=(select top 1 depth from tmp where name=@name)
0 v& L: H& K e" b7 bwhile @depth<>1
* y# ]5 ~5 h \; U) `, Z; F' ?+ Q6 E2 jbegin 4 j& n1 i% g& E/ f% |, R. Q
set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc)
, Q [. f9 B* h' U: P' I. c. O5 rset @depth=(select depth from tmp where id=@id) $ f7 l8 ?/ g% A; d7 D) I# j
set @name=(select name from tmp where id=@id)+'\'+@name . N% n! [! W( y7 e* j) L
end update tmp set name=@root+@name where id=1% Y0 u# M$ R, X% C# m0 o* `
select name from tmp where id=1 |