15.1.2 More Path Utilities
(require racket/path) | package: base |
procedure
(file-name-from-path path) → (or/c path-for-some-system? #f)
path : (or/c path-string? path-for-some-system?)
procedure
(path-get-extension path) → (or/c bytes? #f)
path : (or/c path-string? path-for-some-system?)
See path-replace-extension for the definition of a filename extension.
> (path-get-extension "x/y.rkt") #".rkt"
> (path-get-extension "x/y") #f
> (path-get-extension "x/y.tar.gz") #".gz"
> (path-get-extension "x/.racketrc") #f
Added in version 6.5.0.3 of package base.
procedure
(path-has-extension? path ext) → (or/c bytes? #f)
path : (or/c path-string? path-for-some-system?) ext : (or/c bytes? string?)
If ext is a byte string with the shape of an extension (i.e., starting with .), this check is equivalent to checking whether (path-get-extension path) produces ext.
> (path-has-extension? "x/y.rkt" #".rkt") #t
> (path-has-extension? "x/y.ss" #".rkt") #f
> (path-has-extension? "x/y" #".rkt") #f
> (path-has-extension? "x/.racketrc" #".racketrc") #f
> (path-has-extension? "x/compiled/y_rkt.zo" #"_rkt.zo") #t
Added in version 6.5.0.3 of package base.
procedure
(filename-extension path) → (or/c bytes? #f)
path : (or/c path-string? path-for-some-system?)
NOTE: This function is deprecated; use path-get-extension, instead.
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? #:more-than-same? more-than-same? #:normalize-case? normalize-case?]) → (or/c path-for-some-system? path-string?) base : (or/c path-string? path-for-some-system?) path : (or/c path-string? path-for-some-system?) more-than-root? : any/c = #f more-than-same? : any/c = #t normalize-case? : any/c = #t
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.
If path is the same as base, then (build-path 'same) is returned only if more-than-same? is true. Otherwise, path is returned when path is the same as base.
If normalize-case? is true (the default), then pairs of path elements to be compared are first converted via normal-case-path, which means that path elements are comparsed case-insentively on Windows. If normalize-case? is #f, then path elements and the path roots match only if they have the same case.
The result is normally a path in the sense of path?. The result is a string only if path is provided a string and also returned as the result.
Changed in version 6.8.0.3 of package base: Made path elements case-normalized
for comparison by default, and
added the #:normalize-case?
argument.
Changed in version 6.90.0.21: Added the #:more-than-same?
argument.
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.
> (equal? (current-directory) (normalize-path ".")) #t
procedure
(path-element? path) → boolean?
path : any/c
procedure
(path-only path) → (or/c #f path-for-some-system?)
path : (or/c path-string? path-for-some-system?)
> (path-only (build-path "a" "b")) #<path:a/>
> (path-only (build-path "a")) #f
> (path-only (path->directory-path (build-path "a"))) #<path:a/>
> (path-only (build-path 'up 'up)) #<path:../..>
procedure
(simple-form-path path) → path?
path : path-string?
procedure
(some-system-path->string path) → string?
path : path-for-some-system?
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)
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.
> (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>