Your script reads the contents of that text file into variables, reformats the contents of those variables into HTML, and then writes the new HTML contents to another file on disk (so that you don't destroy the original text file). Therefore, you need to create a new filename for the HTML file. Yes, you could prompt the user to type in a name of his choice. But to make it easier upon him, perhaps you can create that new name yourself by chopping off the .TXT extension upon the original filename, and adding a .HTM extension to the new name.
So you need to transform the original filename into another filename.
There are many ways to do this. First, you can do it with some calls to the built-in functions that operate upon a string. For example, you can use LASTPOS() to look for the final dot (within the original filename) to locate where the extension begins. (Don't assume that an extension is always 4 characters long -- a dot and only 3 characters. Under newer Windows operating systems, an extension could have more than 3 characters, although traditionally, there are only 3 characters). Then, you can subtract one from that position and use LEFT() to extract everything except the extension. Then you can concatenate the .HTM extension to the end of that.
Obviously this involves numerous instructions, and of course, checking for errors (such as whether there may be no extension at all on the name).
/* Assume that this is the original filename. */ filename = 'C:\MyDir\MyFile.txt' /* Find the extension. */ pos = LASTPOS('.', filename) /* Was there an extension? */ IF pos > 1 THEN /* Chop off the extension */ filename = LEFT(filename, pos - 1) /* Append an .HTM extension. */ filename = filename || '.htm' /* Display the new filename. */ SAY filenameOk, that works if you wish to save the new file to the same directory as the original file. But what if you want to create a new directory and save the file there? Now you have to break off the path from the filename part, and concatenate your new directory name to the beginning of the filename part. Obviously, this is going to involve some more calls to LASTPOS() and RIGHT() to trim off everything before (and including) the final backslash character. As you can see, things are getting a little more complex.
If your interpreter offers the FILESPEC() built-in function, your work can be made easier. FILESPEC can extract a drive, directories, or filename part. So, let's say that we wish to save our new file in the directory C:\MyNewDir:
/* Assume that this is the original filename. */ filename = 'C:\MyDir\MyFile.txt' /* Find the extension. */ pos = LASTPOS('.', filename) /* Was there an extension? */ IF pos > 1 THEN /* Chop off the extension */ filename = LEFT(filename, pos - 1) /* Extract only the filename part, prepend our new directory, * and append an .HTM extension. */ filename = 'C:\MyNewDir\' || FILESPEC('N', filename) || '.htm' /* Display the new filename. */ SAY filenameThis isn't so bad. But Reginald makes things even easier, with the built-in function EDITNAME(). EDITNAME is designed to transform a filename into another name by using wildcards. EDITNAME can put a new path (of your own choosing) on the transformed filename, or keep the original path. All of this can be done with a single call to EDITNAME(). So, if we want to transform the original filename by keeping the filename part up to its extension, and then append a new extension of .HTM, and prepend a new path of C:\MyNewDir, we simply do the following:
filename = 'C:\MyDir\MyFile.txt' filename = EDITNAME(filename, "C:\MyNewDir\*.htm", "S") SAY filenameLet's take another example. Suppose you wish to keep the same extension (as the original) on the new filename. But you want to add a '2' to the filename part. In other words, if the original filename is C:\MyDir\MyFile.txt, then you want a new name of C:\MyDir\MyFile2.txt. Here is one way of doing it:
/* Assume that this is the original filename. */ filename = 'C:\MyDir\MyFile.txt' /* Assume no extension. */ ext = "" /* Find the extension. */ pos = LASTPOS('.', filename) /* Was there an extension? */ IF pos > 1 THEN /* Save the extension */ ext = RIGHT(filename, LENGTH(filename) - (pos - 1)) /* Chop off the extension */ filename = LEFT(filename, pos - 1) /* Insert a '2' between the filename and extension. */ filename = filename || '2' || ext /* Display the new filename. */ SAY filenameUsing Reginald's EDITNAME is much easier:
filename = 'C:\MyDir\MyFile.txt' filename = EDITNAME(filename, "*2.*", "U") SAY filenameNote: None of the above examples check that the resulting name is a file or directory that actually exists. Use MATCHNAME check to see whether there is an existing file/dir by that name. You may want to do this before you use the name to create a new file/dir, and if there is an existing file/dir with that same name, warn the user about overwriting it, or allow him to choose a new name.