Directory Manipulation Functions


Some functions apply equally well to directories as well as files. This section is concerned with those that apply only to directories.

The function CurDir, with a drive specifier as the argument, returns the current directory for the specified drive. See Listing 2 and Figure 1 . If the argument is omitted, the current directory for the current drive is returned. The drive specifier is ignored on Unix systems. The initial value of the current directory is system dependent and may change depending upon how OOo is started. If you start OOo from a command-line prompt, you'll likely have a different current directory than if you start OOo from a menu or another application. For some operating systems, using File Open to open an existing document sets the current directory to the directory containing the opened document (I have seen this in Windows). In some operating systems, such as Linux, this does not affect the current directory. Do not rely on this behavior!

click to expand
Figure 1: CurDir returns the current directory.
Listing 2: ExampleCurDir is found in the File module in this chapter's source code files as SC07.sxw.
start example
 Sub ExampleCurDir   MsgBox "Current directory on this computer is "&_          CurDir, 0, "Current Directory Example" End Sub 
end example
 

The functions ChDir and ChDrive, although present in OOo Basic, do nothing; they will eventually be removed from the language. Their original purpose was to change the current drive and directory. Removing ChDir and ChDrive improves system independence. However, Unix computers do not support the notation of a drive letter, so ChDrive is not applicable . Furthermore, the initial current directory is dependent upon the operating system and how OOo was opened. The initial values, therefore, cannot be assumed, and thus ChDir is also useless.

Compatibility  

Visual Basic supports ChDir and ChDrive. OOo Basic supports these two functions only to the extent that they don't cause a run-time error.

Use the MkDir function to create a directory, and RmDir to remove a directory. You can create and remove multiple directory levels at one time. For example, in Listing 3 , "/home/andy" existed before the code ran. The entire directory path is created from the argument to MkDir. If an absolute path is not provided, the created directory is relative to the current directory as obtained with the function CurDir. The function RmDir removes the directory, all directories below it, and all files contained in the directories.

Listing 3: In this example, only "/home/andy" existed previously.
start example
 MkDir("/home/andy/dum/a/b/c")     'This works on my Linux computer RmDir("/home/andy/dum")           'This works on my Linux computer MkDir("c:\home\andy\dum\a\b\c")   'This works on my Windows computer RmDir("c:\home\andy\dum")         'This works on my Windows computer 
end example
 

The code in Listing 3 uses absolute paths. It's possible to use relative paths, but I strongly discourage it. The behavior of the current directory is operating-system dependent.

File- related functions that also work with directories include Dir, FileDateTime, FileExists, FileLen, GetAttr, and Name . These are discussed later.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net