Code covered by the BSD License  

Highlights from
GetFullPath

5.0

5.0 | 7 ratings Rate this file 31 Downloads (last 30 days) File Size: 9.96 KB File ID: #28249

GetFullPath

by Jan Simon

 

21 Jul 2010 (Updated 03 Nov 2011)

Full path name for partial or relative path

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

GetFullPath - Get absolute path of a file or folder name

This function converts a partial or relative name to an absolute full path name. The fast Mex works on Windows only (sorry), but the M-file runs on Windows, MacOS and Unix.

FullName = GetFullPath(Name)
INPUT:
  Name: String or cell string, file or folder name with relative or absolute path.
OUTPUT:
  FullName: String or cell string, file or folder name with absolute path.
      The created path need not exist.

EXAMPLES:
  cd(tempdir); % Assuming C:\Temp here
  GetFullPath('File.Ext') % ==> 'C:\Temp\File.Ext'
  GetFullPath('..\File.Ext') % ==> 'C:\File.Ext'
  GetFullPath('..\..\File.Ext') % ==> 'C:\File.Ext'
  GetFullPath('.\File.Ext') % ==> 'C:\Temp\File.Ext'
  GetFullPath('*.txt') % ==> 'C:\Temp\*.txt'
  GetFullPath('D:\Folder1\..\Folder2') % ==> 'D:\Folder2'
  GetFullPath('..') % ==> 'C:\'
  GetFullPath('\') % ==> 'C:\', current drive!
  GetFullPath('Folder\') % ==> 'C:\Temp\Folder\'
  GetFullPath('\\Server\Folder\Sub\..\File.ext')
  % ==> '\\Server\Folder\File.ext'

See also (timings converted with GetFullPath):
  WHICH: only for existing files, ~24 times slower.
  System.IO.FileInfo: .NET (thanks Urs), more features, ~50 times slower.
  java.io.File: "/.." and "/." are fixed by getCanonicalPath (~6 times slower),
      but no completing of partial/relative path.

Tested: Matlab 6.5, 2008a, 2009a, 2001b, WinXP/32, Win7/64
Compiler: LCC 2.4/3.8, OpenWatcom 1.8, BCC 5.5, MSVC 2008/2010
Precompiled Mex: http://www.n-simon.de

Suggestions and question by email or in the comment section are very welcome - especially a test under Linux and MacOS.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Mex for Windows, M for Linux/MacOS
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
22 Jul 2010 Thomas Schreiter

Exactly what I needed.
I included this GetFullPath into my setup files which prepare the Matlab paths. And now these paths do not contain '\..' anymore. Effect: Matlab uses only one path for a file instead of multiple, which had confused the debugger.

22 Jul 2010 us

jan

this should be corrected
- System.IO.FileInfo is a .NET component (available only in more recent ML versions)
- java.io.File is the java class

urs

22 Jul 2010 Jan Simon

Thanks Urs! The doc is fixed.

26 Jul 2010 Oliver Woodford

For those people wanting a function that works across platforms, doesn't require mexing, and who don't care so much about speed, an alternative is:
http://www.mathworks.com/matlabcentral/fileexchange/23603-fullpath

27 Jul 2010 Jan Simon

@Oliver: Or use the shipped M-version, which runs on Win/MacOS/Linux, considers the cases "C:", "..", "." and "~/", and does not change the current path.
If you comment out the warning about the unfound MEX, GetFullPath.m is 2.5 times faster than FULLPATH for files without path, and 40 times faster for file with a realtive or full path (WinXP, 2009a).

02 Aug 2010 Oliver Woodford

Yes, the non-mex version (which I didn't originally see) is very good.

01 Mar 2011 Eugeny Sosnovsky  
20 Aug 2011 Igor  
17 Oct 2011 Daniel

I am not sure this works on Linux ...
GetFullPath('/home/test/../')
gives /ho/home/

04 Nov 2011 Jan Simon

@Daniel: I've tried to improve the code, but cannot test it under Linux.

09 Dec 2011 Paul Sexton

I was having the same problem as Daniel (with the 01May2011 version). Using the 03Nov2011 version fixes that behavior.

Running the tests on Linux (32-bit) still breaks though:

>> uTest_GetFullPath
==== Test GetFullPath 09-Dec-2011 14:21:31
  Function: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/GetFullPath.m
  Current path: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011

  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011, Folder: [Folder/]
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011, File: File
  ok: ../Folder/
  ok: ../../Folder/
  ok: ../../../Folder/
  ok: ../../../../Folder/
  ok: ../../../../../Folder/
  ok: ../File
  ok: ../../File
  ok: ../../../File
  ok: ../../../../File
  ok: ../../../../../File
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../Folder/
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../Folder/
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../Folder/
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../../Folder/
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../../../Folder/
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../File
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../File
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../File
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../../File
  ok: /home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/../../../../../File
Path: [/home/psexton/Documents/MATLAB/GetFullPath_03Nov2011/..] ==> error
  GetFullPath replied: [/home/psexton/Documents/MATLAB]
  Expected: [home/psexton/Documents/MATLAB]
Error using uTest_GetFullPath (line 202)
Error using uTest_GetFullPath (line 196)
uTest_GetFullPath: GetFullPath with folder failed

27 Feb 2012 Kiran  
23 Mar 2012 Oscar  
14 Apr 2012 Igor

It looks like I've found another bug - if one try to use an 255+ long UNS path as GetFullPath argument, (for example: nested GetFullPath) this may result in duplicate prefix, i.e. invalid filename.

---------------------------------------
K>> GetFullPath('I:\ROSPAN International\Suzunskoye field\Well 969\X-ray micro-CT\1. Suzun 82180-82181\Tfit\\incomplete\smp_w_fdiff 2012-04-13 20.43.59\\fit_Tall_IR\/fit_Tall_IR_2(inscribed square)//unaligned//Histograms0001/errorbar_runmean_mask (vol1-vol2)_vs_mean(vol1(mask))')
ans =
\\?\I:\ROSPAN International\Suzunskoye field\Well 969\X-ray micro-CT\1. Suzun 82180-82181\Tfit\incomplete\smp_w_fdiff 2012-04-13 20.43.59\fit_Tall_IR\fit_Tall_IR_2(inscribed square)\unaligned\Histograms0001\errorbar_runmean_mask (vol1-vol2)_vs_mean(vol1(mask))
K>> GetFullPath( GetFullPath('I:\ROSPAN International\Suzunskoye field\Well 969\X-ray micro-CT\1. Suzun 82180-82181\Tfit\\incomplete\smp_w_fdiff 2012-04-13 20.43.59\\fit_Tall_IR\/fit_Tall_IR_2(inscribed square)//unaligned//Histograms0001/errorbar_runmean_mask (vol1-vol2)_vs_mean(vol1(mask))'))
ans =
\\?\UNC\?\I:\ROSPAN International\Suzunskoye field\Well 969\X-ray micro-CT\1. Suzun 82180-82181\Tfit\incomplete\smp_w_fdiff 2012-04-13 20.43.59\fit_Tall_IR\fit_Tall_IR_2(inscribed square)\unaligned\Histograms0001\errorbar_runmean_mask (vol1-vol2)_vs_mean(vol1(mask))
K>>

19 Apr 2012 Jan Simon

Thanks Igor! There is another problem if the the full path has 254 characters. While Windows accept this, Matlab's DIR command doesn't. I'm going to post an improved version soon.

Please login to add a comment or rating.
Updates
22 Jul 2010

Doc updated, function not touched.

27 Jul 2010

M-Version replied bad path for input with leading separator. GetFullPath('\') is the current drive on Windows.

31 Jan 2011

Accept cell strings as inputs.

31 Mar 2011

Bugfix in M-version: Treat empty matrix as empty string.

01 May 2011

Minor bug: '***' in the ErrorID caused bad messages in case of errors.

03 Nov 2011

C-Mex tested under Win7/64. Improved UNC path support in the M-file. Bugfix for M-file under Linux (thanks Daniel).

Tag Activity for this File
Tag Applied By Date/Time
full Jan Simon 21 Jul 2010 12:21:28
name Jan Simon 21 Jul 2010 12:21:28
path Jan Simon 21 Jul 2010 12:21:28
file Jan Simon 21 Jul 2010 12:21:28
parent directory Jan Simon 21 Jul 2010 12:21:28
qualified Jan Simon 21 Jul 2010 12:21:28
potw Lindsay Coutinho 01 Apr 2011 14:45:22
pick of the week Lindsay Coutinho 01 Apr 2011 14:45:22
relative Jan Simon 04 Nov 2011 09:15:02

Contact us at files@mathworks.com