假如文件路径和全名在str="c:\winnt\aa.txt"里?用如下代码: dim driv as string dim fold as string dim fname as string dim tmpfold as string sub find() dim i as integer i=instr(str,"\") driv=left(str,i-1) str=right(str,len(str)-i) do while instr(str,"\")<>0 i=instr(str,"\") fold=fold & left(str,i-1) str=right(str,len(str)-i) loop file=right(str,len(str)-i) end sub
如果可以的话,用split函数试试. Dim a As Variant Dim s As String Dim i As Long
s = "c:\abc\efg\h.txt" a = Split(s, "\") For i = 0 To UBound(a, 1) MsgBox a(i) Next 结果: a(0)="c:" a(1)="abc" a(2)="efg" a(3)="h.txt"
谢谢大家~~我的问题己经解决了根据大家的提示。。代码如下。 Private Sub Command5_Click() Dim a As Variant Dim s As String Dim i As Long Dim b As String s = "C:\XXA\SKINS\001.BMP" a = Split(s, "\")
For i = 0 To UBound(a, 1) a(i) = a(i) Next MsgBox a(0) & "|" & a(UBound(a)) '显示如下a(0)提取为C: '显示如下a(ubound(a))001.BMP
For i = 1 To UBound(a, 1) - 1 b = b & "\" & a(i) Next
学会用FSO,专门有方法去的所有文件、文件夹、路径、以及相关的删除、新建等操作的
http://www.csdn.net/expert/topic/666/666906.xml?temp=.9986231
dim driv as string
dim fold as string
dim fname as string
dim tmpfold as string
sub find()
dim i as integer
i=instr(str,"\")
driv=left(str,i-1)
str=right(str,len(str)-i)
do while instr(str,"\")<>0
i=instr(str,"\")
fold=fold & left(str,i-1)
str=right(str,len(str)-i)
loop
file=right(str,len(str)-i)
end sub
Dim a As Variant
Dim s As String
Dim i As Long
s = "c:\abc\efg\h.txt"
a = Split(s, "\") For i = 0 To UBound(a, 1)
MsgBox a(i)
Next
结果:
a(0)="c:"
a(1)="abc"
a(2)="efg"
a(3)="h.txt"
我明白你的意思~~可是如何的把字符提取出来呢? 比方说~~把C:提取到DRIVE1控件。把\XXA\SKINS\"提取到DIR1控件,把001。BMP提取到FILE1控件。。(而且可以任意的提取‘各种路径’下的字符。。
分割得到数组:
c:
aaa
bbb
ccc
ddd.bmp然后将最后一个值放到file控件,第一个值放到dir控件,中间的重新组合成
\aaa\bbb\ccc\
Private Sub Command5_Click()
Dim a As Variant
Dim s As String
Dim i As Long
Dim b As String
s = "C:\XXA\SKINS\001.BMP"
a = Split(s, "\")
For i = 0 To UBound(a, 1)
a(i) = a(i)
Next
MsgBox a(0) & "|" & a(UBound(a))
'显示如下a(0)提取为C:
'显示如下a(ubound(a))001.BMP
For i = 1 To UBound(a, 1) - 1
b = b & "\" & a(i)
Next
MsgBox b
'显示如下\XXA\SKins
End Sub
再次的感谢大家。。