recordsplit.ps1

2012-May-20, Sunday 11:23 am
mellowtigger: (penguin coder)
[personal profile] mellowtigger
Someone on Google Plus asked for a quick program. I couldn't format the text properly on G+, so I'm posting it here instead.

# Save this text using Notepad to a file named recordsplit.ps1
# Right-click the new recordsplit.ps1 file, then select "Run with PowerShell"
# If it doesn't run, you need to enable powershell scripts...
#      http://technet.microsoft.com/en-us/library/ee176949.aspx#EEAA

# This script separates a text file into other text files, based on the delimiter text.
# Change the first 3 variables as needed.
# If more than 999 records will be used, be sure to change the "D3" in the $newfile to an appropriate digit length ("D4")

$file='P:\ps\CraigFroehle.txt'
$delimiter='{{NEW_RECORD}}*'
$keepdelimiter=$true

$counter = 0
$fileitem = gci $file
foreach ($line in (get-content $file)) {
    if ($line -like $delimiter) {
        $counter += 1
        $newfile = $fileitem.directoryname + '\' + $fileitem.basename + '.' + ("{0:D3}" -f $counter) + $fileitem.extension
        set-content -path $newfile -value $null
    }
    if (($line -ne $null) -and ($line -ne '')) {
        if (($keepdelimiter -eq $true) -or ($line -ne $delimiter)) {
            add-content -path $newfile -value $line
        }
    }
}

Enjoy.  :)

Profile

mellowtigger: (Default)
mellowtigger

About

June 2025

S M T W T F S
1 234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Powered by Dreamwidth Studios
Page generated 2025-Jun-03, Tuesday 07:04 am