Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Guide to Relative Paths

04 Aug 2015   #1
disclaimer

Windows 7 Home Premium 64bit.
 
 
Guide to Relative Paths

I've been toying whith a project in Win7 making use of relative paths (and relative paths shortcuts) to start apps and open files and folders on removable drives. A rather common problem it seems, since USB sticks came around.
Then I realized how vague my ideas were about navigation in the directory structure, a must for forming relative paths. I started using computers in good 'ol DOS times, but all that primordial knowledge has just faded into a thick fog.
Thus I searched the web for some basic tutorial, a primer for absolute beginners. I might be real bad at searching but, rather surprisingly, I found nothing of the kind. Just scattered references (most about Unix or HTLM) and no organic Windows tutorial, not even for the use in the "Command Prompt" window.
The same happened when I searched for the creation of shortcuts using relative references.
Thence the idea to organize the collected fragments in personal memos for my future use and reference. Only later it occurred to me that some others might be interested in my findings. So I edited the lot and gave it the form of two small tutorials for navigational lubbers like me, the first of which, at the suggestion of the esteemed admin Brink, I'm posting hereunder.

Hopefully, quite a few knowledgeable eyes will scan this material for inaccuracy or/and incompleteness and offer corrections and additions, which are very warmly welcomed. Heck, I'm re-learning this stuff myself!

So, here we go.

==============================================================
A BEGINNER'S GUIDE TO RELATIVE PATHS IN WINDOWS
==============================================================

Relative paths have often the advantage of simplifing referencing data, particularly in combination with the Window system's environment variables.

When operating within removable external drives however, the possibility of defining relative paths to a file or a directory by their relative location to the current working directory (CWD) instead than relative to the root of a named drive, is a decisive asset.

All links and shortcuts located on esternal drives based on absolute paths and therefore relative to the root of a named drive, can become misleading and consequently useless when these drives are mounted on other computers and, with all probability, are assigned a different drive letter.

The downsides of relative paths are mainly two:

1. A relative path cannot span over different drives, thus making it impossible to reference data on other drives other than the local one.

2. When moving documents, applications and folders, all links and shortcuts referencing them must be accordingly moved as well, in order to maintain intact their relative route.

In order to succesfully use relative paths, it is necessary to have a clear idea of the conventions and switches making possible the navigation in the directory structure.
Below is a basic (for dummies) reminder of the elements of this navigation with regard to relative addressing.

==============================================================
1. RELATIVE REFERENCE IN THE ROOT DIRECTORY
==============================================================

Al directories and files on the system can be thought of as a tree growing upside-down from the root directory.
The "first" directory is called the root, and the parent directory of the root directory is the root itself. One can't go back in the file structure more than the that.

IN CMD.EXE
----------------
In a "Command Prompt" window, the root directory is represented by the notation [drive_letter:], followed by a BACKWARD slash (\), for example:

C:\

To change to the root of another drive mounted in the system, just enter it's letter followed by a colon, for example:

E:

IN WINDOWS
----------------
The root directory is represented by a FORWARD slash (/), the first one in the path string, as for example in the pathname:

C:/prices/fruit/apples.txt

As you can see, in path strings the FORWARD slash (/) is also used as a separator between directories.

If C: is the current drive, the above can also be expressed with the relative notation:

/prices/fruit/apples.txt

Which is interpreted as follows:

1. Start at the root (/) directory.
2. Move from / down to a directory named "Prices".
3. Move from "Prices" down to a directory named "Fruit".
4. The file "Apples.txt" is located in that directory.

Even if the pathname doesn't use a [drive_letter:] specification, the above example is actually a sort of absolute path in disguise, since the first slash (/) represents the root directory of the current drive, whichever that is. However, the fact that the drive is not referenced by name makes all the difference. It means that such a string, for example:

/travel/flight/info.txt

can also be used as a sort of "relative path", on external drives subject to changes of [drive_letter:] on other computers, where an absolute path making use of a specification like:

E:/travel/flight/info.txt

could become entirely useless.

==============================================================
2. RELATIVE REFERENCE IN THE CURRENT WORKING DIRECTORY (CWD)
==============================================================

IN CMD.EXE
----------------
Files and folders located in the current working directory can be referenced directly by their name, for example when renaming or deleting them in a "Command Prompt" window:

del apples.txt

The commands "cd/" or "cd\", take you from whichever is the current working directory (CWD) to the root of the filesystem on the current drive.

IN WINDOWS
----------------
A file in the current working directory is referenced using it's name, optionally preceded (mainly for readability in complex paths), by a dot and a slash (./). For example:

./apples.txt

refers to a file named "Apples.txt" in the current working directory (of the current drive since there is no [drive:] specification).

