powershell - "$_.extension -eq"没有按预期工作?

标签 powershell

我尝试编写一个小脚本来自动为数十个 mp3/mp4 文件的文件夹/子文件夹创建播放列表 (m3u),同时省略其中的各种其他杂项文件。我对 Powershell 知之甚少,但设法拼凑出一些几乎可以工作的东西。唯一的问题是,当我使用“$_.extension -eq”时,它似乎不起作用,或者至少我没有正确使用它。例如,如果我用它来匹配临时文件夹中的 log/txt 文件,它可以工作,但在这种情况下不行。这是代码 -

$pathname = read-host "Enter path"
$root = Get-ChildItem $pathname | ? {$_.PSIsContainer}
$rootpath = $pathname.substring(0,2)
Set-Location $rootpath
Set-Location $pathname
foreach($folder in $root) {
Set-Location $folder
foreach($file in $folder) {
$txtfile =".m3u"
$files = gci | Where-Object {$_.extension -eq ".mp3" -or ".mp4"}
$count = $files.count
if($count -ge 2){
$txtfile = "_" + $folder.name + $txtfile
Add-Content $txtFile $files
}  
}
if(test-path $txtFile){
Add-Content $txtFile `r
}
Set-Location $pathname
}

我尝试了几种变体,例如将“-match”换成“-eq”,但没有运气。顺便说一句,如果我从括号中省略“-或”.mp4“”,那么它可以正常工作,但我需要它来匹配两者,并且只匹配 mp3/mp4。

提前致谢。

最佳答案

至于你提示扩展,让我们从它开始。大概是代码有bug;这个表达式/语法在技术上是有效的:

$_.extension -eq ".mp3" -or ".mp4"

但显然意图是:
$_.extension -eq ".mp3" -or $_.extension -eq ".mp4"

首先尝试更正的表达式。

关于powershell - "$_.extension -eq"没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4100505/

相关文章:

powershell - 如何确定执行函数的文件名

powershell - 如何在 Powershell 中列出 .zip 文件的内容?

powershell - 使用DirectorySearcher的Powershell LDAP过滤器

email - Powershell-编写来自Exchange的电子邮件的脚本

powershell - Active Directory用户登录和注销 session 历史记录

powershell - 如何从 Powershell 设置 TeamCity 构建状态?

powershell - Powershell中的原型(prototype)继承?

powershell - 使用 powershell 将文本文件中的特定行复制到单独的文件

powershell - 批量编写和执行Powershell脚本

powershell - 从字符串中提取文本到文档末尾