Wednesday, August 02, 2006

a jscript that cut txt files for ipod notes

After painfully looking for a program that can cut txt for my ipod, I wrote a small piece of jscript code that works in Windows Script Host.

The place you see a blue "NEXT" should be < a h r e f = \ " " + strFileNameShorter + nFile + "." + strSuffix +"\">NEXT< / a >. Remove the unwanted empty space before you insert code.

var oWShell = new ActiveXObject( "WScript.Shell" );
var oFS = new ActiveXObject("Scripting.FileSystemObject");
var oDialog = new ActiveXObject("UserAccounts.CommonDialog");

var strCurrentDirectory = oWShell.CurrentDirectory 
var forReading = 1, forWriting = 2;
var asUnicode = -1
var nFile = 0;

oDialog.Filter = "All Files|*.*"
oDialog.InitialDir = strCurrentDirectory

var dialogResult = oDialog.ShowOpen()
if( dialogResult == 0 ) {
 oWShell.Quit()
}

var strFileName = oDialog.FileName
var vDotPos = strFileName.lastIndexOf(".")
var strSuffix = "", strFileNameShort = strFileName;
if( -1 != vDotPos ){
 strSuffix = strFileName.substring( vDotPos + 1, strFileName.length )
 strFileNameShort = strFileName.substring( 0, vDotPos  )
}

var vAntiSlashPos = strFileNameShort.lastIndexOf("\\")
var strFileNameShorter = strFileNameShort.substring( vAntiSlashPos + 1, strFileNameShort.length )

var inFile = oFS.OpenTextFile(strFileName, forReading, false, asUnicode)

do{
 var outFile = oFS.CreateTextFile(strFileNameShort + nFile + "." + strSuffix, true, asUnicode)

 var iCharacter = 0
 var temp = ""
 while( ! inFile.AtEndOfStream && iCharacter ++ < 100 ){
  temp += inFile.Read( 20 );
 }
 outFile.Write( temp )
 nFile ++;
 if( ! inFile.AtEndOfStream ){
  outFile.Write( "NEXT")
 }
 outFile.Close()
 
}while( ! inFile.AtEndOfStream )

inFile.Close()