How to get filename from string? (1 Viewer)

Rick Stanich

King and Supreme Ruler
Local time
Today, 14:12
Joined
May 13, 2009
Messages
93
(SOLVED) How to get filename from string?

The code below is a snippet from this forum (Thank you).
I am trying to get the file name (remove path) from the variable "strInputFileName".
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.out)", "*.OUT")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an Input file...", _
Flags:=ahtOFN_HIDEREADONLY)
Me!txtCustFilePath = strInputFileName

The path can vary, the drive will always be a network drive but could someday be renamed (you know how those IT types are. :eek: )
File path example:
\\Mainnet\netdrive\CNC Mill\#19 CNC CMM\Inspection Reports\SUNSTR\4506667\Raw data\33557-1\235\4506667 235A 33557-1 090403.OUT
The string "4506667 C 235A 33557-1 090403.OUT" is the file name. Right now the only portion of the entire path\filename that may remain the same is \\Mainnet\netdrive\CNC Mill\#19 CNC CMM\Inspection Reports\, the network drive, the file name will always have the same formatting. Spaces indicate an end of the string for the file name.
Example:
4506667
C
235A
33557-1
235
090403
And to make it interesting, character length will never remain the same. :D

:rolleyes:

All hints, tips and or examples are appreciated.
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:12
Joined
Sep 12, 2006
Messages
15,679
find the position of the last backslash

the remainder of the string is the filename

in access after A97 you have instrrev

so (I think)

filename = mid(filename,instrrev(filename,"\")+1)
 
Last edited:

Rick Stanich

King and Supreme Ruler
Local time
Today, 14:12
Joined
May 13, 2009
Messages
93
Brilliant!
It never crossed my mind to use the last backlash as a delimiter. :rolleyes:
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:12
Joined
Sep 12, 2006
Messages
15,679
just edited the previos post - you need to startfrom 1 pos AFTER the backslash, so added a +1
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 14:12
Joined
May 13, 2009
Messages
93
haha
This is what happens when you (I) dont know the syntax.
I solved that by using replace.
Again, thank you.
 

Mr. B

"Doctor Access"
Local time
Today, 16:12
Joined
May 20, 2009
Messages
1,932
To find the last occurance of the backslash (or any other character or delimiter) use:

dim varCharLoc
varCharLoc = InStrRev("String/to/be/searched", "/")

Using the code above, the value that will be returned to the variable "varCharLoc" would be 13.
 

petehilljnr

Registered User.
Local time
Today, 14:12
Joined
Feb 13, 2007
Messages
192
If the file indicated by the filename already exists then you could use:

strFile = Dir(strFileName)

Regards,
Pete
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 14:12
Joined
May 13, 2009
Messages
93
Thanks to all who contributed!

What I worked out:
Replace(Mid(strInputFileName, InStrRev(strInputFileName, "\")), "\", "")
Of course I will simplify this to one of the prior suggestions.
 

Users who are viewing this thread

Top Bottom