找回密码
 加入
搜索
查看: 4913|回复: 12

[系统综合] [已解决]求方法:动态检查路径合法性,不管路径是否存在

  [复制链接]
发表于 2015-9-6 18:22:50 | 显示全部楼层 |阅读模式
本帖最后由 bhffhzh 于 2015-9-11 19:24 编辑

综合相关贴子,如下代码似乎可以判断,但我要求是能在输入框动态修改也能判断,一直头疼。

需要如果功能:




  1. 以下代码是通过浏览按钮实现的代码:


  2. $Search1 = FileSelectFolder("请选择一个文件夹:", "", 1)
  3. If StringRegExp($Search1, "[^\x00-\xff]") Or $Search1 = "" Then ;判断路径是否有中文或返回空值
  4. MsgBox(16, "警告", "路径必须指定正确并且不能含中文或特殊字符,请修改后执行!")
  5. Else
  6. GUICtrlSetData($Input1, $Search1)
  7. GUICtrlRead($Input1)
  8. EndIf
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2015-9-7 01:07:06 | 显示全部楼层
实在找不到能识别单个字母的正则方法,只能瞎搞一个出来,应该勉强满足要求的吧
  1. Local $url,$regExp,$URLarry
  2. $url = "C:\Users\hulu\Desktop\AutoIt"
  3. ;~ $url = "C:\Users\hulu\Desktop\AutoIt?%"
  4. ;~ $url = "CcCcC:\Users\hulu\Desktop\AutoIt"
  5. ;~ $url = "123:\Users\hulu\Desktop\AutoIt"
  6. ;~ $url = "ab"

  7. $URLarry = StringSplit($url,":",1)   ;根据路径中都会唯一存在的字符串":"进行分割

  8. For $i=0 To UBound($URLarry)-1    ;只是打印分割后的数组元素而已
  9.         ConsoleWrite($i &"-->"& $URLarry[$i] & @CR)
  10. Next

  11. If UBound($URLarry)=3 Then   ;根据数组元素的个数,判断是否存在":",而断定路径格式是否有错
  12.        
  13.         If StringLen($URLarry[1])=1 And StringRegExp($URLarry[1],"[a-z,A-Z]") Then  ;判断盘符是否为一个英文单词,从而判断盘符的正确性
  14.                 ConsoleWrite("URL的盘符准确。盘符为:"& $URLarry[1] & @CR)
  15.                
  16.                 If StringRegExp($URLarry[2], "[^\x00-\xff]") Or $URLarry[2] = "" Then
  17.                         ConsoleWrite("URL的具体路径错误,路径必须指定正确并且不能含中文或特殊字符。路径为:"& $url & @CR)
  18.                 ElseIf StringRegExp($URLarry[2], "") Then  ;判断路径是否有中文或返回空值
  19.                         ConsoleWrite("URL的具体路径正确。路径为:"& $url & @CR)
  20.                 Else
  21.                         ConsoleWrite("URL的具体路径出现未知情况,请检查。路径为:"& $url & @CR)
  22.                 EndIf
  23.                
  24.         Else
  25.                 ConsoleWrite("URL的盘符出错,请检查。盘符为:"& $URLarry[1] & @CR)
  26.         EndIf
  27.        
  28. Else
  29.         ConsoleWrite("路径格式出错,请检查:"& $url & @CR)
  30. EndIf
复制代码
 楼主| 发表于 2015-9-7 07:17:39 | 显示全部楼层
回复 2# user030


    你这似乎也是静态判断。
 楼主| 发表于 2015-9-7 07:24:48 | 显示全部楼层
本帖最后由 bhffhzh 于 2015-9-7 13:32 编辑

我用一个另类的方法让大家知道我的想法吧。

大家运行就知道了,当然这种方法实在不可取。


  1. #include <ButtonConstants.au3>
  2. #include <EditConstants.au3>
  3. #include <GUIConstantsEx.au3>
  4. #include <WindowsConstants.au3>
  5. #Region ### START Koda GUI section ### Form=
  6. $Form1 = GUICreate("Form1", 623, 442, 192, 124)
  7. $Input1 = GUICtrlCreateInput("c:", 80, 176, 297, 21)
  8. $Button1 = GUICtrlCreateButton("Button1", 336, 280, 89, 49)
  9. GUISetState(@SW_SHOW)
  10. #EndRegion ### END Koda GUI section ###




  11. While 1
  12.         $nMsg = GUIGetMsg()
  13.         Switch $nMsg
  14.                 Case $GUI_EVENT_CLOSE
  15.                          
  16.                         Exit

  17.         EndSwitch

  18.         $Search11 = GUICtrlRead($Input1)
  19.         $hzh = StringMid($Search11, 1, 3) ;提取路径的前三个字符
  20.         $iFileExists1 = FileExists($hzh) ;检查文件或目录是否存在,这里其实是检查诸如是否存在c:盘   d:盘之类的。
  21.         If $iFileExists1 = 0 Or _PathValidateCheck1($Search11, 1) = 0 Or StringRegExp($Search11, "[^\x00-\xff]") Then ;盘符不对,路径含中文,不符合Windows文件命名规则 均不执行。
  22.                 GUICtrlSetState($Button1, $GUI_DISABLE)
  23.                 MsgBox(0,0,"不合法")
  24.         Else
  25.                 GUICtrlSetState($Button1, $GUI_ENABLE)
  26.                 MsgBox(0,0,"合法")
  27.         EndIf

  28.         Sleep(500)
  29. WEnd








  30. ;验证安装目录路径
  31. Func _PathValidateCheck1($Search11, $OnlyAbsolutePath = 0)
  32.         ;afan提示:验证路径是否合法(路径可不存在)。
  33.         ;        本函数可用于验证用户提供的路径是否合法有效。
  34.         ;        与 FileExists 用途不同,FileExists 验证的路径必须存在,而本函数路径可不存在,只要符合Windows文件命名规则即可。
  35.         ;$sPath - 待验证的路径字符串
  36.         ;$OnlyAbsolutePath - 仅验证绝对路径
  37.         ;返回值 - 合法返回1;非法返回0,并设置@Error非0。

  38.         If $OnlyAbsolutePath And Not StringRegExp($Search11, '^[a-zA-Z]:') Then Return SetError(1, 0, 0)
  39.         Local $aSR1 = StringRegExp($Search11, '^(.*?)([^\\/]+)\\?
  40. , 1)
  41.         If @error Then Return SetError(2, 0, 0)
  42.         If StringLen($aSR1[0]) >= 248 Or StringLen($aSR1[1]) >= 260 Then Return SetError(2, 0, 0)
  43.         If StringRegExp($Search11, '[[:cntrl:]]') Then Return SetError(3, 0, 0)
  44.         Local $sIns = '?'
  45.         If $OnlyAbsolutePath Then $sIns = ''
  46.         If StringRegExp($Search11, '^(?:[a-zA-Z]:\\?|([a-zA-Z]:\\)' & $sIns & '(\S[^\\/:*?"<>|]*(?<!\s)\\)*\S[^\\/:*?"<>|]*(?<!\s)\\?)
  47. ) Then Return 1
  48.         Return SetError(4, 0, 0)
  49. EndFunc   ;==>_PathValidateCheck1
