Friday, 15 February 2008

Split a text file with VBScript

I had to split some text reports (csv format) that are read by colleagues in Excel just the other day. Excel can only read ~65k lines on one sheet, but the reports were ~100k lines - so I had to split the files in two. The reports are generated via script; so I decided to write another script to split the files at around the 60K mark. Hope this can save someone else some time :)

The script below takes as arguements; source filename, base filename and the target directory.

if WScript.Arguments.Count > 0 then

 ' retrieve the command line arguments
 sourceFile = WScript.arguments.Item(0)
        basename = WScript.arguments.Item(1)
        targetDir = WScript.arguments.Item(2)
 
 set objFSO = CreateObject("Scripting.FileSystemObject")

 ' check the file exists
        if objFSO.FileExists( sourceFile ) then
  WScript.Echo ("input file exists...")

  ' new files
  new1 = targetDir + "\" + basename + "_" + "a." + objFSO.GetExtensionName(sourceFile)
  new2 = targetDir + "\" + basename + "_" + "b." + objFSO.GetExtensionName(sourceFile)
  
  set fs = CreateObject("Scripting.FileSystemObject")  
  set w1 = objFSO.CreateTextFile(new1, True)  
  set w2 = objFSO.CreateTextFile(new2, True)  
  isNotSplit = True
   
  with fs.OpenTextFile(sourceFile)  
   
   while Not .AtEndOfStream  
 
    if .Line < 60000 then  
     w1.WriteLine .ReadLine  
    else    
     w2.WriteLine .ReadLine  
     if( isNotSplit ) then
      isNotSplit = False
      WScript.Echo ("file being split...")
     end iF
    end if  
   
   wend  
   
   .Close  
   
  end with  
   
  w1.Close  
  w2.Close  
  
  if( isNotSplit ) then
   WScript.Echo ("input file not split...")
  else
   WScript.Echo ("input file split...")
  end if
 else
  WScript.Echo ("input file does not exist...")
 end if
 WScript.Echo ("finished!")
end if

2 comments:

Anonymous said...

It does save my time for looking for such a code as yours. I've been wondering if I ever would encounter the code that satisfy my requirement.... Big Thanks.

Anonymous said...

米蘭情趣用品情趣用品情趣飛機杯自慰套充氣娃娃AV女優按摩棒跳蛋潤滑液角色扮演情趣內衣自慰器穿戴蝴蝶變頻跳蛋無線跳蛋電動按摩棒情趣按摩棒丁字褲G點SM後庭

視訊美女視訊做愛


網頁設計
網頁設計