使用-Recurse和-Force删除项目不会删除所有文件和文件夹

时间:2016-05-27 21:42:09

标签: powershell

在Powershell中我有一个脚本,它应该清除从TFS检索的项目文件的文件夹。

命令很简单:

$deletePath = 'c:\Compile\*'
Remove-Item $deletePath -Recurse -Force

在运行之前,c:\ Compile中有大约150个文件夹,之后剩下3个文件夹。

该脚本生成5个错误

Remove-Item : Cannot remove item C:\Compile\DatabaseObjects: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\IFIFWCHG\obj: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\IFIFWCHG: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\PrecompiledWeb\IIS: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\PrecompiledWeb: The directory is not empty.

我想因为使用-Force它处理了目录中的文件/文件夹。

有趣的是,脚本运行后,DatabaseObjects文件夹为空,因此在删除文件之前似乎正在尝试删除该文件夹。

1 个答案:

答案 0 :(得分:2)

尝试使用Get-ChildItem cmdlet检索所有项目,并使用ForEach-Object循环迭代它们并删除。

WIFEXITED(status)
    returns true if the child terminated normally, that is, by
    calling exit(3) or _exit(2), or by returning from main().

WEXITSTATUS(status)
    returns the exit status of the child. This consists of the
    least significant 8 bits of the status argument that the child
    specified in a call to exit(3) or _exit(2) or as the argument
    for a return statement in main(). This macro should only be
    employed if WIFEXITED returned true.