Script Optimizer

This script removes all unnecessary code in order to shorten the file.

Use this script when your script is ready to be deployed : it will take less space on disk and will be a little quicker to run.
It suppresses unused lines, constants, space, removes comments…

File Name : optim.vbs
Requirement : WSH 5.1
Author : Jean-Luc Antoine
Submitted : 24/08/2001
Category : 4K
option explicit
Dim fSource, fDest,x,fTemp,Flag
fDest=""
If Wscript.Arguments.Count<>1 Then
	WScript.Echo "You must supply a vbs file as parameter (drag and drop it!)"
Else
	fSource=WScript.Arguments(0)
	If ucase(right(fSource,4))<>".VBS" Then
		WScript.Echo "A VBS file and no other must be supplied !" & vbCrLF & fSource
	Else
		x=InstrRev(fSource,"\")
		fDest=Left(fSource,x) & "!" & Mid(fSource,x+1)
	End If
End If
If fDest="" Then WScript.Quit
fTemp=fSource & ".TMP"
Dim fso,fs,fd,Chaine,ScrAll,Consts(),NbConsts,strTemp
NbConsts=-1
ScrAll="" 'All the code without constant declarations
Set fso=CreateObject("Scripting.FileSystemObject")
Set fs=fso.OpenTextFile(fSource,1)
Set fd=fso.OpenTextFile(fTemp,2,True)
While Not fs.AtEndOfStream
	Chaine=fs.ReadLine

	Chaine=DelLeftSpace(Chaine)

	If Left(Chaine,1)="'" Then Chaine=""

	'Supress comments at the end of the line
	x=InstrRev(Chaine,"'")
	If x>0 Then
		If x>InstrRev(Chaine,"""") Then
			Chaine=Trim(Left(Chaine,x-1))
		End If
	End If

	Chaine=DelRightSpace(Chaine)

	If Chaine<>"" Then
		'Schrink the first space
		x=Instr(Chaine," ")
		Flag=Instr(Chaine,chr(9))
		If (Flag<x) And (Flag>0) Then x=Flag
		If x>0 Then
			Flag=Instr(Chaine,"""")
			If (x<Flag) Or (Flag=0) Then
				Chaine=DelRightSpace(Left(Chaine,x)) & " " & DelLeftSpace(Mid(Chaine,x))
			End If
		End If

		fd.WriteLine Chaine

		'Remember all constant declarations
		If UCase(Left(Chaine,"6"))="CONST " Then
			strTemp=LTrim(Mid(Chaine,7))
			x=Instr(strTemp,"=")
			If x>0 Then
				strTemp=RTrim(Left(strTemp,x-1))
				NbConsts=NbConsts+1
				Redim Preserve Consts(NbConsts)
				Consts(NbConsts)=UCase(strTemp)
			End If
		Else
			ScrAll=ScrAll & Chaine & vbCrLf
		End If
	End If

Wend
fd.Close
fs.close
Set fs=Nothing
Set fd=Nothing

'Constant analysis
'Forget all unused constants
ScrAll=UCase(ScrAll)
For x=NbConsts To 0 Step -1
	If Instr(ScrAll,Consts(x))=0 Then
		Consts(x)=Consts(NbConsts)
		NbConsts=NbConsts-1
	End If
Next

'Consts() has only used constants
Set fs=fso.OpenTextFile(fTemp,1)
Set fd=fso.OpenTextFile(fDest,2,True)
While Not fs.AtEndOfStream
	Chaine=fs.ReadLine
	If Ucase(Left(Chaine,6))="CONST " Then
		'Delete All Unused declarations
		Flag=False
		For x=0 to NbConsts
			If Instr(Ucase(Chaine),Consts(x))>0 Then Flag=True
		Next
		If Not Flag Then Chaine=""
	End If
	If Chaine<>"" Then fd.WriteLine Chaine
Wend
fd.Close
fs.close
Set fs=Nothing
Set fd=Nothing
fso.DeleteFile(fTemp)

msgbox "OK"
Set fso=Nothing

Function DelRightSpace(ByVal Chaine)
	'Delete space at the end of the line
	Chaine=Trim(Chaine)
	Do While Chaine<>""
		If Right(Chaine,1)=chr(9) Then
			Chaine=RTrim(Left(Chaine,Len(Chaine)-1))
		Else
			Exit Do
		End If
	Loop
	DelRightSpace=Chaine
End Function

Function DelLeftSpace(ByVal Chaine)
	'Delete Space at the beginning of the line
	Chaine=Trim(Chaine)
	Do While Chaine<>""
		If Left(Chaine,1)=chr(9) Then
			Chaine=LTrim(Mid(Chaine,2))
		Else
			Exit Do
		End If
	Loop
	DelLEftSpace=Chaine
End Function
Server Info
Remote Info
Compress

Batchs
BootSector
Droit
Accessoire GFA
On Now
Ecran plasma
Système d'exploitation
Son
Unités
Scripting
Multilingue
Site Web
Bruit et son
Architecture Google


4K
Color picker
TaskList
Backup files
Mini Port Scanner
Website To CHM
PixyDemo
Web password recovery
4KWebServer
Kill Popup
VBE decoder
Script Optimizer
Database Password Recovery
Class
Ini
EMail validator
Graphical Class
MP3 Info
Pinger Class
IP ranger class
SoftwareMetering
Path Validator
Time Stamper Class
LoggerCLS
HTA
Keyword Ranking
DOM Explorer
HTA-Notepad
PixyPortManager
Other
flashMessage
Kill Popup
VBE decoder (fixed)
Error Codes
Maze generator
Char counter
HTML To Word
ICQ Choose User
ScreenSaver Password decoder
Mp3Playlister - singleList
Long Filename To Short
Trace Math
Dump Hexa File
Self modifying script
Mp3Playlister - multiList
HTANoid
PixyWebServer
Python
Progress indication

©2002 Jean-Luc Antoine. All Rights Reserved. Scripts or any other material on this website may not be redistributed or put as part of ANY collection (script archives, CDs etc) without prior written permission. Permission granted to use and modify any of the scripts found on InterClasse.com