我有一些pdf文件来拉链,我需要解压他们,然后保存只是第一页。我有解压缩和保存的第一页代码,它应该工作。然而,当我进行解压缩时,大约有150个文件夹在4000个文件夹中创建,其中包含pdf和其他格式文件,如.txt、.csv等。因此,我的问题是,我需要将pdf文件提取出来,并将它们与其他pdfs一起移到主文件夹中,以便批量保存每个pdf的第一页。
解压缩后的文件夹结构为:
SecondStorage (main folder where all pdf end after unzip)
-Folder containing pdf and another file
-folder containing pdf and another file
-pdf
-pdfPDF总是一个文件夹水平过去的地方,其余的pdf是定位的。
我正在尝试编写一个foreach循环,它使用测试路径来查看主文件夹中是否已经存在pdf,如果不存在,则将其移动到父文件夹。我一直有问题的测试路径告诉我,pdf存在于文件夹中,因此不移动文件。
这是我目前正在使用的代码
$ListFiles = get-childitem -path $SecondStorage -Recurse -Filter "*.pdf"
$ShortList = $ListFiles | Where-Object -Property Name -Like "20*.pdf"
foreach ($i in $ShortList)
{
if (Test-Path -Path "$SecondStorage\$($i.name)" )
{
Move-Item -Path $i.fullname -Destination $SecondStorage
}
else
{
Write-Output "File already exists"
}
}当代码运行时,它会告诉我该文件已经存在。我在想,我可以使用文字路径,但我错过了一些东西,因为我一直在取得同样的结果。
谢谢你的建议,非常感谢。
发布于 2019-04-05 19:02:07
只需翻转您的if语句。当测试文件是否存在于主文件夹中时,您希望输出文本,而不是移动;)
if (Test-Path -Path "$SecondStorage\$($i.name)" )
{
Write-Output "File already exists"
}
else
{
Move-Item -Path $i.fullname -Destination $SecondStorage
}https://stackoverflow.com/questions/55541682
复制相似问题