找回密码
 加入
搜索
查看: 4433|回复: 10

[AU3基础] 删除指定文件夹下的全部文件(子目录以及文件)(已解决)

 火.. [复制链接]
发表于 2011-1-12 08:50:30 | 显示全部楼层 |阅读模式
本帖最后由 dearmb 于 2011-1-13 17:15 编辑

以前用批处理,保留文件夹本身,删除文件夹以下的全部文件,
del   /q    D:\1\*.*
但在AU3里
FileDelete("D:\1\*.*")只删除D:\1下的文件,文件夹却不删除
DirRemove("D:\1",1)会把D:\1整个都删除,包括D:\1
DirRemove("D:\1",0) 0 = (默认)不删除文件及子目录,没明白怎么用,一直返回0,失败

问一下有什么办法呢?
发表于 2011-1-12 09:35:26 | 显示全部楼层
;这样呢?
Run(@ComSpec & " /c " & 'del   /q    D:\1\*.*', "", @SW_HIDE)
 楼主| 发表于 2011-1-12 09:51:03 | 显示全部楼层
这样可以,但是没有返回值啊
发表于 2011-1-12 11:52:50 | 显示全部楼层
回复 1# dearmb
方法有点笨,但是能实现你要的功能呃!偶菜呃
#Include <Date.au3>
#Include <File.au3>
$_Path='C:\temp\'
_FileFolders($_Path);===>扫描$_Path。
Func _FileFolders($_Path)
        $File = _FileListToArray($_Path, "*", 0)
        If Not IsArray($File) Then Return 0
        For $0 = 1 To $File[0]
                If StringInStr(FileGetAttrib($_Path&$File[$0]),'d',2) Then
                        If DirRemove($_Path&$File[$0],1) Then
                                FileWrite('Del.log',_NowCalc()&'        Del '&$File[$0]& 'successful!'&@CRLF)
                        Else
                                FileWrite('Del.log',_NowCalc()&'        Del '&$File[$0]& 'Failed!'&@CRLF)
                        EndIf
                Else
                        If FileDelete($_Path&$File[$0]) Then
                                FileWrite('Del.log',_NowCalc()&'        Del '&$File[$0]& 'successful!'&@CRLF)
                        Else
                                FileWrite('Del.log',_NowCalc()&'        Del '&$File[$0]& 'Failed!'&@CRLF)
                        EndIf       
                EndIf
        Next
EndFunc
发表于 2011-1-12 12:44:28 | 显示全部楼层
高手啊,学习了!
发表于 2011-1-12 13:46:03 | 显示全部楼层
我这个是脱裤子打屁,其实用水木兄的代码实现最简单。
这段代码只是纯AU3而已。
_filedelete(@ScriptDir&"\backup",'*.jpg',0)
ShellExecute(@ScriptDir&"\backup")

Func _filedelete($path, $ext = '*.*', $flag = 0);_filedelete(路径,[[文件类型],0=不包括子目录,1=包括子目录(当包括子目录时只能用*.*)])
        Local $reulst,$abc
        If StringRight($path, 1) <> "\" Then $path = $path & "\"
        If $flag = 0 Then
                $reulst = FileDelete($path&$ext)
                Return $reulst
        ElseIf $flag = 1 And $ext = '*.*' Then
                $search = FileFindFirstFile($path&$ext)
                If $search = -1 Then
                        Return SetError(2)
                EndIf
                While 1
                        $file = FileFindNextFile($search)
                        If @error Then ExitLoop
                        $abc&=$path&$file&"|"
                        If FileGetAttrib($path&$file)='D' Then 
                                DirRemove($path&$file,1)
                        Else
                                FileDelete($path&$file)
                        EndIf
                WEnd
                FileClose($search)
        EndIf
EndFunc   ;==>_filedelete

评分

参与人数 1金钱 +50 收起 理由
水木子 + 50 学习啦!

查看全部评分

发表于 2011-1-12 13:49:12 | 显示全部楼层
回复 6# 3mile

呵呵!我刚开始就是想这样的,不过感觉太麻烦了所以没做。
嘿嘿!3G真是有心,向你学习!
发表于 2011-1-12 16:50:51 | 显示全部楼层
回复 7# 水木子
水木兄谦虚了,我那只是在脱裤子打屁而已。哈哈
发表于 2011-1-12 22:57:08 | 显示全部楼层
我碰到这样的问题,以前的做法是连文件夹全删了,然后再建一个文件夹。学习了。保存到函数库中吧!
 楼主| 发表于 2011-1-13 07:01:05 | 显示全部楼层
我碰到这样的问题,以前的做法是连文件夹全删了,然后再建一个文件夹。学习了。保存到函数库中吧!
gzh888666 发表于 2011-1-12 22:57


我也想了,但是权限要全重加啊,更麻烦
发表于 2011-1-13 08:07:44 | 显示全部楼层
本帖最后由 netegg 于 2011-1-13 08:19 编辑
Func delete($current)
  Local $search = FileFindFirstFile($current & "\*.*")
  While 1
     Dim $file = FileFindNextFile($search)
     If @error Or StringLen($file) < 1 Then ExitLoop
     If StringInStr(FileGetAttrib($current & "" & $file), "D") And ($file <> "." Or $file <> "..") Then
       #cs;此部分用于修改属性
       $attrib = FileGetAttrib($current & "" & $file)
       $aAttrib = StringSplit($attrib, '', 2)
       For $i = 0 To UBound($aAttrib)
          $attrib &= '-' & $aAttrib[$i]
       Next
       FileSetAttrib($current & "" & $file, $attrib & '+N', 1)
        #ce
        Dirremove($current & "" & $file, 1)
     EndIf
     delete($current & "" & $file)
   WEnd
;   DirRemove($current, 1)  ;去掉此行会保留一个父文件夹
EndFunc   ;==>delete

评分

参与人数 1金钱 +50 收起 理由
水木子 + 50 学习!

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-2 16:28 , Processed in 0.094246 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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