Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '--------------------------------
- ' Split a file into smaller chunks
- ' Last Update: 20161130
- ' CmdLine: {this-script} {path to big file} {size in KB} {output}
- '--------------------------------
- LF = Chr(10)
- Dim oFSO, FullName, Path, Name, Size, TargetDir
- Set oFSO = CreateObject("Scripting.FileSystemObject")
- 'Set stdout = oFSO.GetStandardStream (1)
- 'Set stderr = oFSO.GetStandardStream (2)
- Set stdout = WScript.StdOut
- Set stderr = WScript.StdErr
- stdout.Write " **** SPLIT-FILE CLI VERSION **** "
- Process_CmdParams
- Split
- '-----------------------------------
- 'Process the command line parameters
- '-----------------------------------
- Sub Process_CmdParams
- Dim oArgs
- Set oArgs = WScript.Arguments
- If oArgs.Count <> 3 Then Error _
- ("Incorrect number of parameters." & LF & LF _
- & "Usage: {this-script} {big-file-path} {size} {output}" & LF _
- & " * size in KB") & LF _
- '& "State desired output file size in kBytes!")
- FullName = oArgs(0)
- Path = oFSO.GetParentFolderName(oArgs(0))
- Name = oFSO.GetBaseName(oArgs(0)) & "."
- Size = 1024 * oArgs(1)
- 'TargetDir = Left(oArgs(2),1) & ":\"
- TargetDir = oArgs(2) & "\"
- 'TODO: Set default parameters
- End Sub
- '--------------
- 'Split the file
- '--------------
- Sub Split
- Dim iFile, oFile, iStream, Data
- Dim Ext, e, offset, length, NewName
- if not oFSO.FileExists(FullName) then Error _
- ("Cannot locate the file """ & FullName & """")
- Set iFile = oFSO.GetFile(FullName)
- On Error Resume Next
- Set iStream = iFile.OpenAsTextStream(1)
- if err.number > 0 then Error("Cannot open the file """ & FullName _
- & """" & LF & Err.Description)
- On Error Goto 0
- Data = iStream.Read(iFile.Size)
- iStream.close
- Ext = 0
- offset = 1
- Do
- Ext = Right("00" & Ext + 1, 3)
- if ext > "999" then Error ("Too many files - maximum is 999!")
- NewName = TargetDir & Name & Ext
- Info "Writing """ & NewName & """"
- On Error Resume Next
- Set oFile = oFSO.CreateTextFile(NewName, 2)
- if err.number > 0 then Error("Cannot open the file """ _
- & NewName & """" & LF & Err.Description)
- On Error Goto 0
- length = Size
- If length > Len(data)+1 - offset Then length = Len(data) + 1 - offset
- oFile.Write Mid(Data, offset, length)
- offset = offset + length
- oFile.Close
- Loop Until offset >= Len(data)
- End Sub
- '----------
- 'Error exit
- '----------
- Sub Error (Message)
- stderr.Write Message
- End Sub
- Sub Info (Message)
- stdout.Write Message
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement