我創建了一個 powershell 腳本,它復制給定檔案夾中每個檔案的名稱(它創建 filename.txt,而不是 C:\path\filename.txt)并將其全部放在文本檔案中。
我現在正在嘗試撰寫一個 powershell 腳本,該腳本僅將專案復制到該文本檔案中未包含的不同目錄。
有任何想法嗎?謝謝你們。
uj5u.com熱心網友回復:
以下代碼將以更 PowerShell 的方式完全按照您的要求執行。最好使用陣列來保存要忽略的名稱。如果您需要使用 .txt,則只需使用Get-Content將 .txt 加載到$ignoreFiles變數中即可。
#-file gets files only (not folders)
$ignoreFiles = (Get-ChildItem -Path "C:\Users\YOURS" -file).name
$checkFolder = Get-ChildItem -Path "C:\Users\YOURS" -file
$targetFolder = "C:\Users\YOURS"
foreach ($t in $checkFolder){
if($t.name -notin $ignoreFiles){
Copy-Item $t.fullname $targetFolder
}
}
uj5u.com熱心網友回復:
#-file gets files only (not folders)
$listPath = "\\server\C$\exclusionpath\list.txt"
$downloadPath = "\\adifferentserver\download\"
$targetFolder = "\\adifferentserver\Processing\"
$ignoreFiles = Get-Content -Path $listPath
$checkFolder = Get-ChildItem -Path $downloadPath -File -Exclude $ignoreFiles -
Name
foreach ($t in $checkFolder){
#Copy-Item $t.fullname $targetFolder
Copy-Item (Get-ChildItem -Path $downloadPath -File | Where-Object {$_.Name -eq
$t}).FullName $targetFolder
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364253.html
上一篇:在Python中以寫入模式(“w”)打開后,文本檔案不會附加(“a”)
下一篇:OBS如何記錄隱藏視窗?
