Important alert: (current site time 10/23/2014 8:02:30 AM EDT)
 

VB icon

Detect "\" in directory

Email
Submitted on: 8/17/2000 10:37:46 PM
By: Eric LeBourgeois  
Level: Beginner
User Rating: By 9 Users
Compatibility: VB 5.0, VB 6.0
Views: 22927
(About the author)
 
     Suppose You try to get the app.path when the app is in the C Drive. the result is "C:\". if the app is in another directory the result may be "C:\Directory". notice there is no backslash. most people skip this because they don't think it is a problem, but it is. when you put in a program, app.path & "\filename.file", when it is in the C Drive or if you put app.path & "filename.file" in a named directory, the computer will either return "C:\\filename.file" or "C:\Directoryfilename.file". this code will show you how to detect the backslash and deal with it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Detect "\" in directory
' Description:Suppose You try to get the app.path when the app is in the C Drive. the result is "C:\". if the app is in another directory the result may be "C:\Directory". notice there is no backslash. most people skip this because they don't think it is a problem, but it is. when you put in a program, app.path & "\filename.file", when it is in the C Drive or if you put app.path & "filename.file" in a named directory, the computer will either return "C:\\filename.file" or "C:\Directoryfilename.file". this code will show you how to detect the backslash and deal with it.
' By: Eric LeBourgeois
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=10775&lngWId=1'for details.'**************************************

'this code will give you a valid filename, whether the app.path return has a backslash or not, and displays a message box.
'Please Vote for me at Planet Source Code
if right(app.path,1) = "\" then 'sees if the directory has a backslash at the end of it
msgbox app.path & "filename.file"
goto ResumeMe
end if
msgbox app.path & "\filename.file"
ResumeMe:


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/18/2000 5:28:03 AMM.I.X

Hmm nice way..
But i use..
len(app.path) > 3 then
app.path & "\filename.file"
else
app.path & "filename.file"
end if

Hmm But it works.
yours M.I.X


(If this comment was disrespectful, please report it.)

 
8/18/2000 5:29:44 AMM.I.X

Sorry Here is the complet code :-)
>>>Hmm nice way..
But i
use..
If len(app.path) > 3 then
app.path & "\filename.file"
Else
app.path & "filename.file"
End If

Hmm But it works.
yours M.I.X

(If this comment was disrespectful, please report it.)

 
8/18/2000 5:57:38 AMjparks

Eric, you really shouldn't vote for yourself.
(If this comment was disrespectful, please report it.)

 
8/18/2000 8:53:34 AMFastgraph boy

dim AppPath as string
if RIGHT(App.Path,1)="\" then
AppPath=App.Path
else
AppPath=App.Path & "\"
END IF
OPEN AppPath & Filename FOR INPUT AS #1
(If this comment was disrespectful, please report it.)

 
8/18/2000 2:56:54 PMEric LeBourgeois

sorry, i didn't vote for myself, one of my friends knows my password and he renames things and puts useless pictures as screenshots. i'll try not to let that happen again.
(If this comment was disrespectful, please report it.)

 
8/18/2000 8:44:01 PMBST

M.I.X. - Your code works. The problem comes in if you put this into a function and just pass the path to it. Use Eric's code and you can pass a path from App.Path, or any method that will generate a path. I've seen some methods that will return a path with a
(If this comment was disrespectful, please report it.)

 
8/18/2000 8:44:53 PMBST

M.I.X. - Your code works. The problem comes in if you put this into a function and just pass the path to it. Use Eric's code and you can pass a path from App.Path, or any method that will generate a path. I've seen some methods that will return a path with a "\" and some that don't. (Bring VBA and VBScript into the situation and you'll run into this problem)

Eric's is a more reliable way.
(If this comment was disrespectful, please report it.)

 
9/17/2000 7:43:54 AMTom Walker

A better way would be:
Dim fso As New FileSystemObject
fso = CreateObject("Scripting.FileSystemObject")
AppPath = fso.BuildPath(App.Path, filename)
MsgBox AppPath
(If this comment was disrespectful, please report it.)

 
9/21/2000 9:48:03 PMCoryUlui

I like both all your ways, all work.
I like M.I.X way somewhat more. But I used to use Eric's. ;)
(If this comment was disrespectful, please report it.)

 
9/30/2000 5:03:59 PMRML

This won the contest????????????
(If this comment was disrespectful, please report it.)

 
10/20/2000 1:11:41 AMThomas...

It can be done this way too...

DirectoryPath = Replace(App.path & "\FileName","\\","\")

: )
(If this comment was disrespectful, please report it.)

 
10/21/2000 2:52:49 PMEric LeBourgeois

when did i win?
(If this comment was disrespectful, please report it.)

 
1/6/2002 5:56:21 AMJim Limborg (Minnesota)

Hello, I've tried to paiste many of your code examples in my VISUAL BASIC 6 application and none of them work. All of the IF statements above turn RED in my Visual Basic code section. Visual Basic doesn't accept it for some reason. Shouldn't your codes be put in a PRIVATE SUB or something? Can an IF statement just be put in the code all by itself? I have a lot of statements like this:

Picture1 = LoadPicture(App.Path & "\videos\" & "BG_VIDEOS.jpg")

The path statement above is in my FORM LOAD area. I have many COMMAND BUTTONS that have path statements like:

Picture1 = LoadPicture(App.Path & "\" & "BG_BCC.jpg")

I need a PATH FIX so when I try running my program from a cdrom, it stops adding that EXTRA '\' in there. It keeps searching for g:\\ not g:\

EXACTLY what is the code I should use that can be put at the beginning of my source code so ALL of my 100 or so path statements will work?

THANKS
(If this comment was disrespectful, please report it.)

 
1/8/2002 1:49:58 PMJim Limborg

A programmer by the name of DAVE TETENS wrote this for me.

FIRST, put the code below at the top of your FORM_LOAD:

Pth = App.Path
If Right(Pth, 1) =
(If this comment was disrespectful, please report it.)

 
1/8/2002 1:51:00 PMJim Limborg

A programmer by the name of DAVE TETENS wrote this for me.

FIRST, put the code below at the top of your FORM_LOAD:

Pth = App.Path
If Right(Pth, 1) = "\" Then
Pth = Left(Pth, Len(Pth) - 1)
End If

Dave added this code below to the very top of my module code in my visual basic 6 program. I'm guessing you can also add this code to the very top of your MAIN FORM's code:
Option Explicit
Global Pth As String

Below is an example of one of my PATH statements:
Picture1 = LoadPicture(Pth & "\videos\" & "BG_VIDEOS.jpg")

Notice the Pth in my path statement above. It use to say App.Path, but I changed it to Pth instead. You HAVE to change all of your App.Path to Pth to make this code work, BUT don't accidentally change the App.Path in the first main code I listed above that goes into your FORM_LOAD area.

(If this comment was disrespectful, please report it.)

 
1/8/2002 1:54:13 PMJim Limborg

MY CODE ABOVE makes your program run even if it is stored in the root of a drive like C:\ or on a CDROM drive. My program ran fine from my computer, but when I copied it to a cdr disk and tried running my program from the cdrom drive, I got a PATH NOT FOUND error cause and EXTRA \ was being added to my path statements. MY CODE ABOVE fixes the problem. I’ve tried all of the codes above mine, and none of them worked. I think they don’t have ALL of the instructions to make them work perfect.
(If this comment was disrespectful, please report it.)

 
5/31/2002 11:10:04 AMWolfsinc

I usually use the following:

pth = iif(right(app.path,1) <> "/",app.path & "\" & "FileName.TXT",app.path & "FileName.TXT")

I use this because it is one line of code, it is clean and reliable. Of course I also come from a C programming world, so I got used to it there.
(If this comment was disrespectful, please report it.)

 
1/2/2003 5:23:56 AMMax Powers

If Right(App.Path,1) <> "\" Then Tmp = "\"
MsgBox App.Path & Tmp
(If this comment was disrespectful, please report it.)

 
7/15/2004 7:15:54 AMPejo

I use this:
Dim separator as String

If ((Right$(App.Path, 1) <> "\")) Then separator$ = "\" Else separator$ = ""

MSGBOX App.Path & separator$
(If this comment was disrespectful, please report it.)

 
7/15/2004 10:12:13 AMEl Mariachi

Stay away from the scripting object. That assumes that people have it installed and active. any people like do not have it installed for security reasons. Also, how in the world did this code snippet win the contest? Ive seen this code on PSC many times.

(If this comment was disrespectful, please report it.)

 
7/15/2004 10:28:20 AMMike Douglas

Just goes to show you, submit single lines of code instead of wasting your weeks on real applications.

(If this comment was disrespectful, please report it.)

 
7/15/2004 11:44:45 AMBrett Malone

This is a useful snippet.. I have a function which returns the path corrected using a very similar technique.

Although useful, I don't know how it could win the contest. Congratulations though.
(If this comment was disrespectful, please report it.)

 
7/16/2004 12:22:13 PMNoor Khazal

excause me butm i dont get th point, please explain it to me :$
(If this comment was disrespectful, please report it.)

 
7/16/2004 1:31:16 PMBart Goossens

to Thomas
DirectoryPath =
Replace(App.path &
"\FileName","\\","\")
is not a good idea, what if you work with network files ???
\\ServerName\Share ...
(If this comment was disrespectful, please report it.)

 
7/16/2004 6:20:13 PMKris Bennett - (i00 Productions)

how about this then - handles everything i think

realapp.path = iif(leftapp.path,1 )="\","\\i00-server\c$\", replace(app.path,"\\","\"))
(If this comment was disrespectful, please report it.)

 
7/16/2004 6:21:04 PMKris Bennett - (i00 Productions)

whops meant

realapppath =
iif(leftapp.path,1
)="\","\\i00-server\c$\",
replace(app.path,"\\","\"))

- just so used 2 typing app.path :$
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.