如何删除目录中的所有文件?

How to delete all files in a directory?

我的脚本:

#RequireAdmin
FileDelete("C:\Users\Administrator\Desktop\temp\")

我想删除该目录中的所有文件。我也试过了:

#RequireAdmin
DirRemove("C:\Users\Administrator\Desktop\temp\")

但是它不起作用,有什么建议吗?

FileDelete() is FileDelete("filename") ;not only directory!. You can also use wildcards for filename (* and ?) 的语法。

DirRemove() 的工作方式如下:DirRemove ( "path" [, recurse = 0] )。使用 recurse=0(默认),删除文件夹,但前提是它是空的。使用 recurse=1 删除文件和子目录(类似于 DOS DelTree 命令)。

也许你误解了使用的标志:

; Remove only the empty folder "Folder_path"
DirRemove("Folder_Path")

; Remove folder "Folder_Path" with all subfolder and all files within
DirRemove("Folder_Path", 1)

如果这不起作用,那就是系统权限问题。如果要删除文件而不删除包含文件夹:

#include <Files.au3>

; Get all files in folder and delete them:
Local $aFilesInRoot = _FileListToArray("Your_Path", 1, True) ; 1=$FLTA_FILES = Return files only,  True=returns full path
For $i = 1 To $aFilesInRoot[0]
    FileDelete($aFilesInRoot[1])
Next

; Get all subfolders under root and delete them:
Local $aFolderInRoot = _FileListToArray("Your_Path", 2, True) ;2=$FLTA_FOLDERS = Return Folders only
For $i = 1 To $aFolderInRoot[0]
    DirRemove($aFolderInRoot[1], 1)
Next

但是只用一个命令删除所有文件夹后重新制作删除的文件夹不是更容易吗?