Script to split large files

This VBscript will split all files with the matching extension (‘sql’ in this example) in the script folder into separate files of size intSize (1gb in this example). Files will not be exactly of that size but will be slightly larger depending on your line sizes and how often the script checks the output file size (can be adjusted by modifying the line with the ‘Mod’ expression in it). I did try doing it in PowerShell but it took roughly 10,000 times as long to process!

'Written by Ben Penney https://sysadminben.wordpress.com Option Explicit Dim strExtension, intSize strExtension = "sql" 'The file type to split intSize = 1000000000 'The size to split into in bytes Dim objShell, objFSO Set objShell = WScript.CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objFile, objInput, objOutput, objOutputFile, objError Dim intLineNum, intFilenum, strRead For Each objFile in objFSO.GetFolder(objShell.CurrentDirectory).Files If LCase(Right(objFile.Name, Len(strExtension))=strExtension) Then intFilenum = 1 Set objInput = objFSO.OpenTextFile(objFile.Name) Set objOutput = objFSO.CreateTextFile(Replace(objFile.Name,"." & strExtension,intFilenum & "." & strExtension)) Set objOutputFile = objFSO.GetFile(Replace(objFile.Name,"." & strExtension,intFilenum & "." & strExtension)) Set objError = objFSO.CreateTextFile(Replace(objFile.Name,"." & strExtension,"ERROR." & strExtension)) intLineNum = 1 Do While Not objInput.AtEndOfStream strRead = objInput.ReadLine If intLineNum Mod 10000 = 0 Then 'Put this in as it slowed down the process checking size every line If objOutputFile.Size > intSize Then objOutput.Close Set objOutput = objFSO.CreateTextFile(Replace(objFile.Name,"." & strExtension,intFilenum & "." & strExtension)) Set objOutputFile = objFSO.GetFile(Replace(objFile.Name,"." & strExtension,intFilenum & "." & strExtension)) End If End If Err.Clear On Error Resume Next objOutput.WriteLine strRead If Err.Number 0 Then objError.WriteLine "error writing line " & intFilenum On Error Goto 0 intLineNum = intLineNum + 1 Loop objInput.Close objOutput.Close objError.Close End If Next Wscript.Echo "All Done."

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s