Files and Directories

 

Next Topic

 

The WIL Tutorial

§         Topic by Topic

§         The Tutorial Course

§         The Complete WIL Tutorial

 

Getting started

Using WIL

Reference

Notes

DirChange (pathname)

 

Changes the directory to the pathname specified.

Use this function when you want to run a program which must be started from its own directory. "Pathname" may optionally include a drive letter.

Example:
DirChange("c:\windows\winword")
Run("winword.exe", "")            
                
DirGet ( )

Gets the current working directory.

This function is especially useful in conjunction with DirChange, to save and then return to the current directory.

Example:
origdir = DirGet( )
DirChange("c:\windows\winword")
Run("winword.exe", "")
DirChange(origdir)
                

 

FileExist (filename)

Determines if a file exists.

This function will return @TRUE if the specified file exists, and @FALSE if it doesn't exist.

Example:
If FileExist("win.bak") == @false
 FileCopy("win.ini", "win.bak", @false)
EndIf
Run("notepad.exe", "win.ini")
                

 

FileCopy (source-list, destination, warning)

Copies files.

If warning is @TRUE, WIL will pop up a dialog box warning you if you are about to overwrite an existing file, and giving you an opportunity to change your mind, along with selecting various options for copying the files. If warning is @FALSE, it will overwrite existing files with no warning.

Example:
FileCopy("win.ini", "*.sav", @true)
Run("notepad.exe", "win.ini")
                

The asterisk (*) is a wildcard character, which matches any letter or group of letters in a file name. In this case, it will cause WIN.INI to be copied as WIN.SAV.

 

FileDelete (file-list)

Deletes files.

Example:
If FileExist("win.bak")==@true Then FileDelete ("win.bak")
                

 

FileRename (source-list, destination)

Renames files to another set of names.

We can illustrate the use of these WIL program functions with a typical WIL application. Let's suppose that our word processor saves a backup copy of each document, with a BAK extension, but we want a larger safety net when editing important files. We want to keep the five most recent versions of the document that we're writing. Here's a WIL program to accomplish this:

Example:
If FileExist("wil.bak") == @true
 FileDelete("wil.bk5")
 FileRename("wil.bk4", "wil.bk5")
 FileRename("wil.bk3", "wil.bk4")
 FileRename("wil.bk2", "wil.bk3")
 FileRename("wil.bk1", "wil.bk2")
 FileRename("wil.bak", "wil.bk1")
EndIf
Run("winword.exe", "wil.doc")
Exit

 

If the file WIL.BAK exists, it means that we have made a change to WIL.DOC. So, before we start editing, we delete the oldest backup copy, and perform several FileRename functions, until eventually WIL.BAK becomes WIL.BK1.

However, this still isn't quite right. What would happen if the file WIL.BK5 didn't exist? In the DOS batch language, we would get an error message, and processing would continue. But in WIL, the error would cause the WIL program to terminate resulting in a message resembling the following:

 

image\tutbut10_shg.gif

 

There are two ways that we can handle this. We could use an If FileExist test before every file operation, and test the returned value for a @TRUE before proceeding. But this is clumsy, even with such a small WIL program, and would become unwieldy with a larger one.