找回密码
 加入
搜索
查看: 4591|回复: 13

[系统综合] 【已解决】路径与扩展名问题

 火.. [复制链接]
发表于 2010-12-29 10:58:04 | 显示全部楼层 |阅读模式
本帖最后由 touch_xu 于 2010-12-30 09:42 编辑

己知一个文件的完整路径〈不带引号〉,有什么函数可以快速从路径得到:
1 文件名
2 接展名

譬如c:\abc.txt

要的结果是
1 abc
2 txt
发表于 2010-12-29 11:20:35 | 显示全部楼层
;$spath必须是一个确实的文件
;_getdir($spath) 给出文件路径但不包含文件名
;_getfilename($spath)  给出文件名 $sflag=1时不包含文件类型
;_getfiletype($spath)  给出文件类型
然后调用函数即可
;=======将以下文本"Filegetinfo.au3"文本复制到Include下=============
Func _getdir($spath)
        If StringRegExp($spath, '\\') Then
                $sdir = StringRegExpReplace($spath, '\\[^\\]*$', '')
        Else
                $sdir = ''
        EndIf

        Return ($sdir)
EndFunc   ;==>_getdir

Func _getfilename($spath, $sflag = 0)
        If $sflag = 0 Then
                $sflag = 0
        Else
                $sflag = 1
        EndIf
        If $sflag = 0 Then
                $sFileName = StringRegExpReplace($spath, '.+\\', '')
        Else
                $sFileName = StringRegExpReplace($spath, '.+\\', '')
                $sFileName = StringRegExpReplace($sFileName, '\.[^\.]*$', '')
        EndIf

        Return ($sFileName)
EndFunc   ;==>_getfilename

Func _getfiletype($spath)
        $sFiletype = StringRegExpReplace($spath, '.+\.', '')
        Return ($sFiletype)
EndFunc   ;==>_getfiletype
;================================================
发表于 2010-12-29 11:52:29 | 显示全部楼层
#Include <File.au3>
_PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)

$szPath
要拆分的路径The path to be split (Can contain a UNC server or drive letter)
$szDrive
驱动器字符串
$szDir
目录字符串
$szFName
文件名字符串
$szExt
扩展名字符串
发表于 2010-12-29 12:18:08 | 显示全部楼层
该标记已解决吧呵呵
发表于 2010-12-29 12:18:47 | 显示全部楼层
本帖最后由 netegg 于 2010-12-29 12:21 编辑

#Include <WinAPIEx.au3>
_WinAPI_PathFindFileName 文件名
_WinAPI_PathFindExtension 扩展名
_WinAPI_PathGetDriveNumber 驱动器
_WinAPI_PathGetArgs 参数(通常在命令行中使用)
发表于 2010-12-29 15:01:49 | 显示全部楼层
呵呵,学习了
发表于 2010-12-29 16:04:55 | 显示全部楼层
来个纯手工的
$FullPath = @ScriptFullPath
$FullPath = "\\192.168.0.108\tmp$\新建文件夹 (3)\Autoit_ACN\_____~~~T.au3"
$FullPath = "D:\192.168.0.108\tmp$\新建文件夹 (3)\Autoit_ACN\_____~~~T.au3"

ConsoleWrite("==============>"& $FullPath &@LF ) 
ConsoleWrite("==============>"& _fpGetName($FullPath, 1) &@LF ) 
ConsoleWrite("==============>"& _fpGetName($FullPath, 2) &@LF ) 
ConsoleWrite("==============>"& _fpGetName($FullPath, 3) &@LF ) 
ConsoleWrite("==============>"& _fpGetName($FullPath, 4) &@LF ) 




Func _fpGetName($string, $getname = 4) ; 1.drive / hostIP, 2.dir, 3.filename, 4.ext

        If $getname = 1 Then
                If StringInStr($string,":") = 2 Then 
                        Return StringMid($string, 1, 1)
                ElseIf StringInStr($string,"\") = 1 Then 
                        Return StringMid($string, 1, StringInStr($string, "", 0, 1, 3) - 1 )
                EndIf
        ElseIf $getname = 2 Then 
                Return StringReplace( $String, StringMid( $string, StringInStr($string, "", 0, -1), StringLen($string) ) , "" )                
        ElseIf $getname = 3 Then 
                Return StringReplace( StringMid( $string, StringInStr($string, "", 0, -1)+1, StringLen($string) ) , StringRight( $string, StringLen($string) - StringLen(StringRegExpReplace($string, "\.\w+\Z", "", 1)) ), "" )
        ElseIf $getname = 4 Then 
                Return StringRight( $string, StringLen($string) - StringLen(StringRegExpReplace($string, "\.\w+\Z", "", 1)) - 1 )
        Else
                Return ""
        EndIf

EndFunc
发表于 2010-12-29 19:35:10 | 显示全部楼层
$a="c:\abc.txt"
$exp=StringRegExp($a,'[^\.]+$',3)
MsgBox(0,0,$exp[0])
$fn=StringRegExp($a,'[^\\]+(?=\.[^\\]+$)',3)
MsgBox(0,0,$fn[0])
未判断文件命名的合法性
发表于 2010-12-29 20:42:27 | 显示全部楼层
回复 8# pcbar

无后缀的能出来吗
 楼主| 发表于 2010-12-30 09:41:41 | 显示全部楼层
多谢各位高手,学习了
发表于 2010-12-30 13:36:10 | 显示全部楼层
谢谢!!学习了!
发表于 2011-1-11 12:48:58 | 显示全部楼层
看起来好深奥,深,努力学习中
发表于 2011-1-11 15:30:05 | 显示全部楼层
看起来好深奥,深,努力学习中
发表于 2011-7-18 21:30:53 | 显示全部楼层
路过@$$$$$$$$$
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-3 01:32 , Processed in 0.083313 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表