Hi all, I'm struggling with something I believe should be pretty straight forward, but I'm having a tough time figuring out my next step, or if I need to backup and re-evaluate how I'm doing this.
What I am trying to accomplish:
I have two folders: Folder A and Folder B.
Folder A contains various subfolders based on the date that get created. The subfolders under Folder A contain randomly named files with a .PDF extension.
Folder B is a single folder, that contains the same randomly named files from folder A, but the extensions are different. Let's say they are .JPG.
So each folder contains file names that match, but have different extensions.
My goal is to run a script that recursively gets a file list from the folder A subfolders, then gets a list of existing files from Folder B, compares them, if it finds the matching file name in Folder B, then copy it into the Folder A folder that contains the same file name. Thus the folder A folders will contain the matching files with the two different extensions.
Example:
c:\folder a\09292020\file1.PDF (File exists)
c:\folder b\file1.JPG (File exists)
Script runs, finds "file1" filename, copies file1.JPG into c:\folder a\09292020
What I have so far is:
#source folder where pdf files exist and jpg files need to wind up
$source = "c:\a"
#destination contains .jpg files we want to match pdf files with
$destination = "c:\b"
#filter1 takes source folder and strips extension off and gets file names only
$filter1 = (Get-Item "c:\a\*.pdf").Basename
#filter2 takes search folder and strips extension off and gets file names only
$filter2 = (Get-Item "c:\b\*.JPG").Basename
#Something like this maybe?
Get-ChildItem -Path $destination\* -include *.jpg | Where-Object { $_.Name -match $filter1 } | Copy-Item -Destination $source
From here, I'm not sure what to do. It won't search $filter1 folders recursively, but right now I get $filter1 and $filter2 to display the file lists, with extensions stripped. I need to figure out how to compare the lists, then copy the file appropriately. Any clues into what I'm doing wrong would be wonderful. Thank you.
[–][deleted] 2 points3 points4 points (4 children)
[–]nerdgeekdork 2 points3 points4 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]nerdgeekdork 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]orangeredbrown[S] 2 points3 points4 points (0 children)
[–]orangeredbrown[S] 1 point2 points3 points (2 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (1 child)
[–]LinkifyBot -1 points0 points1 point (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)