# 指定要搜索的目录路径
$directoryPath = "C:\path\to\your\directory"
# 获取该目录下的所有.jpg和.json文件
$jpgFiles = Get-ChildItem -Path $directoryPath -Filter *.jpg
$jsonFiles = Get-ChildItem -Path $directoryPath -Filter *.json | Select-Object -ExpandProperty BaseName
# 遍历.jpg文件并检查是否包含在.json文件列表中
foreach ($jpgFile in $jpgFiles) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($jpgFile.Name)
if ($jsonFiles -notcontains $baseName) {
Remove-Item -Path $jpgFile.FullName -Force
}
}
效果: