Skip to content

Using absolute paths

Harry Kakoulidis edited this page Feb 16, 2020 · 1 revision

An important rule for scripts to run both offline and remotely, is to use relative paths to the files they use, or use the subst command demonstrated below.

Assume your remote directory structure looks like this:

/htdocs/index.html

/cgi-bin/form.cgi

/data/data.txt

You have your site offline in windows looking like this:

c:\webs\mysite\htdocs\index.html

c:\webs\mysite\cgi-bin\form.cgi

c:\webs\mysite\data\data.txt

On your remote server you would access the file data.txt from the script form.cgi, using something like:

open(FILE, "/data/data.txt")

This however would not work offline! the folder c:\data does not exist.

You have 3 solutions to this problem:

1) Use relative paths to the file:

open(FILE, "../data/data.txt")

2) Use version converter remarks:

#@Local#?#@Server

#? open(FILE, "/data/data.txt")

open(FILE, "c:/webs/mysite/data/data.txt") #?

After the upload this would become:

#@Server#?#@Local

open(FILE, "/data/data.txt") #?

#? open(FILE, "c:/webs/mysite/data/data.txt")

3) Use the subst command

This is a technique that will allow absolute paths without any changes. It is very easy to do and you can also create a user tool in OptiPerl to do this automatically.

i) Open a MS-DOS prompt or Console window

ii) Enter:

subst z: c:\webs\mysite

(don't add the ending backslash)

where z: a drive letter that is not being used. You can also leave it as z, or if you manage many web sites, assign them to large letters like x:, y:, z: etc.

iii) You are done! Now open the file form.cgi from optiperl, but select drive z: to open from. Notice that the file is at z:\cgi-bin\form.cgi

If you run form.cgi, even absolute paths will work! Also don't forget that this is not a second copy of c:\webs\mysite\cgi-bin\form.cgi. If you save the file in the z: drive it will also change in the original folder.

To delete the z: drive, restart windows, or enter the command:

subst z: /d

The files will not be deleted of course; they are still in the original path.

icon_exclaim You will also need to change the internal or external webroot from

c:\webs\mysite\htdocs (and accordingly any of it's aliases) to z:\htdocs. Remember that the file must be run from the z: drive.

icon_exclaim If you are using sendmail and date support, don't forget to setup again, by selecting "drive to copy files" as the new drive z:. Note that this will create folders under c:\webs\mysite, but this should not be a problem.

Common setups of web hosts

Unless you have a dedicated server, your website will probably be hosted on a machine with many other accounts. This means that depending on your web host, an absolute path to your website might look something like:

/usr/local/customer/virtualfp/www.mysite.com/

/usr/local/customer/virtualfp/www.mysite.com/cgi-bin

/www.mysite.com/htdocs

/www.mysite.com/cgi-bin

/users/students/disk1/george/public_html/

You will need to check with the CGI directory help section of your web host to learn where your directory is. In any case, if you want to use absolute paths, you will need to recreate the structure in your hard disk. For example:

x:\usr\local\customer\virtualfp\www.mysite.com

y:\www.mysite.com\htdocs

z:\users\students\disk1\george\public_html

icon_exclaim Don't get mixed up however if your remote root path is simply "/", this does not mean that your files are also at root level. Sometimes web hosts might set-up an alias when you log in your FTP or SFTP account, so that '/' is really one of the above for your account only. But still when programming, you will need to use the real path to your files.

Linking to a project

You might want each project to be associated with it's own drive letter. Have every project set it's drive in one of the Project options / data0..9 fields. For example enter in Data5 z:, and enter in Data6 the real starting path of the project (like for the above example c:\webs\mysite). Add a custom tool:

Name: Subst project

Program: Subst

Parameters: %data5% %data6%

You can also create a tool to unlink after use:

Name: UnSubst project

Program: Subst

Parameters: %data5% /d

You can also replace all the files in the project with the files under z:, plus settings for the internal and external server. Note however that when first loading the project, if the subst command has not been run yet, you will get warnings about the files not existing in drive z. Ignore the warning, and execute the subst command.

Automating when managing many webs

A better procedure would be a create a .bat file that makes all the substitutions, and run once per window session (you can even put this file in the Start menu / Startup folder so it gets executed automatically whenever windows starts).

Create Webs.bat with the following:

subst x: c:\webs\ruth

subst y: c:\webs\george

subst z: c:\webs\harry

Working on the same project on different machines

If you are working on a project on different computers, chances are that the drive lettering may change, thus invalidating project set-ups. However by using the subst command, you can set your projects to point to the virtual drive on both computers, and then change how you call subst between computers.

Other tips

icon_exclaim Using this technique might be necessary if you are trying to run both offline and remotely, complicated perl projects that share many scripts and data folders for storage, like on-line discussion boards, search engines etc.

icon_idea A fourth method of working with absolute paths, is very similar like the above, but your will need a second hard disk. Repartition it with 15-20 partitions of 50-100 mb each assigning to each a drive letter. A major drawback however it that a web site might "grow" larger then the partition that holds it.

icon_idea Another option is for a group of developers sharing a network. Unfortunately the subst command will not work with UNC paths like \main\c\webs. But you can have the same result the following way:

i) Go go to explorer (not internet explorer), menu Tools / Map network drive. Enter a drive letter (like z:) and as folder the UNC path to the remote folder that is the web root, for example:

\\maincomputer\customers\webs\GeorgeSite. Select also "reconnect at log-in" so you don't have to do this every time windows starts.

ii) Again the new drive letter is not a local copy. Saving to files in z: will also save them via the network to the remote machine.

Clone this wiki locally