如何创建 'Missing' 个文件列表
How to Create a 'Missing' File(s) List
我正在尝试编译两个 folder/directories 之间丢失的文件列表,其中 text/image 个文件具有相同的基本名称。
# file directory/folder with .txt files
$filesPathText = "C:\test\test3"
# file directory/folder with .JPG files
$filesPathImage = "C:\test\test4"
内容:
Copy of 0002.txt
Copy of 0003.txt
Copy of 0004.txt
Copy of 0006.txt
Copy of 0002.jpg
Copy of 0003.jpg
Copy of 0004.jpg
Copy of 0005.jpg
Copy of 0006.jpg
我想输出 'missing' 文件是:Copy of 0005.txt
我试过这种东西:
$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage
$result = $images | Where-Object{$texts -notcontains $images}
$result
对我来说逻辑正确,但结果是所有图像文件的输出。
尽管这是一个简单的示例并且看起来很常见,但我还没有找到已回答的类似问题。
欢迎提出任何建议。
您的 Where-Object
脚本块 正在将 1 个对象数组 ($texts
) 与另一个对象数组 ($images
) 进行比较将 每个对象 ($_
) 与对象数组进行比较。您也没有引用要比较的 属性 (.BaseName
)。
$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage
# missing text files
$images | Where-Object { $texts.BaseName -notcontains $_.BaseName } | ForEach-Object {
$_.BaseName + '.txt'
}
# missing images
$texts | Where-Object { $images.BaseName -notcontains $_.BaseName } | ForEach-Object {
$_.BaseName + '.jpg'
}
我正在尝试编译两个 folder/directories 之间丢失的文件列表,其中 text/image 个文件具有相同的基本名称。
# file directory/folder with .txt files
$filesPathText = "C:\test\test3"
# file directory/folder with .JPG files
$filesPathImage = "C:\test\test4"
内容:
Copy of 0002.txt
Copy of 0003.txt
Copy of 0004.txt
Copy of 0006.txt
Copy of 0002.jpg
Copy of 0003.jpg
Copy of 0004.jpg
Copy of 0005.jpg
Copy of 0006.jpg
我想输出 'missing' 文件是:Copy of 0005.txt
我试过这种东西:
$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage
$result = $images | Where-Object{$texts -notcontains $images}
$result
对我来说逻辑正确,但结果是所有图像文件的输出。
尽管这是一个简单的示例并且看起来很常见,但我还没有找到已回答的类似问题。
欢迎提出任何建议。
您的 Where-Object
脚本块 正在将 1 个对象数组 ($texts
) 与另一个对象数组 ($images
) 进行比较将 每个对象 ($_
) 与对象数组进行比较。您也没有引用要比较的 属性 (.BaseName
)。
$texts = Get-ChildItem -Path $filesPathText
$images = Get-ChildItem -Path $filesPathImage
# missing text files
$images | Where-Object { $texts.BaseName -notcontains $_.BaseName } | ForEach-Object {
$_.BaseName + '.txt'
}
# missing images
$texts | Where-Object { $images.BaseName -notcontains $_.BaseName } | ForEach-Object {
$_.BaseName + '.jpg'
}