The following PowerShell script should do what you want.
Change the first three variables to fit your environment.
## Q:\Test\2018\06\28\SU_1334840.ps1
#Requires -Version 3
# get files to copy from file in same foler
$FileList = Get-Content '.\FileList.txt'
$BaseSrc = 'X:\Source\path\'
$BaseDst = 'Y:\Destination\path\'
ForEach ($File in $FileList){
ForEach ($Find in (Get-ChildItem $BaseSrc$File -File -Recurse)) {
$DestFile = Join-Path $BaseDst ($Find.FullName.Replace($BaseSrc,'') )
$DestDir = Split-Path $DestFile
If (!(Test-Path $DestDir)){ MD $DestDir |Out-Null}
# "Copying {0} to {1}" -f $Find.FullName,$DestFile
Copy-Item $Find.FullName $DestFile
}
}
To see what's going on while the script works, remove the #
before "Copying...
The script requires at least PowerShell version 3
Depending on the size of your list / the number of files and subdirs in your source tree, this a bit different approach may be faster.
## Q:\Test\2018\06\28\SU_1334840_2.ps1
#Requires -Version 3
# get files to copy from file in same foler
$FileList = Get-Content '.\FileList.txt'
$BaseSrc = 'X:\Source\path\'
$BaseDst = 'Y:\Destination\path\'
ForEach ($File in (Get-ChildItem $BaseSrc -File -Recurse)) {
If ($File.Name -in $FileList) {
$DestFile = Join-Path $BaseDst ($File.FullName.Replace($BaseSrc,'') )
$DestDir = Split-Path $DestFile
If (!(Test-Path $DestDir)){ MD $DestDir |Out-Null}
"Copying {0} to {1}" -f $File.FullName,$DestFile
Copy-Item $File.FullName $DestFile
}
}
Sample output:
> . Q:\Test\2018\06\28\SU_1334840.ps1
Copying C:\sys\7z.dll to A:\Test\sys\7z.dll
Copying C:\sys\7z.exe to A:\Test\sys\7z.exe
Copying C:\sys\ClipBoard.exe to A:\Test\sys\ClipBoard.exe
Copying C:\sys\ClipBoard.txt to A:\Test\sys\ClipBoard.txt
Copying C:\sys\DUMPHEX.EXE to A:\Test\sys\DUMPHEX.EXE
1
Starting point for you to build on but this is trivial for a batch scripter like myself....
– Pimp Juice IT – 2018-06-28T05:48:58.330FOR /F "USEBACKQ TOKENS=*" %A IN ("C:\Folder\Path\TextFile.txt") DO ECHO %~A
. . . https://ss64.com/nt/for_cmd.html . . . Read over some of my answer that may be applicable just pick some and read as I'm sure I've helped with this before for someone: https://superuser.com/search?q=user%3A510662+body%3A+for+%2Ff