Using URL Notation to Specify a File


Many of the functions in Table 1 specify a file or path . These functions accept both system-specific names and Uniform Resource Locator (URL) notation. This is the same notation used by your Web browser. Table 2 shows examples.

Table 2: URL examples.

System

System Path

URL Path

Windows

c:\Temp\help.txt

file:///c:/Temp/help.txt

Windows

c:\My Documents

file:///c:/My%20Documents

Unix

/home/andy/Temp/help.txt

file:///home/andy/Temp/help.txt

Unix

/home/andy/My Documents

file:///home/andy/My%20Documents

Tip  

The statement "Shell("C:\Prog Files\calc.exe",2)" failed because there is a space in the path. The Shell statement passes the string to the command shell, which interprets the portion of the path before the space as the program to run. URL notation avoids this problem.

One advantage of URL notation is that special characters are encoded. Arguments that are passed to a shell, for example, frequently have problems with paths that contain a space. In URL notation, spaces are encoded as "%20" (see Table 2). Use the functions ConvertToURL to convert a system-specific path to URL notation and ConvertFromURL to convert to a system-specific path.

 Print ConvertToURL("/home/andy/logo.miff") Print ConvertFromURL("file:///home/andy/logo.miff")  'This requires UNIX Print ConvertToURL("c:\My Documents")                'This requires Windows Print ConvertFromURL("file:///c:/My%20Documents")    'This requires windows 

Special characters, such as the space, are encoded with a percent sign (%) followed by the ASCII value of the character encoded as a two-digit hexadecimal number. The space character has an ASCII value of 32, which is 20 in hexadecimal format. This is why a space is encoded as %20.

 Print ConvertFromURL("file:///%41%42%43/%61%62%63")  '/ABC/abc   (UNIX) Print ConvertFromURL("file://c:/%41%42%43/%61%62%63")'/ABC/abc   (Windows) 

URL notation is system independent, so URL paths work as well on an Apple computer as they do on a Windows computer. To create a system-specific path, use the function GetPathSeparator to obtain the system-specific path separator. Listing 1 demonstrates how to use GetPathSeparator to build a complete path. Windows-based computers use "\" as the path separator, and Unix-based computers use "/" as the path separator. URL notation uses "/" as the separator regardless of the operating system.

Listing 1: Use GetPathSeparator() rather than "\" or"/".
start example
 sPathToFile = getFilePathFromUser() sBookName   = getBookNameFromUser() sPathToBook = sPathToFile & GetPathSeparator() & sBookName 
end example
 
Compatibility  

Visual Basic for Applications (VBA) does not support the function GetPathSeparator, but it does have the property Application.PathSeparator, which always returns a backslash, even on a Macintosh computer. VBA also does not support ConvertToURL or ConvertFromURL.




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