wiki.getshifting.com

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


addtexttoeachfile

How to Add Text to Each File in a Directory in a Repository

Summary: This wiki page shows how I added google ads to each wiki page in a Repository using powershell, and use a VS Code extension to make sure new files get the same treatment.
Date: 25 October 2025

Background

When using Google Auto Adsense on my wiki, I get ads like everywhere, which makes the pages look cluttered and hard to read. My solution is to add an ad at the bottom of each page. However, I don not want to do that manually.

Using a VS Code extension to run a script on file save

I originally wanted to use git hooks to run a script. However, git hooks have a notourious bad reputation when running them on Windows. So instead I opted to use a VS Code extension that runs a script when saving a file.

I previously used this Run on Save extension for work, which worked very well, so I decided to use it here again.

After installing the extension, you need to add some configuration to your settings.json file. I've used the vscode settings extensively before, so I just needed to add the following settings:

    "emeraldwalk.runonsave": {
        // Messages to show before & after all commands
        "message": "*** All Start ***",
        "messageAfter": "*** All Complete ***",
        // Show elappsed time for all commands
        "showElapsed": true,
        "commands": [
            {
                "match": "\\.txt$",
                "notMatch": "\\drafts/.*$",
                "cmd": "pwsh.exe -ExecutionPolicy Bypass -File ${workspaceFolder}\\ads\\Add-AdsToWikiPages.ps1"
            }
        ]
    }

This will run the powershell script located in the ads folder every time a .txt file is saved in the dokuwiki/pages folder.

The Powershell script

This is the powershell script me and copilot created together and I tested very thoroughly before using it on my actual wiki pages:

  • It starts with defining the function that can later be called with parameters.
  • It has an option to exclude certain files from being modified.
  • It checks if the string to be added is already present in the file, and only adds it if it's not there yet.
  • It appends the string at the end of the file, ensuring proper formatting with new lines.
function Add-StringToFiles {
  param(
    [Parameter(Mandatory)]
    [string]$FolderPath,
    [Parameter(Mandatory)]
    [string]$StringToAdd,
    [string[]]$ExcludeList = @()
  )
 
  Get-ChildItem -Path $FolderPath -File -Filter "*.txt" | ForEach-Object {
    $filePath = $_.FullName
    $fileName = $_.Name
 
    # Skip if file is in exclude list
    if ($ExcludeList -contains $fileName) {
      Write-Host "Skipping excluded file: $fileName"
      return
    }
 
    $content = Get-Content -Path $filePath -Raw
 
    if ($content -notmatch [regex]::Escape("//This wiki has been made possible by://")) {
      $newContent = "$content`r`n$StringToAdd`r`n"
      Set-Content -Path $filePath -Value $newContent
    }
  }
}
 
$folder = "C:\Repos\GetShifting\knowledge\dokuwiki\pages"
$excludeFiles = @("overview.txt", "alltags.txt", "sidebar.txt")
$adString = '//This wiki has been made possible by://
 
<HTML>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8613096447910897"
     crossorigin="anonymous"></script>
<!-- Wiki End of Page -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-8613096447910897"
     data-ad-slot="6221699236"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</HTML>
'
 
Add-StringToFiles -FolderPath $folder -StringToAdd $adString -ExcludeList $excludeFiles

This wiki has been made possible by:

addtexttoeachfile.txt · Last modified: by 127.0.0.1