This shouldn't be too difficult:
$sourceFolder = 'D:\Test\src_folder'
$destination = 'D:\Test\tgt_folder'
Get-Content -Path 'D:\Path\To\test.txt' | ForEach-Object {
Copy-Item -Path (Join-Path -Path $sourceFolder -ChildPath $_) -Destination $destination
}
If you're worried that test.txt may contain empty lines, do:
Get-Content -Path 'D:\Path\To\test.txt' | Where-Object { $_ -match '\S' } | ForEach-Object { .. }
as per your comment you need to have two destinations, depending on the file extension, use below code:
$sourceFolder = 'D:\Test\src_folder'
$csvDestination = 'D:\Test\tgt_folder'
$txtDestination = 'D:\Test\new_tgt_folder'
# test if the destination folders exist. If not create them first
if (!(Test-Path -Path $csvDestination)) {
$null = New-Item -Path $csvDestination -ItemType Directory
}
if (!(Test-Path -Path $txtDestination)) {
$null = New-Item -Path $txtDestination -ItemType Directory
}
Get-Content -Path 'D:\Path\To\test.txt' | Where-Object { $_ -match '\S' } | ForEach-Object {
$file = Join-Path -Path $sourceFolder -ChildPath $_.Trim()
switch ([System.IO.Path]::GetExtension($file)) {
# you can add more options here if there are other extensions to handle differently
'.csv' {$destination = $csvDestination; break}
default {$destination = $txtDestination} # this is for .txt or any other extension
}
if (Test-Path -Path $file -PathType Leaf) {
Copy-Item -Path $file -Destination $destination
}
else {
Write-Warning "File '$file' not found"
}
}