Link to home
Start Free TrialLog in
Avatar of sparker1970
sparker1970Flag for United States of America

asked on

Find and Copy Specific Files from a Subfolder Tree

I have 5 years of historical data in drill-down folders (by year, then by month, then by day). I am trying to find and copy all files in the folder tree named Financial_300.txt into 1 centralized location. I do not want to move the originals, just make a centralized copy I can then use in a database or for easier searches. I believe the basic XCOPY command will pull the subfolders along with the file so I am possibly looking for a different solution than XCOPY (unless there are switches in the parameters that would make it work).
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

And just for the fun of it, here's a Powershell version.
As before, it's in test mode; remove the -WhatIf at the end of line 9 to run it for real.
$Source = "D:\Temp"
$Target = "C:\Temp\Financial_300"
$FileMask = "Financial_300.txt"
Get-ChildItem -Path $Source -Filter $FileMask -Recurse | % {
	"Processing '$($_.Fullname)' " | Write-Host -ForegroundColor White -NoNewline
	If ($_.DirectoryName -match '\A.+\\(?<YYYY>\d{4})\\(?<MM>\d{1,2})\\(?<DD>\d{1,2})\Z') {
		$CentralFileName = "$($_.BaseName)_$($Matches['YYYY'])_$($Matches['MM'])_$($Matches['DD'])$($_.Extension)"
		"--> '$($CentralFileName)'" | Write-Host -ForegroundColor Green
		Copy-Item -Path $_.FullName -Destination "$($Target)\$($CentralFileName)" -WhatIf
	} Else {
		"... skipped." | Write-Host -ForegroundColor Yellow
	}
}

Open in new window

If you move multiple copies of the same file named "Financial_300.txt" to a single destination folder, they will overwrite each other, or not be copied at all.  What's your intention with managing the file name in the destination?

~bp
Avatar of sparker1970

ASKER

I've requested that this question be deleted for the following reason:

The question was not asked properly and, once clarified, had no solution.
A valid answer was posted in http:#a41370650.
It will find all files named Financial_300.txt in a subfolder structure build as \YYYY\MM\DD\, and copy them to a central folder, while adding the date built from their respective folder structure to their name.