How to get filename from string?

Rick Stanich

King and Supreme Ruler
Local time
Today, 00:14
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:
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:
Brilliant!
It never crossed my mind to use the last backlash as a delimiter. :rolleyes:
 
just edited the previos post - you need to startfrom 1 pos AFTER the backslash, so added a +1
 
haha
This is what happens when you (I) dont know the syntax.
I solved that by using replace.
Again, thank you.
 
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.
 
If the file indicated by the filename already exists then you could use:

strFile = Dir(strFileName)

Regards,
Pete
 
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

Back
Top Bottom