Example scripts and tips (replaces Old Scripts & Tips archive)
Moderators: Dorian (MJT support), JRL, Phil Pendlebury
-
Rain
- Automation Wizard
- Posts: 550
- Joined: Tue Aug 09, 2005 5:02 pm
-
Contact:
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
-
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.
-
CyberCitizen
- Automation Wizard
- Posts: 723
- 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
FIREFIGHTER
-
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
[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.
-
CyberCitizen
- Automation Wizard
- Posts: 723
- 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'.
-
CyberCitizen
- Automation Wizard
- Posts: 723
- 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
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.
-
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.
-
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?
-
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.
-
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.