1 Introduction
2 Function Reference
get-file-or-folder-name
name-as-string
filename-main
filename-suffix
compose-name
make-unique-name
parent-directory
make-unique-path
path-equal?
file=?
move-folder-to
copy-folders/ renaming
file-is-visible?
count-lines
move-file-to
file-equal?
Version: 5.3

File Utilities

Erich Rast <erich ’at snafu.de>

1 Introduction

The File Utilities library is a collection of helper functions for dealing with paths and files.

Note: Most of this library has been developed for older versions of Racket. You should check if there is a built-in Racket library function with the same functionality before using one from this library.

2 Function Reference

 (require (planet erast/file-utils:1:=0))

procedure

(get-file-or-folder-name file-path)  string?

  file-path : (or/c string? path?)
Returns the name part from a file or folder path.

procedure

(name-as-string file-path)  string?

  file-path : (or/c string? path?)
If file-path is a already string, it is returned without change, otherwise the name part of the path is returned as by get-file-or-folder-name.

procedure

(filename-main file-path)  string?

  file-path : (or/c string? path?)
Returns the name part of a file or folder path without the suffix.

procedure

(filename-suffix file-path)  string?

  file-path : (or/c string? path?)
Returns the suffix part of a file or folder path. If there are multiple suffixes, only the last one is returned. If no suffix is present, an empty string is returned.

procedure

(compose-name name n suffix)  string?

  name : string?
  n : (or/c number? boolean?)
  suffix : string?
Composes a file name out of a main part name, a number, and a suffix string. If the number part n is #f then no number will be added to the name part.

procedure

(make-unique-name name folder)  string?

  name : (or/c string? path?)
  folder : path?
Returns a name string that is guaranteed to be unique within folder. Caution: This procedure creates neat file names such as "file-3.txt" for a given name "file.txt" but does so at the cost of checking for existing files with number suffixes 1, 2, ..., so for performance reasons you ought not use this function to create many files with the same base and suffix part within a folder.

procedure

(parent-directory path)  (or/c path? boolean/c)

  path : path?
Returns the parent directory of a folder or #f if there is none.

procedure

(make-unique-path suggested-path)  path?

  suggested-path : path?
Returns a file or directory path that is unique within the parent directory of suggested-path. The same performance caveat as for make-unique-name applies.

procedure

(path-equal? p1 p2)  boolean/c

  p1 : path?
  p2 : path?
Returns #t if the two paths given likely point to the same location in the file system, #f otherwise. This method normalizes the paths, but might yield false negatives under rare circumstances. Use file=? for a slower, but more reliable method to compar e whether two paths point to the same file.

procedure

(file=? file1 file2)  boolean/c

  file1 : path?
  file2 : path?
Returns #t if the two file paths alias the same file in the file system, #f otherwise. This method requires read permissions on the files and temporarily opens the files for reading. It cannot be used to compare directories.

procedure

(move-folder-to source-directory    
  destination-directory)  void/c
  source-directory : path?
  destination-directory : path?
Moves the source directory into the destination directory. This method might fail with a filesystem exception if the source and destination directories do not reside on the same volume.

procedure

(copy-folders/renaming source-directory    
  dest-directory    
  pred?)  void/c
  source-directory : path?
  dest-directory : path?
  pred? : (path? .->. boolean/c)
Copy all files in the source directory for which pred? returns true into the destination directory. If a file or directory with the same name already exists in the destination directory, then the target file or folder is renamed by adding a number to it before its suffix.

procedure

(file-is-visible? file)  boolean/c

  file : path?
Returns #t if the file is visible according to Unix naming conventions, #f otherwise. This function only checks whether the file name starts with "." and might not correctly identify other types of invisible files (e.g. on OS X).

procedure

(count-lines [port])  integer/c

  port : port? = (current-input-port)
Count the lines in the port. The port must support line counting or else an exception is raised.

procedure

(move-file-to source target)  void/c

  source : path?
  target : path?
Move the file at source to the directory target. This function also works accross volume boundaries, in case of which the file is copied and the original file is deleted.

procedure

(file-equal? file1 file2)  boolean/c

  file1 : path?
  file2 : path?
Returns #t if the given files are byte-for-byte identical, #f otherwise.