Absolute path with disk name (C:\\) to relative path (~/) - asp.net

Any built in way to get virtual / relative path or URL from absolute path containing disk name such as?
Or how to convert this :
C:\\ProjectRoot\\Somedir\\demo.text
to this
~/Somedir/demo.text
Thanks.

Request.PhysicalApplicationPath will give you the physical root of your virtual directory.
So in your case if your virtual directory was at c:\projectroot, then you'd want ~/somedir/demo.txt right?
So if you did a string replace on the file path, replacing Request.PhysicalApplicationPath into a tilde character, along with swapping \ for / , then you'd get what you needed.

Related

UNIX paths -- what do ., ~, .. prefixes mean?

/home/ise is my shell-base folder.
What are the differnces between those lines ?
Thanks.
/home/ise
./home/ise
~/home/ise
../home/ise
/home/ise # absolute path (often "/" is the root of your hard drive)
./home/ise # relative to current directory
~/home/ise # relative to the current users home directory
../home/ise # relative to the current directory's parent folder
/home/ise - specifies path from disk
./home/ise - same as above, the . indicates current directory
~/home/ise - ~ indicates from home directory
../home/ise - .. specifies parent directory

What does .. at the start of filepath do?

What does ..\ at the start of a file path do?
Example: The following file is referenced in the directory
\work\QA\Reports\TimeOff.rpt
What is the difference between referencing the file path as \work\QA\Reports\TimeOff.rpt and referencing the file path as ..\work\QA\Reports\TimeOff.rpt?
it's the difference between relative and absolute path referencing.
\work\QA\Reports\TimeOff.rpt
starts with a (back)slash "\" (or "/" in unix if it matters), so it's indicating "Start at the root, or top most level directory".
Then navigate down.
It's an Absolute path reference. It doesn't matter where you are, you will always end up pointing to the same file/location.
.\work\QA\Reports\TimeOff.rpt
however, begins with "." Which is Relative path reference. It says to "start where you currently are". so if you were already in folder: \my\home\directory
then you'll end up navigating to:
\my\home\directory\work\QA\Reports\TimeOff.rpt
".." is a reference to go one level up ... but again "relative path".
so:
..\work\QA\Reports\TimeOff.rpt
if you were in \my\home\directory
you'd end up here:
\my\home\work\QA\Reports\TimeOff.rpt
Since it backs up one level ("\directory\") ..and goes from there.
Hope that makes sense ;)
.. refers to the parent directory, the directory one level up from the current directory. Additionally, . refers to the current directory.
Say you're in directory \a\b\c\. You want to access file \a\b\c\d. You can access that file with just d or .\d or \a\b\c\d. Say you now want file \a\b\x. You can access that as ..\x or the full absolute path. You can, of course, chain . and .., like ..\.\..\y.
Paths starting with \ are absolute (or <drive letter>:\); they refer to the same file every time and don't depend on the current directory. Other paths are relative, the file they refer to changes with the current directory.
It means go to parent folder first and then look for the path specified.
so it is basically the same when you do cd .. in command line.
The difference between \work\QA\Reports\TimeOff.rpt and ..\work\QA\Reports\TimeOff.rpt is thet if you're in \a\b folder, first one will match \a\b\work\QA\Reports\TimeOff.rpt and second one a\work\QA\Reports\TimeOff.rpt.

How to find a file path in current java directory

I have a function that gets a file path as an input. input file is located in a folder in my project (etc/xsd/template.xsd). how can i set this path?
this ia my function:
JAXBUtilityTool tool = new JAXBUtilityTool("etc/xsd/template.xsd","src.com.classes");
and it can not find the file "etc/xsd/template.xsd"
System.getProperty("user.dir") does not help since when I add the rest of path to it:
System.getProperty("user.dir")+ "etc/xsd/template.xsd"
result is c:\eclipse\myworkingdirectory\project/etc/xsd/template.xsd
Try:
String path = System.getProperty("user.dir");
path = path.replaceAll("\\","/") + "/etc/xsd/template.xsd"
Source:
String.replaceAll single backslashes with double backslashes

Server.MapPath does not like ~/ and ./

I am using the following code to try and find a file contained in another directory from my code file.
Set fi=fs.OpenTextFile(Server.MapPath("~/counter/counter.txt"), 1)
I have also tried.
Set fi=fs.OpenTextFile(Server.MapPath("./root/folder1/counter/counter.txt"), 1)
In either case this should get me back to the counter.txt file. From what I understand ~/ moves up 1 directory and ./ moves up to the root directory.
Both times however I receive an error saying an invalid character has been used. When removing these I get a different error saying the path cannot be found (Which I would expect because it is not a valid path without moving up 1 directory).
What are the valid characters to do the following in VBscript:
move up a single directory?
move up to the root directory?
Thanks for the help
A few things:
The tilde character "~" is not valid here.
The single period character "." is for specifying the current directory/folder.
A set of period characters ".." is for specifying the parent directory/folder. For example, to refer to a file found in the parent of the current directory, you might use:
Server.MapPath("../counter.txt")
You can chain these to walk up more than a single parent path. To refer to a file found three directories above the current, you might use:
Server.MapPath("../../../counter.txt")
The documentation on MSDN for the MapPath function outlines this. Pay attention to the caution listed here about enabling parent paths if you want to be able to refer to relative paths above the current directory. If you get an error when trying to refer to a parent path, then you do not have parent paths enabled.

about the file path

There are relative path and absolute path of the a file. But some of the writings confuse me sometimes:
/a/b/c.php //relative document root
./a/b/c.php //what does this mean? equals to '/a/b/c.php' or a/b/c.php?
a/b/c.php //relative to current directory
../a/b/c.php //parent folder relative to current directory
/../a/b/c.php //what does this mean? parent folder of document root?
Are there other ways of writing this?
Thank you.
Here's some basic directory symbol for you:
. (dot) is your current directory
.. (double-dot) is the parent of your current directory
~ (tilde) is your home directory.
/ (slash) if it present at first character, it usually is called root directory.
These all came from linux / unix terminology (CMIIW here).
Now, let's take a look at the implementation:
Let's say, you are on /home/username/
if you write something like this, the result is:
./wwwroot/somedir/ => /home/username/wwwroot/somedir/
../wwwroot/somedir/ => /home/wwwroot/somedir/
/../wwwroot/somedir/ => /wwwroot/somedir
You might get confused on example #3. If you put / in front of path info, it mean you are at the root directory. Therefore, if you write /../somedir/ it mean, you are pointing to /somedir/. Why? because root directory doesn't have parent.
. = current directory. So ./a/b/c.php would be equivalent to a/b/c.php.
/../a/b/c.php means go to the root directory, then up one, then directory a, then directory b, then c.php.

Resources