Working with file paths and directories

Red by default uses the forward slash, /in some sort of universal path notation. The / character denotes the root of the current drive, ./ denotes the current folder, and ../ points to the folder one step up in the file hierarchy. As we have seen in the guess-number.red script from Chapter 4, Code-Controlling Structures, this ../ notation is used to denote a path relative to the current folder. An absolute path starts with a drive-letter or with the root drive /.

Use relative paths instead of absolute paths to make your scripts machine-independent. Also make sure that your program can run on Windows as well as on Linux or OS X.

Converting a string to a file is done with to-file, but only the syntax changes, nothing else is checked.

If you need to transform a file path to a specific platform notation such as Windows, use the to-local-file function, such as in the following:

;-- see Chapter07/working-with-files-directories.red:
to-local-file %/E/Red/red.bat ;== "E:Red ed.bat"

Conversely, to convert a specific path notation to a platform-independent format, use to-red-file, as shown in the following code:

to-red-file "E:Red
ed.bat"    ;== %/E/Red/red.bat

Both of the preceding functions have a /full refinement to turn the file path into an absolute path if needed.

to-local-file/full %red.bat     ;== "E:Red
ed.bat"

Note that Red will automatically transform a platform-specific path with to a standard notation, if you enclose the path within "":

write %"C:ProgramDataRed	est.txt" "test"

Here is a quick overview of some handy functions for your reference:

  • Both pwd and what-dir return the current working folder, which at the start is the folder from which the current program is executed—for example, for the REPL—pwd ;== %/C/ProgramData/Red/get-current-dir returns the same value in string format—"C:ProgramDataRed".
  • Both cd and change-dir change the current directory, and with it the values of the functions in the previous bullet—cd %/E/test, ;== %/E/test/, and pwd ;== %/E/test/.
  • Each of the words dir,  ls, and list-dir folder  gives you the contents of the current or the given folder.
  • The characters file? and dir? respectively test whether their argument is a file or a folder:
      file? %/E/Red/red.bat ;== true
dir? %/E/Red/ ;== true (trailing / is needed)
  • The phrase make-dir creates a new folder, with no error if this already exists: make-dir %scripts.
  • To test whether a file or folder really exists, use exists?:
      exists? %/E/Red/red.bat    ;== true
exists? %/E/Red/red2.bat ;== false
exists? %/E/NotExist/ ;== false
  • The  suffix? phrase returns the extension of a file—suffix? %/E/Red/red.bat ;== %.bat.
  •  The size? phrase returns a file's size in bytes, or none if it does not exist—size? %red.bat  ;== 22.
  • To delete a file, simply use delete %file. This returns true when it succeeds.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86