s c h e m a t i c s : c o o k b o o k

/ Cookbook.FileNameFromPath

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Extracting the File Name from a Path

Problem

You want to extract just the file name from a path. For example, given a path such as "foo/bar/baz.ss" you want to extract "baz.ss"

Solution

Use file-name-from-path from the file.ss module in MzLib?. This function returns the file name specified by a path, if one exists, or #f. For example:

> (require (lib "file.ss"))
> (current-directory)
#<path:/home/pg/nhw/shared/annealing/analysis>
> (file-name-from-path (build-path (current-directory) "plot.ss"))  ; a file
#<path:plot.ss>
> (file-name-from-path (current-directory)) ; a directory
#<path:analysis>
> (file-name-from-path "..") ; no name exists
#f

Discussion

Note that the string returned by file-name-from-path may actually be the name of a directory, if the path denotes a directory.

If the last part of the path denotes a relative directory or the current directory (e.g. ".." or ".") then #f will be returned. Use simplify-path to ensure a file name does exist.

file-name-from-path uses the built-in split-path to split a path into constituents.


Comments about this recipe

Contributors

-- NoelWelsh - 01 Feb 2006

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest