recordsplit.ps1
2012-May-20, Sunday 11:23 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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.
Enjoy. :)
# 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. :)