Home > Mobile >  Recursive Get-ChildItem and merging files within the same directory
Recursive Get-ChildItem and merging files within the same directory

Time:04-27

I am trying to perform a recursive search that will merge files together into a single file within the directory the file was found. Find files named Dexis_Conflit_(*).dat and merge, or create into a file named dexis.dat. The .dat files are plain text. There are thousands of folders that I need to find and merge these files in. I have a basic PowerShell code that finds all of the files and their corresponding folders, but I cannot figure out how to merge them all into a single file within the correct directories.

Get-ChildItem -File -Include "dexis_conflict_(*).dat" -Path D:\apps\dexis\data -Recurse -OutVariable DEXIS.dat

I have tried using Out-File, Set-Content, Add-Content, and foreach but I cannot figure it out. Any help would be greatly appreciated.

EDIT: Most 95% of the folders will already have a dexis.dat file in it. All "conflict" files need to be merged into the main dexis.dat file. Those folders that do not have a dexis.dat file, it will need to be created and the conflict file merged into it.

CodePudding user response:

As always, there are plenty of ways to go about this using PowerShell so here's my take:

Get-ChildItem -Path "D:\apps\dexis\data" -Filter "dexis_conflict_(*).dat" -File -Recurse | 
    ForEach-Object -Begin {
        $saveTo = "DEXIS.dat"
    } -Process {
        $parentPath = Resolve-Path $_.PSParentPath -Relative 
        $jointPath  = Join-Path -Path $parentPath -ChildPath $saveTo
            if (-not (Test-Path -Path $jointPath)) {
                New-Item -Path $jointPath | Out-Null  
            }
        Get-Content -LiteralPath $_.FullName | Add-Content -LiteralPath $jointPath 
    }

Given that your files are just plain text files, there shouldn't be a need to use a specific file encoding such as bytes, but there is still that option if needed. Other than that, the rest is pretty simple. Using Resolve-Path we can can get rid of the unwanted prepended text PowerShell adds to the parent path. Then, using Join-Path we can create the file path of .\.\DEXIS.dat which we can test against. This is doable since PowerShell processes the files in each directory one at a time before moving onto the next sub-folder.

So...

  • Test to see if dexis.dat exists, and if not, create the file.
  • If the file does exist just append the content of each dexis_conflict_(*).dat file to it using Add-Content.

Now, every directory should have a DEXIS.dat file with merged content from file names of dexis_conflict_(*).dat.

  • Related