I created a powershell script that copies the name of every file in a given folder (it creates filename.txt, not C:\path\filename.txt) and places it all in a text doc.
I'm now trying to write a powershell script that ONLY copies items to a different directory that are not contained in that text doc.
Any ideas? Thank you all.
CodePudding user response:
The following code will do exactly what you’re after in a more PowerShell way. Better to use an array to hold the names to be ignored. If you need to use the .txt then just useGet-Content
to load the .txt into the $ignoreFiles
variable.
#-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
}
}
CodePudding user response:
#-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
}