簡體   English   中英

如何在整個目錄中進行測試路徑搜索或解決

[英]How to not have test-path search through whole directory or work around

我有一些壓縮的pdf文件,我需要解壓縮它們,然后保存第一頁。 我有解壓縮和保存的第一頁代碼正常工作。 但是,當我進行解壓縮時,大約有150個文件夾在4000中創建,其中包含pdf和其他格式文件,如.txt,.csv等。所以我的問題是我需要拉出pdf文件並將它們移動到主文件夾與其他pdf的其余部分,所以我可以批量保存每個pdf的第一頁。

解壓縮后的文件夾結構將是:

SecondStorage (main folder where all pdf end after unzip)
   -Folder containing pdf and another file
   -folder containing pdf and another file
   -pdf
   -pdf

PDF始終是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"
        }
    }

代碼運行時,它告訴我文件已經存在。 我以為我可以使用文字路徑,但我遺漏了一些東西,因為我一直得到相同的結果。

感謝您的任何建議,非常感謝。

只需翻轉你的if語句。 在測試以確定文件是否存在於主文件夾中時,您要輸出文本,而不是移動;-)

if (Test-Path -Path "$SecondStorage\$($i.name)" )
{
    Write-Output "File already exists"
}
else 
{
    Move-Item -Path $i.fullname -Destination $SecondStorage
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM