?
Solved

Why do I get a \ after GETDIRECTORYFROMPATH(#CGI.CF_TEMPLATE_PATH#)

Posted on 2003-03-27
15
Medium Priority
?
701 Views
Last Modified: 2013-12-24
I am using GETDIRECTORYFROMPATH(#CGI.CF_TEMPLATE_PATH#)and it returns the proper directory except that it has a \ at the end. I.E. c:/www/test/\. Anybody know why this happens?
0
Comment
Question by:dmatthews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 17

Expert Comment

by:anandkp
ID: 8222664
weird,

can i have a look at ur code - i guess shld be some typo .. i dont know - will need to take a look

let me know

K'Rgds
Anand
0
 
LVL 17

Expert Comment

by:anandkp
ID: 8222697
this works fine for me

<CFOUTPUT>
#GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH)#
</CFOUTPUT>

giving me :
D:\My Doc\Reference\My-ColdFusion\MyUitlity\TEST\

K'Rgds
Anand
0
 
LVL 1

Expert Comment

by:jladams97
ID: 8223233
What version of CF are you using?  I'm using CFMX and all the comments below are relevant to results achieved with CFMX and may differ from results achieved with other versions of CF.

1.  If you put what you have written directly inside a <cfoutput></cfoutput> tag pair so that you have <cfoutput>GETDIRECTORYFROMPATH(#CGI.CF_TEMPLATE_PATH#)</cfoutput>, you'll get something like:

GETDIRECTORYFROMPATH(C:\Inetpub\wwwroot\test\ztestexe001.cfm)

That leads me to wonder if you're instead assigning that code to a variable.  Are you, if so, please provide the complete line of code.

2.  If you move your #s to the outside and put them in a <cfoutput></cfoutput> tag pair so that you have <cfoutput>#GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH)#</cfoutput>, you should get something like:

C:\Inetpub\wwwroot\test\

3.  Note that the correct output includes backslashes and no slashes.  You said your output includes slashes.  That's a red flag right there.  I can't say how it is related to the issue of you getting one lone backslash at the end, but I bet there is some relation.

4.  Note that the correct output has a capital C.  If you're on a machine with a drive that is actually named C and not c, then that's what the output should be.

5.  How about using CGI.PATH_TRANSLATED instead of CF_TEMPLATE_PATH?  Try <cfoutput>#GETDIRECTORYFROMPATH(CGI.PATH_TRANSLATED)#</cfoutput> and you should get this:

c:\inetpub\wwwroot\test\

5.  Note that the correct output has a lowercase c. I'm not sure why it is different from the output of <cfoutput>#GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH)#</cfoutput>.

Josh

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 6

Expert Comment

by:dash420
ID: 8223374
it quite strange.
I tried with one test.cfm it is giving correct path

F:\inetpub\cfusion\cftest\dash\
0
 

Author Comment

by:dmatthews
ID: 8224590
I had <CFSET Path=GETDIRECTORYFROMPATH(#CGI.CF_TEMPLATE_PATH#)>. I think the extra \ was due to the fact that it is running on an apache web server. I read that in some article somewhere. The purpose of this code was to do some login verification. So I do <CFIF (CGI.CF_TEMPLATE_PATH IS NOT "#PATH1#login.cfm") AND (CGI.CF_TEMPLATE_PATH IS NOT "#PATH1#login2.cfm")> that to make sure that the user is coming from the login pages before I set a session variable. This code was in an application.cfm page.

I ended up doing a removechars function on path and using that variable called path1. I am am still curious as to why that happens however.

An article here http://www.systemanage.com/cff/cftips7.cfm says that it puts the extra \ due to the fact that it is a unix system.
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8224780
That's what I expected when I read the question, "IT's UNIX"

at unix world it's "/" to seprate directories, not "\" as windows.

0
 

Author Comment

by:dmatthews
ID: 8224793
Funny that it only made the last slash a back slash and not all the other ones however.
0
 
LVL 2

Accepted Solution

by:
weeezl earned 500 total points
ID: 8226310
ColdFusion seems to like to reinterpret the direction of slashes. You can put them in either way or even mixed for some functions and CF will process it anyway.

Here's an easy catch:
#replace(GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH),'\','')#
0
 

Author Comment

by:dmatthews
ID: 8226457
I did the same thing using the removechars function but your answer will work so you get the points.
0
 
LVL 1

Expert Comment

by:jladams97
ID: 8226873
Sure that solution will work on a UNIX box--but guess what it does on a Windows box?  If you're sure you'll *never* port or reuse your code on a Windows box then you're in good shape; otherwise, you should *not* use the code weeezl gave you.  This code, on the other hand, should never cause any harm:

#replace(GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH),'/\','')#

Josh
0
 
LVL 1

Expert Comment

by:jladams97
ID: 8226880
My mistake!  That should be:

#replace(GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH),'/\','/')#

Josh
0
 

Author Comment

by:dmatthews
ID: 8227002
That would give me an extra / wouldn't it. It would turn c:/www/test/\ into c:/www/test// wouldn't it? Needless to say I will be sure to be careful if I use it on a windows system. I won't have this problem on a windows system anyway because it will not add the \ on a windows system.
0
 
LVL 1

Expert Comment

by:jladams97
ID: 8227134
No, that won't give you an extra slash.  All it will do is replace the first instace of the string "/\" with the string "/" (and note that if you wanted to check for all instances of the string "/\" you'd use #replace(GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH),'/\','/', 'all')#).  So effectively what it will end up doing is removing the trailing backslash.

You're right--you won't have this problem on a Windows system because it won't add the trailing backslash.  But if you port weeezl's code (#replace(GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH),'\','')#) over to a Window's system, it will produce an undesired result:  instead of removing a trailing backslash, it will remove the first backslash, specifically the slash after the drive letter and colon.  The reason for this is that weeezl's code searches for the first backslash and replaces it with the empty string.  Simply put:  on a Windows system, weeezl's code will break stuff.  The alteration I suggested won't break anything because instead of searching for a backslash alone, it searches for a slash followed by a backslash.  Since that string will not exist in GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH) on a Windows machine, the code I gave will not make any changes to the result of GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH) on a Windows system, thereby not breaking anything.

Josh
0
 
LVL 17

Expert Comment

by:anandkp
ID: 8229400
I hope this is not a timebeing makeover solution as it may behave differently in different situations .. causing undesired results ...

Identify the root cause & then go ahead with it ...

K'Rgds
Anand
0
 
LVL 1

Expert Comment

by:jladams97
ID: 8230702
dmatthews--please read and respond!

Anand--

I'm not sure what it is that you're saying may behave differently in different situations.  If you're talking about the value in GETDIRECTORYFROMPATH(CGI.CF_TEMPLATE_PATH), we know that is different on Windows and UNIX.  In terms of the best way to approach the issue, I agree that identifying the root cause is the best way to go.  And as I'm thinking about it, I think I might have an idea about what that root cause is.  But let me hold that for a second and get back to what you were saying.  But though identifying the root cause may be the best way to go, if for whatever reason it can't be done, another solution has to be reached.  Now, that solution has to be as robust as possible--as you point out, a "timebeing makeover solution" is not a good one.  And this is where I come back to the question of what it is you question as such a solution.  The solution weeezl gave is, in my opinion, such a solution as it will cause different results on UNIX and Windows.  The solution I gave is not, in my opinion, such a solution as it will not cause different results on UNIX and Windows (let me note that the actual result will not be the exact same on UNIX and Windows as the UNIX result will contain slashes and the Windows result will contain backslashes but I believe that this is the desired result because that is how paths on those OSs are represented--someone correct me if I am wrong).  My solution is only capable of causing problems if the string "/\" is a legitimate part of a disk path on an OS.  If that string is never legitimate on any OS (and I think it isn't) then my solution is okay.

Okay, dmatthews--I hope you're still with me here.  I'd like you to test the following code and give me your results, please.  I do not have UNIX box on which to test or I would test this myself.  Thanks!  Here's the code:

<cfoutput>#CGI.CF_TEMPLATE_PATH#</cfoutput><br>
<cfoutput>#GetDirectoryFromPath(CGI.CF_TEMPLATE_PATH)#</cfoutput><br>
<cfoutput>#Replace(GetDirectoryFromPath(CGI.CF_TEMPLATE_PATH), '/', '\', 'all')#</cfoutput><br>
<cfoutput>#GetDirectoryFromPath(Replace(CGI.CF_TEMPLATE_PATH, '/', '\', 'all'))#</cfoutput><br>

Please copy and paste the results into a post in this thread.  Please also tell me what version of CF you're running.

Do the results you get look something like this:

C:/Inetpub/wwwroot/test/ztestexe001.cfm
C:/Inetpub/wwwroot/test/\
C:\Inetpub\wwwroot\test\\
C:\Inetpub\wwwroot\test\

If your results look like that then here's what I think is going on:  ColdFusion bug, baby!  The last two lines of the output *should* look exactly alike; if they don't, ColdFusion must have a bug.  The reason they should look exactly alike is that they're doing the same operations on the variable--they're just doing the operations in a different order.  But these particular operations should be transitive and the order shouldn't matter.  If the order does matter, that to me is a bug with CF.

If the results you get look like what I wrote above, here's my explanation:  when CF does GetDirectoryFromPath on a variable, it removes the trailing backslash along the way but it replaces it before it finishes.  In Windows, this works fine.  But in UNIX, where there are no backslashes, it does not work.  Instead, for some reason the trailing slash is not removed and the appended backslash is just wrong!  That could be a little off; the truth could instead be even more simple--when GetDirectoryFromPath has completed its operations it does one last thing:  it looks for a trailing backslash and if it isn't there it appends it.  But regardless, if either of those theories is correct the output shown above is what you should get on UNIX.
0

Featured Post

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question