2016-12-06 136 views
0

在Windows 7 x64上,Powershell 3.0:

我有一個目錄,我在這裏下載文件,這是通過.csv文件完成的。 文件的樣本是:Powershell檢查文件是否存在

008KS04.zip 
008KS05.zip 
008KS06.zip 
008KS07.zip 
008KS08.zip 
008KS09.zip 

我有一個CSV,運至極這是一個示例:

Documentnummer,Versie,Status,Geocode,IDS datum,Documentgroep (Omsch),Documenttype (Wrd),Tekeningnummer 
550164,I,AB,008,26.03.2016,Themalagen OI,KS,008KS09.zip 
550163,D,AB,008,19.07.2013,Themalagen OI,KS,008KS08.zip 
550162,D,AB,008,19.07.2013,Themalagen OI,KS,008KS07.zip 
550161,C,AB,008,19.07.2013,Themalagen OI,KS,008KS06.zip 
550160,D,AB,008,19.07.2013,Themalagen OI,KS,008KS05.zip 
550159,E,AB,008,15.08.2014,Themalagen OI,KS,008KS04.zip 

因此,可以說該文件位於C:\測試\
和在C CSV:\ drawings.csv
我想是缺少一個CSV文件,可以說C:\ missing.csv
起初我有一個繪圖想在一個變量:

$drawnr = '008KS09.zip' 
$destcsv = "C:\missing.csv" 
Set-Location C:\test 
    If(Test-Path -Path ${drawnr}) { 
     Write-Output "${drawnr}'.zip' exists" 

    } Else { 

    Write-Output "${drawnr}'.zip' doesnt exist" 
    ${drawnr} | Export-Csv $destcsv -Append -NoTypeInformation 

} 

The output when having an existing and a non existsing drawing in the variable
這工作完美,看到圖像,但使用CSV作爲輸入,我無法讓它工作。
這是我到目前爲止有:

$sourcecsv = 'C:\drawings.csv' 
$destcsv = 'C:\missing.csv' 
Set-Location 'C:\test\' 

Import-Csv $sourcecsv | Select-object -Property Tekeningnummer | ForEach { 

    Write-Output "${_} is the current object" #For Testing purpuse 
If (Test-Path -Path ${_}) { 

    Write-Output "${_} does exist" 

    } Else { 

    Write-Output "${_} doesnt exist" 
    ${_} | Export-Csv $destcsv -Append -NoTypeInformation 
    } 
} 

這告訴我,沒有任何文件的存在,而所有確實存在。
看圖片,看的PowerShell的輸出,並寫入missing.csv
The picture of the output where all files are reported as non existend

+0

如果你改變'選擇對象,物業Tekeningnummer'到'選擇對象-ExpandProperty Tekeningnummer'或交替變化每個'$ {_}'來'$ _。Tekeningnummer'。 –

回答

0

我不知道我明白你的意圖的內容,但它看起來像這可能工作。

$sourcecsv = 'C:\drawings.csv' 
$destcsv = 'C:\missing.csv' 
Set-Location 'C:\test\' 

Import-Csv $sourcecsv | ForEach { 

    Write-Output "${_} is the current object" #For Testing purpuse 
    $TK = $_.Tekeningnummer 
If (Test-Path -Path $TK) { 

    Write-Output "$TK does exist" 

    } Else { 

    Write-Output "$TK doesnt exist" 
    $_ | Export-Csv $destcsv -Append -NoTypeInformation 
    } 
} 
0

不知道${_}是一種新的語法,我不知道,但我不能讓它到我的PS4主機上的工作,所以我改變是正常$_在我的答案。除此之外,你只需要按照@DaveSexton的建議做ExpandProperty

Import-Csv $sourcecsv | Select-object -ExpandProperty Tekeningnummer | ForEach { 

Write-Output "$($_) is the current object" #For Testing purpuse 
If (Test-Path -Path $($_) { 

    Write-Output "$($_) does exist" 

} Else { 

    Write-Output "$($_) doesnt exist" 
    $_ | Export-Csv $destcsv -Append -NoTypeInformation 
} 
} 
0

就去做;)

$sourcecsv = 'C:\drawings.csv' 
$destcsv = 'C:\missing.csv' 
$dirtest='C:\test\' 

Import-Csv $sourcecsv | where {-not (Test-Path "$dirtest$($_.Tekeningnummer)") } | export-csv $destcsv -NoTypeInformation