==============================================================
3. RELATIVE REFERENCE TO A FILE OR FOLDER IN A SUBDIRECTORY OF THE CURRENT WORKING DIRECTORY
==============================================================

IN CMD.EXE
----------------
From a "Command Prompt" window, you can move from the current working directory to one of it's subdirectories simply by using the command "cd" and the name of the subdirectory:

cd dirname

and can point to a file or folder in the subdirectory by it's name preceded by a BACKWARD slash (\), in it's turn preceded by the name of the subdirectory, as in:

fruit\apples.txt

IN WINDOWS
----------------
A file or folder in a subdirectory of the current working directory is referenced by the name of the subdirectory followed by a FORWARD slash (/), followed in it's turn by the file or folder name.

fruit/apples.txt

Optionally (mainly for readability in complex paths), a dot and a FORWARD slash (./) may be placed before the string to represent, redundantly, the current working directory (of the current drive, since there is no [drive_letter:] specification):

./fruit/apples.txt

Both the paths above refer to a file named "Apples.txt" in the subdirectory "Fruit" located in the current working directory. Accordingly, the path:

fruit\apples\pics\sample.jpg

refers to a file called "Sample.jpg" located in directory "Pics", located in directory "Apples", located in directory "Fruit" which is, in it's turn, located in the current working directory (of the current drive, since there is no [drive_letter:] specification).

==============================================================
4. RELATIVE REFERENCE TO A FILE OR FOLDER IN THE PARENT DIRECTORY
OF THE CURRENT DIRECTORY (CWD)
==============================================================

IN CMD.EXE
----------------
From a "Command Prompt" window, you can move from the current working directory to it's parent (one level up) directory by using the command "cd" followed by a double dot and either a BACKWARD slash or a FORWARD slash:

cd ..\ (or cd ../)

In the same way, a file or folder in the parent directory of the current working directory, is referenced by it's name preceded by a double dot and a BACKWARD slash (..\). For example:

..\apples.txt

or even multiple levels above:

..\..\..\apples.txt

IN WINDOWS
----------------
The pathnames use FORWARD slashes:

../fruit.txt

refers to a file named "Fruit.txt" in the parent directory of the current working directory (of the current drive since, unlike the first example, there is no [drive_letter:] specification).

And the pathname:

../../pears

refers to a directory named "Pears" in the directory two levels above the current working directory (of the current drive since there is no [drive_letter:] specification).

The following string:

D:../fruit.txt

while using the relative notation (../) is actually a absolute path, having a [drive_letter:] specification, and refers to a file called "Fruit.txt" located in the parent directory of the current working directory, which in it's turn is located in the root of drive C:

==============================================================
5. RELATIVE REFERENCE TO A FILE OR FOLDER ON ANOTHER DRIVE
==============================================================

IN CMD.EXE
----------------
From a "Command Prompt" window, this is achieved by using an absolute path combined with the change directory "cd" command.

While if you are, for example, on the C: drive, entering:

E:

will only take you to the root of the E: drive, while entering the command "cd" with the "/d" switch and a absolute path to a file or directory, as follows:

cd /d E:\pears

first switches to the E: drive, and then makes "Pears" the current working directory.

IN WINDOWS
----------------

While the use of absolute paths according to the rules above is customary and unproblematic, the referral to files on other drives using relative notations is impossible, as a relative path cannot span over different drives.

==============================================================
6. GENERAL PROCEDURE FOR FORMING RELATIVE PATHNAMES
==============================================================

A relative pathname is given as a list of all directories on the route from the current working directory to the target file or directory, separated by FORWARD "/" slashes.

1. If the target is below the current directory, it is necessary to list all subdirectories on the route from the current directory to the target, separated by forward slashes "/.

2. If the target is above the current directory in the directory tree and in a straight path, that is to say always ascending, it is necessary to move up the file structure with one or more "../" consecutive notations until the parent directory containing the target is reached.

3. If the target is on another branch of the directory tree than the current directory, that is, it's path contains both ascending and descending steps, it is necessary to move up the file structure with one or more "../" consecutive notations until a directory located above the target is reached.
From there, as in point 1. above, all the directories in a straight descending path to the target must be listed, separated by forward slashes "/".

4. The name of the target file or folder must be appended to the resulting path.

==============================================================
7. NOTE ABOUT SLASHES
==============================================================

In Windows, both slash types, BACKWARD (\) and FORWARD (/) can generally be used interchangeably. Windows CMD Shell (cmd.exe) makes an exception.

The "Command Prompt" window likes BACKWARD slashes instead, as usually console programs use FORWARD slashes to define command switches.

A path containing forward slashes often needs to be surrounded by double quotes to disambiguate it from command line switches. For example:

E:\>dir "/Folder/SubFolder/"

To make things even more interesting, the "Command Prompt" command "cd" (change directory) does not follow this guideline all the way. As we have seen in sections 2. and 4. above:

"cd ..\" works as well as "cd ../", taking you to the parent directory of the current directory.

"cd\" works as well as "cd/", taking you from whichever is the current working directory (CWD) to the root of the filesystem on the current drive.

More:

"cd C:\apples" works as well as "cd C:/apples"

Also, from a ROOT folder, the command:

"cd C:.\Programs.\Internet"

would be treated the same as

"cd C:/Programs/Internet"

==============================================================

That's it for now, folks.
The next little tutorial on the use of relative paths in shortcuts will be soon forthcoming.
My System SpecsSystem Spec
04 Aug 2015   #2
JustCall

Windows 8.1 Pro 64bit
 
 

Well, explained, good job!
My System SpecsSystem Spec
05 Aug 2015   #3
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Good guide.

I’m interested in how you’ll pull off this relative-pathed shortcut thing, Disclaimer. There’ve been a couple of discussions on this forum about using shortcuts on usbs already, and there are quite a few answers to this problem but all of which have their limitations or major flaw. You should read up on as many of them as possible, Disclaimer, before continuing.
My System SpecsSystem Spec
.

05 Aug 2015   #4
disclaimer

Windows 7 Home Premium 64bit.
 
 

Quote   Quote: Originally Posted by Pyprohly View Post
Good guide.

Iím interested in how youíll pull off this relative-pathed shortcut thing, Disclaimer. Thereíve been a couple of discussions on this forum about using shortcuts on usbs already, and there are quite a few answers to this problem but all of which have their limitations or major flaw. You should read up on as many of them as possible, Disclaimer, before continuing.
Thanks for the positive comments, JustCall and Pyprohly.

Thing is, I'm not really trying to pull off something. Only, as I said, painstakingly organizing the bits and pieces of information I collected on a variety of sources, of which sevenforums was the first one I scanned, of course.
That doesn't necessarily mean I managed to find everything there is, or that what I found hasn't got limitations or practical flaws of some kind.
I have done, and with my limited means I'm still doing, a bit of testing with relative shortcuts as per the solutions proposed on the net. What I can tell right now is that some of them work for me only on some machines. And for one thing, I've just got 4 systems to test them on: Win2K, Vista; XP and 7, nothing newer than that.
On the other hand, even solutions and workarounds published online by very authoritative sources seem to work for some and not for others. The variety of environements is huge.

My little guide on the works is clearly intended neither as a last word on the matter, nor the solutions listed to be universally valid, but only a organic presentation of what methods might be available according to the general user's base out there, as things stand. I believe this might have some little value.
It's up to any interested party to try them out on their particular boxes.
Therefore, as always, feedback and contributions are enourmously helpful in getting together tutorials worthy of this site.
My System SpecsSystem Spec
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar help and support threads
Thread Forum
Random BSODs on Relative's laptop
A family member's laptop keeps BSOD-ing on them at random. The DMP before the most recent crash I was able to look at myself with the Debugging Tools for Windows. But the only thing I was able to read was: 'Probably caused by: ntkrnlmp.exe' That was before I got DM Log to collect everything else...
BSOD Help and Support
Relative path shortcuts help
Hello. I have a large number of folders and shortcuts on an external drive. Example: Folder 1 - Folder A - Folder B Folder 2 - Folder A - Folder B
General Discussion
Why is my OS installation so large relative to sum of files?
I performed a fresh install of Windows 7 on a SSD. Added a bunch of apps, and moved some of the user folders onto a data drive. Used space on the SSD shows as 65GB Sum of files and folders (with operating system files unhidden): 51.5GB 1. What accounts for this difference? It's not the...
Installation & Setup
Display relative dates in Windows 7 folders
Hello, Is there any way to display the dates of files/folders as "today" "yesterday" etc. instead of the full date? Windows only ever displays, for example, "23/10/2014 5:12 pm" regardless of how much I expand the date column. On a Mac, the date changes from the short format to a longer format...
General Discussion
Shortcut problem (relative path)
Hi everyone, Someone know how I could change my shortcut path, regardless of the machine ? My picture is in C:\Users\Manon\Desktop\perso\1.png. I made some research, I have to change the "Start in" in the properties of the shortcut. But what I have to write in ? Thanks for your advices,...
General Discussion
Dos paths and unix paths conversion
I guess this may be a tiny bit off the subject here, but I am attempting this in windows 7. I have a program file manager tool called 'Directory Opus' that is capable of calling windows external programs and passing them the current directory the file manager is sitting on. So far so good. ...
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 23:59.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App