= 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 \\ {{tag>dokuwiki powershell vscode}} == 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 [[https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave |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 [[https://wiki.getshifting.com/vscode#repo_settings_and_extensions |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:// ' Add-StringToFiles -FolderPath $folder -StringToAdd $adString -ExcludeList $excludeFiles //This wiki has been made possible by://