Public Function GetFileName(ByVal FullPathName As String, Optional ByVal NoExtName As Boolean = False) As String
'FullPathName指定全路径
'返回值:包含的文件名
Dim I As Long, J As Long
Dim filename As String, FileNameNoExt As String
FullPathName = Trim(FullPathName)
I = InStrRev(FullPathName, "\")
J = Len(FullPathName)
If I = 0 Then Exit Function
filename = Mid(FullPathName, I + 1, J - I)
I = InStrRev(filename, "")
J = Len(filename)
If I = 0 Then Exit Function
FileNameNoExt = Mid(filename, 1, I - 1)
If NoExtName = True Then
GetFileName = FileNameNoExt
Else
GetFileName = filename
End If
End Function
这篇文章主要介绍了Linux系统中获取路径的文件名的方法,文中总结出了两条,需要的朋友可以参考下
代码如下:
[root@dabuinfo
]#basename
/root/aaa/bbb/dabutxt
显示:
代码如下:
dabutxt
#获取路径的文件名
shell脚本中如何获得脚本文件所在路径
方法一:
代码如下:
[root@dabuinfo
]#DIR=$(cd
"$(dirname
"$0")";
pwd)
[root@dabuinfo
]#echo
$DIR
但是像这种dirname
"$0"这种写法,在遇到source命令时会得到错误的结果。
方法二:
代码如下:
[root@dabuinfo
]#echo
"$(
cd
"$(
dirname
"${BASH_SOURCE[0]}"
)"
&&
pwd
)"
上面一行命令可以获得脚本的绝对轮径,无论你在何处调用这个脚本。
但是如果含有软链接,就无法使用了。所以,我们为了能正确解析指向脚本的软链接,可以使用下面的多行命令:
代码如下:
SOURCE="${BASH_SOURCE[0]}"
while
[
-h
"$SOURCE"
];
do
#
resolve
$SOURCE
until
the
file
is
no
longer
a
symlink
DIR="$(
cd
-P
"$(
dirname
"$SOURCE"
)"
&&
pwd
)"
SOURCE="$(readlink
"$SOURCE")"
[[
$SOURCE
!=
/
]]
&&
SOURCE="$DIR/$SOURCE"
#
if
$SOURCE
was
a
relative
symlink,
we
need
to
resolve
it
relative
to
the
path
where
the
symlink
file
was
located
done
DIR="$(
cd
-P
"$(
dirname
"$SOURCE"
)"
&&
pwd
)"
也可与source,bash
-c命令使用
但是,如果你在脚本中使用先cd切换到其他目录,在运行时上面的命令片段时,则上面的命令不能等到正确的结果。可以参考关于$CDPATH
陷阱的文章。想理解它如何其作用的,可以运行下面的代码:
代码如下:
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while
[
-h
"$SOURCE"
];
do
#
resolve
$SOURCE
until
the
file
is
no
longer
a
symlink
TARGET="$(readlink
"$SOURCE")"
if
[[
$SOURCE
==
/
]];
then
echo
"SOURCE
'$SOURCE'
is
an
absolute
symlink
to
'$TARGET'"
SOURCE="$TARGET"
else
DIR="$(
dirname
"$SOURCE"
)"
echo
"SOURCE
'$SOURCE'
is
a
relative
symlink
to
'$TARGET'
(relative
to
'$DIR')"
SOURCE="$DIR/$TARGET"
#
if
$SOURCE
was
a
relative
symlink,
we
need
to
resolve
it
relative
to
the
path
where
the
symlink
file
was
located
fi
done
echo
"SOURCE
is
'$SOURCE'"
RDIR="$(
dirname
"$SOURCE"
)"
DIR="$(
cd
-P
"$(
dirname
"$SOURCE"
)"
&&
pwd
)"
if
[
"$DIR"
!=
"$RDIR"
];
then
echo
"DIR
'$RDIR'
resolves
to
'$DIR'"
fi
echo
"DIR
is
'$DIR'"
把以下代码复制到代码窗口,
要获取文件名时,
用这样的语句:
msgbox
GetPathFromFileName("C:\windows\texttxt")
代码如下:
'
'函数:
GetPathFromFileName
'作用:
从完整路径获取
文件名
'输入:
完整路径,
目录分隔符
'返回:
文件名(带扩展名)
Public
Function
GetPathFromFileName(ByVal
strFullPath
As
String,
Optional
ByVal
strSplitor
As
String
=
"\")
As
String
GetPathFromFileName
=
Left$(strFullPath,
InStrRev(strFullPath,
strSplitor,
,
vbTextCompare))
End
Function
以上就是关于VB 从全路径中获取文件名全部的内容,包括:VB 从全路径中获取文件名、Linux系统中获取路径的文件名的方法、vb如何从全路径中获取文件名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)