Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. '--------------------------------
  2. ' Split a file into smaller chunks
  3. ' Last Update: 20161130
  4. ' CmdLine: {this-script} {path to big file} {size in KB} {output}
  5. '--------------------------------
  6. LF = Chr(10)
  7. Dim oFSO, FullName, Path, Name, Size, TargetDir
  8. Set oFSO = CreateObject("Scripting.FileSystemObject")
  9.  
  10. 'Set stdout = oFSO.GetStandardStream (1)
  11. 'Set stderr = oFSO.GetStandardStream (2)
  12. Set stdout = WScript.StdOut
  13. Set stderr = WScript.StdErr
  14.  
  15. stdout.Write " **** SPLIT-FILE CLI VERSION **** "
  16.  
  17. Process_CmdParams
  18. Split
  19.  
  20. '-----------------------------------
  21. 'Process the command line parameters
  22. '-----------------------------------
  23. Sub Process_CmdParams
  24. Dim oArgs
  25. Set oArgs = WScript.Arguments
  26. If oArgs.Count <> 3 Then Error _
  27. ("Incorrect number of parameters." & LF & LF _
  28. & "Usage: {this-script} {big-file-path} {size} {output}" & LF _
  29. & " * size in KB") & LF _
  30. '& "State desired output file size in kBytes!")
  31.  
  32. FullName = oArgs(0)
  33. Path = oFSO.GetParentFolderName(oArgs(0))
  34. Name = oFSO.GetBaseName(oArgs(0)) & "."
  35. Size = 1024 * oArgs(1)
  36. 'TargetDir = Left(oArgs(2),1) & ":\"
  37. TargetDir = oArgs(2) & "\"
  38. 'TODO: Set default parameters
  39. End Sub
  40.  
  41. '--------------
  42. 'Split the file
  43. '--------------
  44. Sub Split
  45. Dim iFile, oFile, iStream, Data
  46. Dim Ext, e, offset, length, NewName
  47.  
  48. if not oFSO.FileExists(FullName) then Error _
  49. ("Cannot locate the file """ & FullName & """")
  50. Set iFile = oFSO.GetFile(FullName)
  51.  
  52. On Error Resume Next
  53. Set iStream = iFile.OpenAsTextStream(1)
  54.  
  55. if err.number > 0 then Error("Cannot open the file """ & FullName _
  56. & """" & LF & Err.Description)
  57.  
  58. On Error Goto 0
  59. Data = iStream.Read(iFile.Size)
  60. iStream.close
  61.  
  62. Ext = 0
  63. offset = 1
  64.  
  65. Do
  66. Ext = Right("00" & Ext + 1, 3)
  67. if ext > "999" then Error ("Too many files - maximum is 999!")
  68.  
  69. NewName = TargetDir & Name & Ext
  70. Info "Writing """ & NewName & """"
  71.  
  72. On Error Resume Next
  73.  
  74. Set oFile = oFSO.CreateTextFile(NewName, 2)
  75.  
  76. if err.number > 0 then Error("Cannot open the file """ _
  77. & NewName & """" & LF & Err.Description)
  78. On Error Goto 0
  79.  
  80. length = Size
  81. If length > Len(data)+1 - offset Then length = Len(data) + 1 - offset
  82.  
  83. oFile.Write Mid(Data, offset, length)
  84. offset = offset + length
  85.  
  86. oFile.Close
  87. Loop Until offset >= Len(data)
  88. End Sub
  89.  
  90. '----------
  91. 'Error exit
  92. '----------
  93. Sub Error (Message)
  94. stderr.Write Message
  95. End Sub
  96.  
  97. Sub Info (Message)
  98. stdout.Write Message
  99. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement