You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本逻辑返回false时仍更新1.wpl文件问题排查

问题说明

需求:仅当$mp3File.FullName与$currentFileInPlaylist不相等时,在<seq>标签后添加新的媒体行。
问题:即便脚本逻辑判定无文件需要添加(输出“nothing to add”),目标播放列表文件1.wpl仍会被修改。


初始代码

$folderPath = "C:\Users\user\Downloads\"
$playList = "C:\Users\user\Music\Playlists\1.wpl"

# 如果记录脚本上次执行时间的文件存在,获取上次执行后创建的mp3文件并存储到$mp3Files变量
$scriptRunLastTimePath = "$folderPath\1.txt"

if (Test-Path $scriptRunLastTimePath -PathType Leaf) {
    $dateToCompareWith = Get-Content -Path $folderPath\1.txt
    $culture = [System.Globalization.CultureInfo]::InvariantCulture
    $format = 'dd.MM.yyyy HH:mm:ss'
    $storedDateTime = [DateTime]::ParseExact($dateToCompareWith, $format, $culture)
    $mp3Files = @(Get-ChildItem -Path $folderPath -Filter "*.mp3" -File |
            Where-Object { $_.LastWriteTime -gt $storedDateTime })
} else {
    $mp3Files = Get-ChildItem -Path $folderPath -Filter "*.mp3" -File
}

($xml = [xml]::new()).Load($playList)
$currentFilesInPlaylist = $xml.SelectNodes("//seq/*").src

# 如果文件夹中有mp3文件且不在1.wpl中,则在<seq>标签后添加完整文件名到播放列表
foreach ($currentFileInPlaylist in $currentFilesInPlaylist) {
    foreach ($mp3File in $mp3Files) {
        if ($mp3File.FullName -ne $currentFileInPlaylist) {
            (Get-Content $playlist) | Foreach-Object {
                $_ # 输出当前行
                if ($_ -match "<seq>") {
                    # 在匹配的标签后添加行
                    '             <media src="' + $mp3File.FullName + '"/>'
                }
            } | Set-Content $playList -Force
        } else {
            echo "nothing to add"
        }
    }
}

# 创建“检查点”文件:导出脚本上次执行的日期和时间到文件
(Get-Date).ToString('dd/MM/yyyy HH:mm:ss') | Out-File -FilePath $scriptRunLastTimePath

1.wpl文件内容

<?wpl version="1.0"?>
<smil>
    <head>
        <meta name="Generator" content="Microsoft Windows Media Player -- 12.0.22621.1"/>
        <meta name="ItemCount" content="2"/>
        <title>Untitled playlist</title>
    </head>
    <body>
        <seq>
            <media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/>
            <media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3"/>
        </seq>
    </body>
</smil>

编辑版代码(结果无改善)

foreach ($currentFileInPlaylist in $currentFilesInPlaylist) {
    foreach ($mp3File in $mp3Files) {
        if ($mp3File.FullName -eq $currentFileInPlaylist) {
            $updatedPlaylist = $updatedPlaylist -ne $currentFileInPlaylist  # 从待更新列表中移除匹配文件
            echo "nothing to add"
        }
    }
}

foreach ($mp3File in $mp3Files) {
    if ($updatedPlaylist -notcontains $mp3File.FullName) {
        $mediaElement = '<media src="{0}"/>' -f $mp3File.FullName
        (Get-Content -Path $playlist) | ForEach-Object {
            $_
            if ($_ -match "<seq>") {
                $mediaElement
            }
        } | Set-Content -Path $playlist
        Write-Host "Added file '$($mp3File.Name)' to the playlist."
    } else {
        Write-Host "File '$($mp3File.Name)' already exists in the playlist."
    }
}

输出

nothing to add
nothing to add
Added file 'Katzianerova vojna.mp3' to the playlist.
Added file 'Rat i mir u povijesti III- dio.mp3' to the playlist.

编辑2版代码(略有进展)

$filesAdded = $false  # 初始化标记,跟踪是否有文件被添加

foreach ($mp3File in $mp3Files) {
    if ($currentFileInPlaylist -notcontains $mp3File.FullName) {
        $mediaElement = '<media src="{0}"/>' -f $mp3File.FullName
        (Get-Content -Path $playlist) | ForEach-Object {
            $_
            if ($_ -match "<seq>") {
                $mediaElement
                $filesAdded = $true  # 设置标记表示有文件被添加
            }
        } | Set-Content -Path $playlist
        Write-Host "Added file '$($mp3File.Name)' to the playlist."
    } else {
        Write-Host "File '$($mp3File.Name)' already exists in the playlist."
    }
}

if (-not $filesAdded) {
    Write-Host "No files were added to the playlist."
}

输出

Added file 'Katzianerova vojna.mp3' to the playlist.
File 'Rat i mir u povijesti III- dio.mp3' already exists in the playlist.

内容的提问来源于stack exchange,提问作者overflowed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:37:11