I've put together a script that recursively copies from one directory to another, skipping files with a certain pattern in the filename:
function Copy-RevitFiles ([string]$source, [string]$destination, [boolean]$recurse) { $pattern = '\.\d\d\d\d\.[RVT]' if ($recurse) {$files = Get-ChildItem $source -Recurse} else {$files = Get-ChildItem $source} $files | ForEach-Object { if ((Select-String -InputObject $_.Name -pattern $pattern -AllMatches -quiet) -eq $null) { #Write-Host $_.Name #Write-Host $_.Fullname #Write-Host "$($destination)\$($_.FullName.TrimStart($source))" Copy-Item $_.FullName -Destination "$($destination)\$($_.FullName.TrimStart($source))" #add on full name of item, less $source start end of file path #Write-Host "----------" } } }
It works well, for the most part. The problem I have though is that it creates an additional subfolder inside each folder with files in it. For example:
If input the source as a directory with this structure:
Source -file1.rvt -file1.0225.rvt (will not copy as it matches the pattern) -file1.0226.rvt (will not copy as it matches the pattern) -folder1 |-file2.rvt |-file2.0121.rvt (will not copy as it matches the pattern) |-file2.0122.rvt (will not copy as it matches the pattern) -folder2
I am expecting the following structure to be created in the destination folder:
Destination -file1.rvt -folder1 |-file2.rvt -folder2
But instead, I am getting:
Destination -file1.rvt -folder1 |-file2.rvt |-folder1 (extra folder not in source) -folder2
Any idea where I am going wrong?