找回密码
 加入
搜索
查看: 6120|回复: 20

[AU3基础] 如何读取上级目录名?

 火.. [复制链接]
发表于 2012-6-7 01:16:22 | 显示全部楼层 |阅读模式
如何读取上级目录名?  不需要完整路径!
发表于 2012-6-7 07:31:30 | 显示全部楼层
本帖最后由 user3000 于 2012-6-7 19:03 编辑

回复 1# qq4045728
好像很多人感兴趣了的样子, 增加点检测非法路径的
是这个意思吗?
$a = 'D:\AutoIt3SVN\Examples\GUI\Advanced\'
$aa='D:\AutoIt3SVN\Include'
$a = _Get_Parent_directory($a)
$aa = _Get_Parent_directory($aa)
MsgBox(0, $a, $aa)

Func _Get_Parent_directory($dir)
   If Not StringRegExp($dir, '^[a-zA-Z]:\\[^\\]+') Then Return SetError(1, 0, '给出的路径不合法!')
   Return StringRegExpReplace($dir, '.+\\([^\\]+)\\[^\\]+\\?$', '\1')
EndFunc
发表于 2012-6-7 07:40:39 | 显示全部楼层
#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)
$Spath = StringTrimRight($TestPath[1] & $TestPath[2], 1)
$tag = StringSplit($Spath, "\\")
If @error = 1 Then
        MsgBox(0, "警告", "当前已为根目录 " & $Spath)
        Exit
EndIf
$SecondPath = StringTrimRight($Spath, StringLen($tag[$tag[0]]))
MsgBox(0, "上一级目录", $SecondPath)
发表于 2012-6-7 08:35:26 | 显示全部楼层
本帖最后由 netegg 于 2012-6-7 09:27 编辑

[au3]MsgBox(0, 0, $path & @CRLF & Stringleft($path, StringInStr($path, '\', 0, -1 - StringInStr(StringRight($path, 1), '\')) -1))[/au3]
发表于 2012-6-7 08:41:24 | 显示全部楼层
进来膜拜楼上几位大牛!!
发表于 2012-6-7 08:56:49 | 显示全部楼层
回复 4# netegg
瞧这思路,不佩服不行。学习了。
发表于 2012-6-7 09:20:14 | 显示全部楼层
本帖最后由 shqf 于 2012-6-7 09:34 编辑

蛮有意思的一题,学正则不久,也用正则试一下:
$path='D:\AutoIt3SVN\Examples\GUI\Advanced'
;$path = 'D:\'
$pPath = StringRegExp($path, "\\([^\\]+)\\[^\\]+\\*$", 3)
If @error Then
        MsgBox(0, 0, StringLeft($path,3))
Else
        MsgBox(0, 0, $pPath[0])
EndIf
细看,基本同一楼了。唉,思路展不开。
发表于 2012-6-7 09:45:06 | 显示全部楼层
方法都不错。学习。
发表于 2012-6-7 09:57:43 | 显示全部楼层
本帖最后由 kevinch 于 2012-6-7 09:59 编辑
$str="D:\personal documents\WebInfo.txt"
$arr=StringSplit($str,"")
MsgBox(0,"",$arr[$arr[0]-1])
依路径分隔符stringsplit后倒数第二个就可以了
发表于 2012-6-7 10:16:28 | 显示全部楼层
楼上的思路不错啊,不过如果是“'D:\AutoIt3SVN\Examples\GUI\Advanced\”的情况要出错。斗胆插一句$str=StringTrimRight($str,1)在第2 行如何?。
发表于 2012-6-7 10:44:36 | 显示全部楼层
$str="D:\AutoIt3SVN\Examples\GUI\Advanced"
If StringRight($str,1)=="" Then $str=StringLeft($str,StringLen($str)-1)
If StringInStr($str,"")>0 then
        $arr=StringSplit($str,"")
        MsgBox(0,"",$arr[$arr[0]-1])
Else
        MsgBox(0,"","只有一层目录啦!")
EndIf
提供个思路,你还非得逼我完善
 楼主| 发表于 2012-6-7 11:08:43 | 显示全部楼层
哈哈。非常感谢各位仁兄!
 楼主| 发表于 2012-6-7 11:08:50 | 显示全部楼层
哈哈。非常感谢各位仁兄!
发表于 2012-6-7 17:49:32 | 显示全部楼层
本帖最后由 netegg 于 2012-6-7 18:11 编辑

惭愧,是上级目录不是路径呀
[au3]local $path = 'c:\1\2'
if stringright($path, 1) = '\' then $path = stringtrimright($path, 1)
msgbox(0,0,stringmid($path, stringinstr($path, '\', 0, -2)+1, stringinstr($path, '\', 0, -1)- StringInStr($path, '\', 0, -2)-1))[/au3]
发表于 2012-6-7 21:14:28 | 显示全部楼层
惭愧,是上级目录不是路径呀
netegg 发表于 2012-6-7 17:49

老兄,不知道我这样写行不行?
Local $a=StringSplit("c:\1\2\3","")
MsgBox(0,"路径",$a[3])
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-3 14:50 , Processed in 0.086098 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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