From f1e2d192d075e6f7cb0737cdf6a343e66eed6ecd Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 13 Oct 2026 12:12:03 +0200 Subject: [PATCH] Fix: Fix empty `docx` folder being left behind in the notebook folder when discarding `.docx` files With `$keepdocx = 1` (default), each page's `.docx` file is removed after conversion, but the notebook's `docx` folder was not, leaving an empty `docx` folder in every converted notebook. Now the `docx` folder is removed after a page's `.docx` file is removed, if the folder is empty. It is created again for the next page. A folder that still contains files (e.g. from a previous run with `$keepdocx = 2`) is kept. Co-Authored-By: Claude Opus 5.5 --- ConvertOneNote2MarkDown-v2.Tests.ps1 | 42 ++++++++++++++++++++++++++++ ConvertOneNote2MarkDown-v2.ps1 | 12 ++++++++ 2 files changed, 54 insertions(+) diff --git a/ConvertOneNote2MarkDown-v2.Tests.ps1 b/ConvertOneNote2MarkDown-v2.Tests.ps1 index 72d1ae7..68f247d 100644 --- a/ConvertOneNote2MarkDown-v2.Tests.ps1 +++ b/ConvertOneNote2MarkDown-v2.Tests.ps1 @@ -1741,6 +1741,48 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { $err | Should -Not -Be $null } + It "Removes the docx directory if it is empty after removing the docx file" { + Mock Get-ChildItem -ParameterFilter { $LiteralPath } {} + $docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent + + Convert-OneNotePage @params 6>$null + + Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory -and $Force } -Times 1 -Scope It + } + + It "Keeps the docx directory if it is not empty" { + Mock Get-ChildItem -ParameterFilter { $LiteralPath } { + [pscustomobject]@{ + Name = 'some-other.docx' + } + } + $docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent + + Convert-OneNotePage @params 6>$null + + Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory } -Times 0 -Scope It + } + + It "Keeps the docx directory if keeping docx files" { + Mock Get-ChildItem -ParameterFilter { $LiteralPath } {} + $docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent + $params['Config']['keepdocx']['value'] = 2 + + Convert-OneNotePage @params 6>$null + + Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory } -Times 0 -Scope It + } + + It "Does not halt conversion if removing the empty docx directory fails" { + Mock Get-ChildItem -ParameterFilter { $LiteralPath } {} + Mock Remove-Item -ParameterFilter { $LiteralPath -and $Force } { throw "i failed" } + + $err = Convert-OneNotePage @params 6>$null 2>&1 + + $err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to remove empty docx directory' + Assert-MockCalled -CommandName Copy-Item -Times 1 -Scope It + } + It "Saves page attachment(s)" { Convert-OneNotePage @params 6>$null diff --git a/ConvertOneNote2MarkDown-v2.ps1 b/ConvertOneNote2MarkDown-v2.ps1 index 3668cba..798dcd9 100644 --- a/ConvertOneNote2MarkDown-v2.ps1 +++ b/ConvertOneNote2MarkDown-v2.ps1 @@ -1221,6 +1221,18 @@ Function Convert-OneNotePage { }catch { Write-Error "Failed to remove existing docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )" -ErrorAction Continue } + # Remove the docx directory if it is empty, so that it is not left behind after the conversion. It is created again for the next page + $docxDirectory = Split-Path $pageCfg['docxExportFilePath'] -Parent + try { + if (!$config['dryRun']['value']) { + if (@( Get-ChildItem -LiteralPath $docxDirectory -Force -ErrorAction Stop ).Count -eq 0) { + "Removing empty docx directory: $( $docxDirectory )" | Write-Verbose + Remove-Item -LiteralPath $docxDirectory -Force -ErrorAction Stop + } + } + }catch { + Write-Error "Failed to remove empty docx directory $( $docxDirectory ). Exception: $( $_.Exception.Message )" -ErrorAction Continue + } } # Save any attachments