There is a seldom used (I hope) technique called "the self modifying program" that, like a GOTO, when used properly can be very convenient. Here is an example of such a program written in vbScript
FILELOC = ""
FILENAM = ""
'keep the above two lines as the first two lines in this script.
set fso = CreateObject("Scripting.FileSystemObject")
'prompt for FILELOC and FILENAM if either value is not already defined
if FILELOC = "" or FILENAM = "" then
SetParms
end if
'replace the first two lines of code in this file with values
'input by the user
Sub SetParms()
dim pgm: pgm = wscript.ScriptFullName
dim txt: txt = Split(fso.OpenTextFile(pgm).ReadAll,vbCrLf)
txt(0) = "FILELOC = """ & GetParam("Enter File Location: ") & """"
txt(1) = "FILENAM = """ & GetParam("Enter File Name: ") & """"
fso.OpenTextFile(pgm,2).Write(Join(txt,vbCrLf))
End Sub
'prompt for and read a string value from the console
Private Function GetParam(prompt)
wscript.StdOut.Write prompt
GetParam = wscript.StdIn.ReadLine
End Function
When the program runs, it checks to see if the configuration values have been defined. If not then the user is prompted to enter them. The program then reads its own source file from disk, modifies the first two lines, then writes itself back to disk. The benefit is that you only ever need the one file. To use this program, copy the code into a file with a "vbs" extension. To run it you can either
cscript myfile.vbs
or you can set cscript.exe to the default by
cscript //nologo /'h:cscript //s
and then you can run it by typing only
myfile
To make it more robust you should do some error checking on the input values to make sure that
- the entered file path exists
- the entered file name exists
Just plug in whatever extra code you need to do the specific task.
Reverend Jim
Carpe per diem
3,600 posts since Aug 2010
Reputation Points: 561
Solved Threads: 447
Skill Endorsements: 32