I have a text file.

I would like a simple portable app if possible which can automatically generate 176 different versions where the only difference (e.g. {%FIELD1%} is pulled from a separate file where each pulls from a newline.

I have seen this but this is a word addon which isn't quite what I'm looking for https://www.gmayor.com/individual_merge_letters.htm

  • How portable should it be? Across different OS'es? If not, a bash or powershell script could work. – Saaru Lindestøkke Jun 22 at 11:12
  • I was thinking of a portable Windows application runnable off a usb and only writing to local directory. Powershell script would work a treat if you have any guidance on how to do this? – MANICX100 Jun 22 at 11:14
  • Added a script as an example. Note that there are existing scripts as well that might be of more use. – Saaru Lindestøkke Jun 22 at 12:15

This script below:

  • Loads a text file
  • Loads a separate file that contains a list
  • Replaces the term {%FIELD1%} in the text file by the values in the list
  • For every replacement writes out a separate text file

Usage instructions:

  • Copy paste below code to a file with a ps1 extension.
  • Open it with Windows PowerShell ISE (or a text editor) and edit the $mainfile, $mergelistfile, $fieldstring and $outpath as needed.
  • Run the .ps1 file.
# A very simple "mail-merge" script that replaces text in a template file with a 
# value from a list and then writes the text with the replacement to another file.
# This script assumes the following:
# - a $mainfile exists: this is the text file where you want to do the replacements in
# - a $mergelist exists: this is a list of values which you want to use as replacements
# - $mainfile uses the value $fieldstring to indicate the part that should be replaced
# - There either is a writable $outpath or you have permission to create one

$mainfile = '.\mainmerge.txt'
$mergelistfile = '.\mergelist.csv'
$fieldstring = '{%FIELD1%}'
$outpath = '.\mergeoutput'

# Get the mainfile and mergelistfile contents
$maincontent = Get-Content -Path $mainfile -Raw
$mergelist = Get-Content -Path $mergelistfile

# Create the output directory if it doesn't exist yet
if(-Not (Test-Path $outpath)){
    New-Item -ItemType directory -Path $outpath

# Loop through $mergelist
$counter = 1
foreach($entry in $mergelist){
    # The output filename with a zero padded number prepended 
    $outfilename = ([string]$counter).PadLeft(3,'0') + " merged.txt"

    # Replace the $fieldstring by the current entry from the mergelist
    $merged = $maincontent -replace $fieldstring, $entry

    # Create the full path of the output file
    $fulloutpath = Join-Path -Path $outpath -ChildPath $outfilename

    # Write the merged file to the full outputpath
    $merged | Add-Content -Path $fulloutpath

    # Increment the counter for the output filename
| improve this answer | |
  • Perfect, simple easy thank you – MANICX100 Jun 22 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.