Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving Path and file name without Drive Name

Status
Not open for further replies.

mstrcmtr

Programmer
Nov 14, 2007
103
PK
Is there any function or way to store Path and file name without drive name

like functions

JUSTdrive(gcImageFlNm)
JUSText(gcImageFlNm)
JUSTfname(gcImageFlNm)
JUSTpath(gcImageFlNm)
 
Looking into FoxPro help reference section Functions listing you can see no further JUST...function.

You could use something as simple as SUBSTR(cImageFlNm,2) and cut off the drive letter, but that assumes you don't use a UNC path starting with \\server\share.
It's easy to extend and check this case, just inspect the first character.

Let's measure this in terms of what JUSTDRIVE returns for a UNC path. JUSTDRIVE("\\server\share\folder\filea.ext") returns empty, not the \\server\share portion of the path, and so you could also just take the fullpath in case the first character is "\" instead of a (drive) letter.

I don't find a good JUSTx name for JUST the filename without drive, so I'd rather opt for FORCEDRIVE() to put in a specific drive letter:

Code:
? ForceDrive(GetFile(),"X")

Function ForceDrive(tcFile, tcDriveletter)
   Return IIf(Left(tcFile,1)="\",tcFile, tcDriveletter+SubStr(tcFile,2))

Simply pass in the empty string as drive letter and you have it removed.

Bye, Olaf.
 
Note: All the native JUST...() functions don't make a check, whether the path/filename you pass in is valid. Try out JUSTSTEM("Hello, World. Good Bye!") and JUSTEXT("Hello, World. Good Bye!")

So I wouldn't care for that, too. Those functions are simple string functions relying on you to pass in valid filenames to make sense. And so FORCEDRIVE() will always just replace the first character, unless it's a "\".

Bye, Olaf.
 
I was thinking that something like [tt]FORCEPATH(JUSTFNAME(x), JUSTPATH(x))[/tt] would do this (where x is the fully-qualified filename). But it seems that JUSTPATH() doesn't drop the drive letter, so it won't work.

On balance, it looks like Olaf's solution is the one to go for. In short, if the string starts with a letter and a "\", drop the first two characters, otherwise leave them alone.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike said:
In short, if the string starts with a letter and a "\", drop the first two characters, otherwise leave them alone.
You surely mean a latter and ':\'. You can of course also remove that part, too and later add it back by passing in 'X:\' instead of only 'X'. It's merely a change of Substr starting at position 4 instead of 2.

Bye, Olaf.
 
Yes,I would have thought it fails on the case JUSTDRIVE(lcPath) is empty when you have a UNC path, but that leaves lcPath untouched. My fault to not even check that.

Nevertheless, SUBSTR(lcPath,2) or SUBSTR(lcPath,4) does a good job. STRRTRAN also generates a new string, but it checks the whole string, while just the left character already tells you what to do. It might turn out fasater neverthelsee by being only one function call, but speed is not an issue for path strings with expected max length of 260 characters.

I dislike string replacements done in the whole string when it's only necessary to work on the begin, even when you limit replacment to the first occurrance and even though a colon isn't allowed in the rest of a path, yet that repells me from suggesting STRTRAN for this.

Bye, Olaf.







 
it checks the whole string, while just the left character already tells you what to do.

If you were really worried about speed, you could use the nNumberOfOccurrences parameter to force it to stop once it had found the drive in the first character, although that wouldn't make a difference if the drive was not present.

It's highly academic, of course, unless you were likely to do the same operation many times in a loop, which is hard to imagine.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Try this also:
lc_saveas= "f:\folder1\folder2"

? SUBSTR(lc_saveas,RAT(":\",lc_saveas)+1) ** "\folder1\folder2"

? SUBSTR(lc_saveas,RAT(":\",lc_saveas)+2) ** "folder1\folder2"

Etc...

You should get the point.

Edgar
Integrated Bar Code Systems, Inc.

Edgar
Integrated Bar Code Systems, Inc.
 
Yes,

Code:
lc_saveas= "\\share\folder1\folder2"
? SUBSTR(lc_saveas,RAT(":\",lc_saveas)+1) && \\share\folder1\folder2
? SUBSTR(lc_saveas,RAT(":\",lc_saveas)+2) && \share\folder1\folder2

If doing it so, you can also argue detection of path type (UNC/drive) is not the job of this function. But when you want to do no harm, RAT returning 0 in case of no drive letter speaks only for the first variant, which then keeps the string untouched, but will not cut off the first backslash in drive paths.

It works, of course, no doubt.

Bye, Olaf.
 
Using regular expressions...

Code:
LOCAL Test AS String

m.Test = ""

DO WHILE m.Test != "*"
	m.Test = INPUTBOX("Enter a full path expression", "UNDRIVE()", m.Test, 0, "", "*")
	IF m.Test != "*"
		MESSAGEBOX("Full path: " + m.Test + CHR(13) + "UnDrived: " + UnDrive(m.Test))
	ENDIF
ENDDO

FUNCTION UnDrive (Path AS String) AS String

	LOCAL RegExp AS VBScript.RegExp

	m.RegExp = CREATEOBJECT("VBScript.RegExp")

	m.RegExp.Pattern = "^([A-Za-z]:\\?|\\\\(\?\\UNC\\)?[^\\]+\\[^\\]+\\?|\\)"
	m.RegExp.Global = .T.

	RETURN m.RegExp.Replace(m.Path, "")
ENDFUNC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top