"$_.extension -eq" not working as intended?
I tried to write a small script to automate the creation of playlists (m3u) for dozens of folders/subfolders of mp3/mp4 files, while omitting various other misc files therein. I know very little about Powershell but managed to piece together something that almost works. The only blip is that when I use "$_.extension -eq", it doesn't seem to work, or at least I'm not using it right. If I use it to match log/txt files in a temp folder for example,开发者_运维技巧 it works, but not in this instance. Here is the code -
$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
}
I have tried several variations like swapping "-match" for "-eq" but no luck. incidentally, if I omit the "-or ".mp4"" from the parentheses then it works fine, but I need it to match both, and only both mp3/mp4.
Thanks in advance.
As far as you complain about extension, let's start with it. Presumably there is a bug in the code; this expression/syntax is technically valid:
$_.extension -eq ".mp3" -or ".mp4"
But apparently the intent was:
$_.extension -eq ".mp3" -or $_.extension -eq ".mp4"
Try the corrected expression at first.
I'm going to add this as a shortcut option:
gci | Where-Object {".mp3",".mp4" -eq $_.extension}
精彩评论