Convenience function to shorten relative paths based on overlap with the provided root(s). Also simplifies current directory repeats (e.g. "././." becomes ".") for better legibility. Does not check whether the original or resulting paths point to valid files or folders. Relative paths that do not start with the supplied root default back to the current working directory (.). Absolute paths are allowed but are returned as is without attempts at shortening. See iso_find_absolute_path_roots for rooting absolute paths.

iso_shorten_relative_paths(path, root = ".")

Arguments

path

vector of file/folder paths, mixed relative and absolute paths are allowed.

root

root directory for the isofiles. Can be relative to the current working directory (e.g. "data") or an absolute path on the file system (e.g. "/Users/..." or "C:/Data/.."). The default is the current working directory ("."). Can be supplied as a vector of same length as the provided paths if the paths have different roots.

Value

a data frame with the root directories and paths relative to the root - order of input paths is preserved

See also

Other file system functions: iso_expand_paths(), iso_find_absolute_path_roots(), iso_root_paths()

Examples

iso_shorten_relative_paths(file.path("A", "B", "C"), "A") # root = "A", path = B/C
#> # A tibble: 1 × 2
#>   root  path 
#>   <chr> <chr>
#> 1 A     B/C  
iso_shorten_relative_paths(file.path("A", "B", "C"), file.path("A", "B")) # root = "A/B", path = "C"
#> # A tibble: 1 × 2
#>   root  path 
#>   <chr> <chr>
#> 1 A/B   C    
iso_shorten_relative_paths(file.path("A", "C", "D"), file.path("A", "B")) # root = "A", path = "C/D"
#> # A tibble: 1 × 2
#>   root  path 
#>   <chr> <chr>
#> 1 A     C/D  
iso_shorten_relative_paths(file.path("A", "B", "C"), "B") # root = ".", path stays "A/B/C"
#> # A tibble: 1 × 2
#>   root  path 
#>   <chr> <chr>
#> 1 .     A/B/C