► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

► | 15 | Operating System

15.1 | Paths
15.2 | Filesystem
15.3 | Networking
15.4 | Processes
15.5 | Logging
15.6 | Time
15.7 | Environment Variables
15.8 | Environment and Runtime Information
15.9 | Command-Line Parsing

▼ | 15.1 | Paths

15.1.1 | Manipulating Paths
15.1.2 | More Path Utilities
15.1.3 | Unix and Mac OS X Paths
15.1.4 | Windows Path Conventions

On this page:

file-name-from-path
filename-extension
find-relative-path
normalize-path
path-element?
path-only
simple-form-path
some-system-path->string
string->some-system-path
shrink-path-wrt

top ← prev up next →

##### 15.1.2`` More Path Utilities

(require racket/path) | package: base

The bindings documented in this section are provided by the racket/path and racket libraries, but not racket/base.

> procedure (file-name-from-path path) → (or/c path-for-some-system? #f)
> path : (or/c path-string? path-for-some-system?)

Returns the last element of path. If path is syntactically a directory path (see split-path), then the result is #f.

> procedure (filename-extension path) → (or/c bytes? #f)
> path : (or/c path-string? path-for-some-system?)

Returns a byte string that is the extension part of the filename in path without the . separator. If path is syntactically a directory (see split-path) or if the path has no extension, #f is returned.

> > procedure
> > (find-relative-path |  | base |
> > |  | path |
> > | [ | #:more-than-root? more-than-root?]) |
> → path-for-some-system?
> base : (or/c path-string? path-for-some-system?)
> path : (or/c path-string? path-for-some-system?)
> more-than-root? : any/c = #f

Finds a relative pathname with respect to base that names the same file or directory as path. Both base and path must be simplified in the sense of simple-form-path. If path shares no subpath in common with base, path is returned.

If more-than-root? is true, if base and path share only a Unix root in common, and if neither base nor path is just a root path, then path is returned.

> procedure (normalize-path path [wrt]) → path?
> path : path-string?
> | wrt |  | : |  | (and/c path-string? complete-path?)
> |  |  | = |  | (current-directory)

> > > For most purposes, simple-form-path is the preferred mechanism to normalize a path, because it works for paths that include non-existent directory components, and it avoids unnecessarily expanding soft links.

Returns a complete version of path by making the path complete, expanding the complete path, and resolving all soft links (which requires consulting the filesystem). If path is relative, then wrt is used as the base path.

Letter case is not normalized by normalize-path. For this and other reasons, such as whether the path is syntactically a directory, the result of normalize-path is not suitable for comparisons that determine whether two paths refer to the same file or directory (i.e., the comparison may produce false negatives).

An error is signaled by normalize-path if the input path contains an embedded path for a non-existent directory, or if an infinite cycle of soft links is detected.

> procedure (path-element? path) → boolean?
> path : any/c

Returns #t if path is a path element: a path value for some platform (see path-for-some-system?) such that split-path applied to path would return 'relative as its first result and a path as its second result. Otherwise, the result is #f.

> procedure (path-only path) → (or/c #f path-for-some-system?)
> path : (or/c path-string? path-for-some-system?)

If path is a filename, the file’s path is returned. If path is syntactically a directory, path is returned (as a path, if it was a string). If path has no directory part #f is returned.

> procedure (simple-form-path path) → path?
> path : path-string?

Returns (simplify-path (path->complete-path path)), which ensures that the result is a complete path containing no up- or same-directory indicators.

> procedure (some-system-path->string path) → string?
> path : path-for-some-system?

Converts path to a string using a UTF-8 encoding of the path’s bytes.

Use this function when working with paths for a different system (whose encoding of pathnames might be unrelated to the current locale’s encoding) and when starting and ending with strings.

> procedure (string->some-system-path str kind) → path-for-some-system?
> str : string?
> kind : (or/c 'unix 'windows)

Converts str to a kind path using a UTF-8 encoding of the path’s bytes.

Use this function when working with paths for a different system (whose encoding of pathnames might be unrelated to the current locale’s encoding) and when starting and ending with strings.

> procedure (shrink-path-wrt pth other-pths) → (or/c #f path?)
> pth : path?
> other-pths : (listof path?)

Returns a suffix of pth that shares nothing in common with the suffixes of other-pths, or pth, if not possible (e.g. when other-pths is empty or contains only paths with the same elements as pth).

Examples:
> > (shrink-path-wrt (build-path "racket" "list.rkt")
> (list (build-path "racket" "list.rkt")
> (build-path "racket" "base.rkt")))
> #<path:list.rkt>
> > (shrink-path-wrt (build-path "racket" "list.rkt")
> (list (build-path "racket" "list.rkt")
> (build-path "racket" "private" "list.rkt")
> (build-path "racket" "base.rkt")))
> #<path:racket/list.rkt>

top ← prev up next →