Separate file name and path from a string

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Separate file name and path from a string

Post by Rain » Tue May 21, 2013 1:28 pm

This example will separate the file name and path from a string.
For Example C:\My Folder\MyFile.txt will return the following info:
Absolute Path: C:\My Folder\MyFile.txt
Parent Folder: C:\My Folder
File Name: MyFile.txt
Base Name: MyFile
Extension Name: txt

Code: Select all

VBSTART
Function GetParentFolder(FileName)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(FileName)
  GetParentFolder = objFSO.GetParentFolderName(objFile)
END Function

Function GetAbsolutePath(FileName)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(FileName)
  GetAbsolutePath = objFSO.GetAbsolutePathName(objFile)
END Function

Function GetFileName(FileName)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(FileName)
  GetFileName = objFSO.GetFileName(objFile)
END Function

Function GetBaseName(FileName)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(FileName)
  GetBaseName = objFSO.GetBaseName(objFile)
END Function

Function GetExtensionName(FileName)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(FileName)
  GetExtensionName = objFSO.GetExtensionName(objFile)
END Function
VBEND

//File path
Let>TheFilePath=C:\My Folder\MyFile.txt
IfDirExists>%TheFilePath%
Mdl>Directory Only
ELSE
  IfFileExists>%TheFilePath%
    VBEval>GetParentFolder("%TheFilePath%"),ParentFolder
    VBEval>GetAbsolutePath("%TheFilePath%"),AbsolutePath
    VBEval>GetFileName("%TheFilePath%"),FileName
    VBEval>GetBaseName("%TheFilePath%"),BaseName
    VBEval>GetExtensionName("%TheFilePath%"),ExtensionName
    mdl>Absolute Path: %AbsolutePath%%CRLF%Parent Folder: %ParentFolder%%CRLF%File Name: %FileName%%CRLF%Base Name: %BaseName%%CRLF%Extension Name: %ExtensionName%
  ENDIF
ENDIF

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue May 21, 2013 4:47 pm

I have always done this like this:

Code: Select all

// Make an example path
Let>FullPathWithFile=C:\Program Files (x86)\Macro Scheduler 14\msched.exe

Code: Select all

// Get the path and full file name and remove "\" (optional)  
Separate>%FullPathWithFile%,\,PathPieces
Let>FileName=PathPieces_%PathPieces_Count%
StringReplace>%FullPathWithFile%,\%FileName%,,PathOnly
// Get the file name only and extension and remove "." (optional)  
Separate>%Filename%,.,FilePieces
Let>FileExtension=FilePieces_%FilePieces_Count%
StringReplace>%
Filename%,.%FileExtension%,,FileOnly

Code: Select all

// Show the results
MDL>Path = %PathOnly%%CRLF%Filename = %FileName%%CRLF%Filename only = %FileOnly%%CRLF%Extension = %FileExtension%
Much shorter and simpler but I know you Rain - There must be some reason for using all that code? Please explain.
Phil Pendlebury - Linktree

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 21, 2013 9:41 pm

Or in v14 just use the built in ExtractFilePath and ExtractFileName functions:

http://www.mjtnet.com/manual/extractfilepath.htm
http://www.mjtnet.com/manual/extractfilename.htm
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Wed May 22, 2013 12:23 am

Like Marcus Said, The New Features Are Awesome.

I Had A Similar One To Phil's, However I Suspect Rain Is A Bit Of A VB Guru, Hence He Uses VB Script A Lot :P
FIREFIGHTER

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed May 22, 2013 8:29 am

Marcus Tettmar wrote:Or in v14 just use the built in ExtractFilePath and ExtractFileName functions:

http://www.mjtnet.com/manual/extractfilepath.htm
http://www.mjtnet.com/manual/extractfilename.htm
[Rushes off to re-write scripts...]

Very curious to hear Rain's take on this though. :-) Oh and I should add VB confuses me so I avoid it unless totally necessary.
Phil Pendlebury - Linktree

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Thu May 23, 2013 2:42 am

Phil Pendlebury wrote:Oh and I should add VB confuses me so I avoid it unless totally necessary.
I Concur.
FIREFIGHTER

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon May 27, 2013 2:58 am


armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon May 27, 2013 3:06 am

I found the both in the MS' Code Builders.
Just scroll down to 'String Handling'.

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Mon May 27, 2013 4:45 am

Looks ok for me using FireFox v18

Image
FIREFIGHTER

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon May 27, 2013 4:52 am

CyberCitizen,
Thanks. Later I found both on my online manual. Guess it's my typo.

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Mon May 27, 2013 6:20 am

armsys wrote:CyberCitizen,
Thanks. Later I found both on my online manual. Guess it's my typo.
Nope, not a typo. How can you mistype a clickable link, you just click on it right?

Anyway for the record: Both links are blank here too.

If however you search the manual you will find the pages. If you then right click on that page link and copy the link, then paste... Blank.

I think this is a Chrome / Cache problem.
Phil Pendlebury - Linktree

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon May 27, 2013 6:34 am

Phil,
Thanks for your feedback.
I love your music.
Do you to pay dobule tax to UK & UAE?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Mon May 27, 2013 9:06 am

armsys wrote:Phil,
Thanks for your feedback.
I love your music.
Do you to pay dobule tax to UK & UAE?
Thank you.

:lol: :lol: :lol:
Phil Pendlebury - Linktree

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue May 28, 2013 12:32 am

Phil Pendlebury wrote:Anyway for the record: Both links are blank here too.

If however you search the manual you will find the pages. If you then right click on that page link and copy the link, then paste... Blank.

I think this is a Chrome / Cache problem.
Same in IE9 too, so not just a Chrome issue.

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue May 28, 2013 9:22 am

Roger That Me. ;-)
Phil Pendlebury - Linktree

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts