mellowtigger: (penguin coder)
mellowtigger ([personal profile] mellowtigger) wrote2012-05-20 11:23 am
Entry tags:

recordsplit.ps1

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.  :)

Post a comment in response:

(will be screened)
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org