繁体   English   中英

如何使用PowerShell在所有空文件夹中创建空文件?

[英]How to create empty files in all empty folders using PowerShell?

我想在所有空的子文件夹中创建空的文本文件。 下面的脚本将列出所有空的子文件夹。

$a = Get-ChildItem D:\test -recurse | Where-Object {$_.PSIsContainer -eq $True}
$a | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName

如何遍历上述命令的输出并在其中创建空文本文件?

您为自己做出的答案中有很多循环。 我提供了此解决方案,该解决方案将在所有没有文件的目录中创建一个空文件(从您的解决方案中可以找到是否有文件夹,所以我会保持这种逻辑。)

Get-ChildItem -Recurse C:\temp | 
        Where-Object {$_.PSIsContainer -and ($_.GetFiles().Count -eq 0)} | 
        ForEach-Object{[void](New-Item -Path $_.FullName -Name "Touch.txt" -ItemType File)}

如果$_.PSIsContainer为false,那么它将不会受到其他检查文件条件的困扰。 还将New-Item的输出New-Item为void以停止成功创建所有这些新文件的输出。

以下代码对我有用。

$a = Get-ChildItem D:\test -recurse | Where-Object {$_.PSIsContainer -eq $True}
$path = $a | Where-Object {$_.GetFiles().Count -eq 0} | foreach {$_.FullName}
$a | Where-Object {$_.GetFiles().Count -eq 0} | foreach {$_.FullName}|ForEach-Object -Process {New-Item -Path $path -Name "testfile.txt" -Value "Test Value" -ItemType File }

如何确保..这不会在目录中创建文件,该目录中包含子目录。

即,应在没有文件和子目录的目录中创建文件

这对我有用..仅当目录既没有子目录也没有文件时,它才创建文件

$a = Get-ChildItem C:\tejasoft -recurse | Where-Object {$_.PSIsContainer -eq $True -and ($_.GetDirectories().Count -eq 0)}
$path = $a | Where-Object {$_.GetFiles().Count -eq 0} | foreach {$_.FullName}
$a | Where-Object {$_.GetFiles().Count -eq 0} | foreach {$_.FullName}|ForEach-Object -Process {New-Item -Path $path -Name ".keep" -Value "Test Value" -ItemType File }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM