Powershell测试文件夹是否为空
在Powershell中,如何测试目录是否为空?如果您对隐藏文件或系统文件不感兴趣,也可以使用测试路径 要查看目录Powershell测试文件夹是否为空,powershell,Powershell,在Powershell中,如何测试目录是否为空?如果您对隐藏文件或系统文件不感兴趣,也可以使用测试路径 要查看目录\temp中是否存在文件,可以使用: Test-Path -Path .\temp\* 或者很快: Test-Path .\temp\* 试试这个 $directoryInfo = Get-ChildItem C:\temp | Measure-Object $directoryInfo.count #Returns the count of all of the objects
\temp
中是否存在文件,可以使用:
Test-Path -Path .\temp\*
或者很快:
Test-Path .\temp\*
试试这个
$directoryInfo = Get-ChildItem C:\temp | Measure-Object
$directoryInfo.count #Returns the count of all of the objects in the directory
如果
$directoryInfo.count-eq 0
,则您的目录为空。仅添加到JPBlanc,如果目录路径为$DirPath,则此代码也适用于包含方括号字符的路径
# Make square bracket non-wild card char with back ticks
$DirPathDirty = $DirPath.Replace('[', '`[')
$DirPathDirty = $DirPathDirty.Replace(']', '`]')
if (Test-Path -Path "$DirPathDirty\*") {
# Code for directory not empty
}
else {
# Code for empty directory
}
一行:
if( (Get-ChildItem C:\temp | Measure-Object).Count -eq 0)
{
#Folder Empty
}
为了防止枚举c:\Temp下的每个文件(这可能很耗时),我们可以执行以下操作:
if((Get-ChildItem c:\temp\ -force | Select-Object -First 1 | Measure-Object).Count -eq 0)
{
# folder is empty
}
获取所有文件和目录,然后只计算它们以确定目录是否为空,这是一种浪费。使用.NET
枚举文件系统更好
$directory = Get-Item -Path "c:\temp"
if (!($directory.EnumerateFileSystemInfos() | select -First 1))
{
"empty"
}
简单方法
if (-Not (Test-Path .\temp*)
{
#do your stuff here
}
如果需要,可以删除-Not
,在文件存在时输入“if”删除空文件夹的示例:
IF ((Get-ChildItem "$env:SystemDrive\test" | Measure-Object).Count -eq 0) {
remove-Item "$env:SystemDrive\test" -force
}
从Get ChildItem获取计数可能会提供错误的结果,因为空文件夹或访问文件夹时出错可能会导致计数为0
检查空文件夹的方法是分离错误:
Try { # Test if folder can be scanned
$TestPath = Get-ChildItem $Path -ErrorAction SilentlyContinue -ErrorVariable MsgErrTest -Force | Select-Object -First 1
}
Catch {}
If ($MsgErrTest) { "Error accessing folder" }
Else { # Folder can be accessed or is empty
"Folder can be accessed"
If ([string]::IsNullOrEmpty($TestPath)) { # Folder is empty
" Folder is empty"
}
}
上面的代码首先尝试访问该文件夹。如果发生错误,则输出发生错误。如果没有错误,请说明“文件夹可以访问”,然后检查它是否为空。在研究了一些现有答案并进行了一些实验后,我最终使用了以下方法:
function Test-Dir-Valid-Empty {
param([string]$dir)
(Test-Path ($dir)) -AND ((Get-ChildItem -att d,h,a $dir).count -eq 0)
}
这将首先检查有效目录(测试路径($dir)
)。然后,它将分别根据属性d
、h
和a
检查任何内容,包括任何目录、隐藏文件或“常规”文件**
用法应该足够清楚:
PS C_\> Test-Dir-Valid-Empty projects\some-folder
False
……或者:
PS C:\> if(Test-Dir-Valid-Empty projects\some-folder){ "empty!" } else { "Not Empty." }
Not Empty.
**实际上,我不能100%确定a
的定义效果是什么,但它在任何情况下都会导致包含所有文件。文档说明ah
显示隐藏文件,我认为as
应该显示系统文件,所以我猜a本身只显示“常规”文件。如果从上面的函数中删除它,它在任何情况下都会找到隐藏文件,但不会找到其他文件。您可以使用方法.GetFileSystemInfos().Count
检查目录的计数
通过使用GetFileSystemFos().Count
和测试,一行用于管道:
gci -Directory | where { !@( $_.GetFileSystemInfos().Count) }
将显示所有没有项目的目录。结果:
Directory: F:\Backup\Moving\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/21/2021 2:53 PM Test [Remove]
d----- 5/21/2021 2:53 PM Test - 1
d----- 5/21/2021 2:39 PM MyDir [abc]
d----- 5/21/2021 2:35 PM Empty
我之所以发布这篇文章,是因为我对包含方括号的名称存在边缘大小写问题[]
;失败是在使用其他方法时,输出管道传输到删除项
缺少带括号的目录名。为什么要删除?谢谢你的解释!这是为了将结果转换为布尔值。被否决的人没有解释原因。向上投票。@SpellingD:我希望一直都是这样。:)+1对于这种简洁的检查方式。这应该是可以接受的答案。顺便说一句,你甚至可以做测试路径。\temp\*
(没有-Path
)。有人知道这是否仍然会在整个glob中重复出现吗?或者它遇到任何问题时会停止吗?不确定是否理解您的问题,因为如果temp中存在目录,temp将不再被视为空。@kenny它不应该递归。它只需要签入单个目录。最多,它应该列出该文件的全部内容。您也可以使用:if(-Not(testpath.\temp*){}进行否定默认情况下,gci
不会显示隐藏文件,因此您需要使用-force
参数来确保目录确实为空。我们必须找到每个文件吗?这可能会很耗时。@拼写会失败,因为文件夹确实为空,但使用-force
将打开桌面.ini
隐藏系统文件“directoryInfo.count”不返回目录中所有文件的计数,而是返回目录中所有文件和子目录的计数。因此,如果您有0个文件,但有1个子文件夹,则返回1。@Benjamin为了清晰起见进行了编辑。如果您只想检查目录中是否没有文件,请添加-file
参数以获取子项,例如$directoryInfo=Get ChildItem C:\temp-file | Measure Object
这是迄今为止最快的解决方案,因为它不会枚举所有文件,只会在找到的第一个文件时停止。要检查文件夹中是否存在文件,请使用(Get ChildItem-LiteralPath's:\Test\'-file-Force | Select Object-first 1 | Measure Object).Count-ne 0
而不是测试路径的:\Test\*'-PathType Leaf
。除非您在PowerShell v1上,否则度量对象不会在此处添加任何内容。虽然我当然想鼓励您发布StackOverflow,G,但您有意或无意地忽略了一些“最佳实践”。(a)一般来说,你不应该只发布代码而不做解释。(b)你不应该重新发布其他人给出的相同答案(如果你滚动浏览这里的答案,你的答案至少给出了两次)。(c)你不应该包含一大堆不相关的代码——你90%的代码与问题无关。我不打算投反对票,只是希望你做好准备,以防出现反对票。请用英语写下你的答案,因为。如果你在get ChildI中使用-Path$\FullName
,你可能会遇到严重的问题请使用!!!
PS C_\> Test-Dir-Valid-Empty projects\some-folder
False
PS C:\> if(Test-Dir-Valid-Empty projects\some-folder){ "empty!" } else { "Not Empty." }
Not Empty.
$docs = Get-ChildItem -Path .\Documents\Test
$docs.GetFileSystemInfos().Count
gci -Directory | where { !@( $_.GetFileSystemInfos().Count) }
Directory: F:\Backup\Moving\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/21/2021 2:53 PM Test [Remove]
d----- 5/21/2021 2:53 PM Test - 1
d----- 5/21/2021 2:39 PM MyDir [abc]
d----- 5/21/2021 2:35 PM Empty