I try to write a script that does the following:
- determine folders that contain PDF files from a source dir
- create the full original directory structure down to these folders to a destination dir
- copy the full content of the folder that contains a PDF - regardless of the types of the other files in that folder
- do not copy any of the files in the parent folders
screenshot with description of what I try to achieve
I hope I described it well enough for you who don't live in my head to understand what i mean. :D
I got this script now.
# Define the source and destination paths
$source = "H:\MC4"
$destination = "H:\Mirror"
# Get all subdirectories that contain at least one PDF file
$dirsWithPDFs = Get-ChildItem -Path $source -Recurse -Directory | Where-Object {
Get-ChildItem -Path $_.FullName -Filter *.pdf -File -Recurse | Where-Object { $_.Extension -eq ".pdf" }
}
# Copy each directory with PDF files using Robocopy
foreach ($dir in $dirsWithPDFs) {
$sourceDir = $dir.FullName
$destDir = $sourceDir.Replace($source, $destination)
robocopy $sourceDir $destDir /MIR
}
It nearly does the job. Only problem: It won't leave the parent folders of my "PDF folder" empty of files.
Can you tell me how to do that? Sorry, I've found many similar questions and answers but not that exact situation.
Thanks so much!