Hello!

At some point last year I moved from Wordpress to Hugo. It was a bit of convoluted process, taking the xml file from Wordpress and munging the data to create markdown files with the content intact, and largely it was a successful process. Some of the older posts on this here blog are a bit broken, but the older the post the less I cared. However, with over 430 posts here the posts folder in my source control was a bit messy as I had one file per post directly under the “post” directory. And the one thing I really wanted to do was to create a folder by month and chuck the pertinent files in there. This was relatively trivial as each post was prefixed by the yyyy-mm, so I could take each file and substring the file name and create a folder if it did not exist, and move the file. Simple enough with PowerShell.

However, there is a problem here in that some of my posts have relative paths to images. So it became necessary to edit all files that had a markdown image tag and add an extra \\.. to the paths, as each file will now be one directory away from the image. Again, not that hard with PowerShell.

The final challenge are those posts I wrote that have links to other posts. Mercifully there’s not to many of those, so I’m editing them manually. It’s just not worth figuring out the -match wildcard for a couple of links.

Anyway, here is the code, more for my pruposes than anyone elses.


$pathToLegacy = Get-ChildItem "C:\Users\RichardLee\source\repos\source.bzzztio\content\post\2012*.md"
foreach ($oldPost in $pathToLegacy) {
    Write-Host $oldPost
    $content = Get-Content $oldPost.FullName
    ForEach ($line in $content) {
        if ($line -match "\!\[" ) {
            Write-Host "Found!"
            $newLine = $line.Replace('..\\..\\', '..\\..\\..\\')
            $newLine
            $content = $content.Replace($line, $newLine)
        }
    }
    Set-Content $oldPost $content -Force
}


$pathToLegacy = Get-ChildItem "C:\Users\RichardLee\source\repos\source.bzzztio\content\post\2012*.md"
$newFolderRoot = "C:\Users\RichardLee\source\repos\source.bzzztio\content\post\"
foreach ($bob in $pathToLegacy) {
    $newFolderName = Join-Path $newFolderRoot $bob.name.Substring(0, 7)
    if ((Test-Path $newFolderName) -eq $false) {
        Write-Host "making $newFolderName"
        New-Item -ItemType directory -Path $newFolderName
    }
    $newFile = Join-Path $newFolderName $bob.Name

    if ((Test-Path $newFile) -eq $false) {
        Write-Host $newFile -ForegroundColor DarkMagenta
        Move-Item $bob -Destination $newFolderName
    }
}