复制代码
发表于 2015-9-7 09:02:01 | 显示全部楼层
回复 3# bhffhzh


    看了你下面的代码,_PathValidateCheck1()函数的处理可以试下换成我那种判断方法。
    你目前遇到的难题应该不是动态与静态,是正则的应用不起作用而已,我弄那个正则也花了不少时间,但au3里面的正则对于单个单词的判断几乎没有,我就换其他方法来进行判断路径了,实在不想花费太多时间在一件能采用其他方法可以解决的事情上。
发表于 2015-9-7 09:58:54 | 显示全部楼层
我只会简单的:能创建的,路径就是对的!
  1. #include <ButtonConstants.au3>
  2. #include <EditConstants.au3>
  3. #include <GUIConstantsEx.au3>
  4. #include <WindowsConstants.au3>

  5. $Form1 = GUICreate("Form1", 623, 442, 192, 124)
  6. $Input1 = GUICtrlCreateInput("c:", 80, 176, 297, 21)
  7. $Button1 = GUICtrlCreateButton("Button1", 336, 280, 89, 49)
  8. GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
  9. GUISetState(@SW_SHOW)

  10. While 1
  11.         $nMsg = GUIGetMsg()
  12.         Switch $nMsg
  13.                 Case $GUI_EVENT_CLOSE
  14.                         
  15.                         Exit

  16.         EndSwitch
  17.       
  18. WEnd

  19. Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
  20.         Local $nCode = BitShift($wParam, 16)
  21.         Local $ID = BitAND($wParam, 0x0000FFFF)
  22.         If $ID <>  $Input1 Then Return
  23.         If $nCode = $EN_CHANGE Then
  24.                         ;MsgBox(16, '', '')
  25.            If _PathValidateCheck(GUICtrlRead($Input1))  = 0 Then
  26.                            GUICtrlSetState($Button1, $GUI_DISABLE)
  27.                    Else
  28.                                 GUICtrlSetState($Button1, $GUI_ENABLE)
  29.         EndIf
  30.         EndIf
  31. EndFunc
  32. Func _PathValidateCheck($sPath)
  33.          Local $iRe = 0
  34.      If DirCreate($sPath) Then $iRe = 1
  35.          DirRemove($sPath)
  36.          Return $iRe
  37. EndFunc   ;==>_PathValidateCheck
复制代码
发表于 2015-9-7 10:50:55 | 显示全部楼层
本帖最后由 水木子 于 2015-9-7 11:01 编辑

回复 6# Alam

好思路,学习了!

不过你这个有点小问题需要注意下,假如指定路径已经存在呢!
原本存在的路径会被删除哦!所以还需要优化下。
 楼主| 发表于 2015-9-7 13:26:07 | 显示全部楼层
本帖最后由 bhffhzh 于 2015-9-7 13:28 编辑

回复 5# user030


我要的就是其它方法,我的方法太另类,而且用
while 1
   Sleep(500)
wend

很是占用内存。我说了  这种方法非常不可取。。
 楼主| 发表于 2015-9-7 13:37:48 | 显示全部楼层
本帖最后由 bhffhzh 于 2015-9-7 14:32 编辑

回复 7# 水木子


我问题的关键是如何在输入框手动输入地址后,程序立即就响应地址是否合法。
而不需要通过诸如点击按钮去响应。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2015-9-7 19:30:17 | 显示全部楼层
回复 9# bhffhzh

6楼代码,不就是你要的效果么?
 楼主| 发表于 2015-9-7 21:01:18 | 显示全部楼层
本帖最后由 bhffhzh 于 2015-9-7 21:03 编辑

回复 10# 水木子


我再测试一下。
发表于 2015-9-8 22:32:12 | 显示全部楼层
回复 8# bhffhzh

sorry,我错了
话说 6楼的那种做法LZ验证通过了没?
 楼主| 发表于 2015-9-8 22:44:48 | 显示全部楼层
似乎可以哟,之前没仔细看。这两天有事,空了好生测试。
感谢关注
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2026-9-20 23:00 , Processed in 0.107354 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

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