Index: /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24931)
+++ /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24932)
@@ -382,5 +382,5 @@
 	$dirname = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
     }
-
+	
     my $directory_ok = $self->check_directory_path($dirname);
     return $directory_ok unless (defined $directory_ok && $directory_ok == 1);
@@ -424,15 +424,17 @@
 
 
-    my $os_dirsep = &util::get_os_dirsep();
+	my $os_dirsep = &util::get_os_dirsep();
     my $dirsep    = &util::get_dirsep();
     my $base_dir_regexp = $base_dir;
     $base_dir_regexp =~ s/\//$os_dirsep/g;
-    my $local_dirname = $dirname;
-    
-    $local_dirname =~ s/^$base_dir_regexp($os_dirsep)*//;
+       
+	# Want to get relative path of local_dirname within the base_directory
+	# but with URL style slashes. 
+	my $local_dirname = &util::filename_within_directory_url_format($dirname, $base_dir);
+
     # if we are in import folder, then local_dirname will be empty
     if ($local_dirname ne "") {
-	# look for extra metadata passed down from higher folders 
-	$local_dirname .= $dirsep;
+	# look for extra metadata passed down from higher folders 	
+	$local_dirname .= "/"; # closing slash must be URL type slash also and not $dirsep;
 	if (defined $self->{'subdir_extrametakeys'}->{$local_dirname}) {
 	    my $extrakeys = $self->{'subdir_extrametakeys'}->{$local_dirname};
@@ -491,5 +493,5 @@
 
 	    my $subdir_extrametakeys = $self->{'subdir_extrametakeys'};
-
+		
 	    my $subdir_rec = { 're' => $subdir_re, 'md' => $md, 'mf' => $mf };
 
Index: /main/trunk/greenstone2/perllib/util.pm
===================================================================
--- /main/trunk/greenstone2/perllib/util.pm	(revision 24931)
+++ /main/trunk/greenstone2/perllib/util.pm	(revision 24932)
@@ -845,4 +845,6 @@
 	$filename =~ s@\(@\\(@g; # escape brackets
 	$filename =~ s@\)@\\)@g; # escape brackets
+	$filename =~ s@\[@\\[@g; # escape brackets
+	$filename =~ s@\]@\\]@g; # escape brackets
 	
     return $filename;
@@ -857,4 +859,6 @@
 	$filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
 	$filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
+	$filename =~ s@\\\[@[@g; # remove RE syntax for [ => "\[" turns into "["
+	$filename =~ s@\\\]@]@g; # remove RE syntax for ] => "\]" turns into "]"
     return $filename;
 }
@@ -1032,5 +1036,6 @@
 }
 
-
+# If filename is relative to within_dir, returns the relative path of filename to that directory
+# with slashes in the filename returned as they were in the original (absolute) filename.
 sub filename_within_directory
 {
@@ -1049,4 +1054,41 @@
     return $filename;
 }
+
+# If filename is relative to within_dir, returns the relative path of filename to that directory in URL format.
+# Filename and within_dir can be any type of slashes, but will be compared as URLs (i.e. unix-style slashes).
+# The subpath returned will also be a URL type filename.
+sub filename_within_directory_url_format
+{
+    my ($filename,$within_dir) = @_;
+	
+	# convert parameters only to / slashes if Windows
+    
+	my $filename_urlformat = $filename;
+	my $within_dir_urlformat = $within_dir;
+	
+	if ($ENV{'GSDLOS'} =~ /^windows$/i) {
+		# Only need to worry about Windows, as Unix style directories already in url-format
+		# Convert Windows style \ => /
+		$filename_urlformat =~ s@\\@/@g;
+		$within_dir_urlformat =~ s@\\@/@g;
+	}
+	
+	#if ($within_dir_urlformat !~ m/\/$/) {
+		# make sure directory ends with a slash
+		#$within_dir_urlformat .= "/";
+    #}
+	
+	my $within_dir_urlformat_re = &filename_to_regex($within_dir_urlformat); # escape any special RE characters, such as brackets
+	
+	#print STDERR "@@@@@ $filename_urlformat =~ $within_dir_urlformat_re\n";
+	
+	# dir prefix may or may not end with a slash (this is discarded when extracting the sub-filepath)
+    if ($filename_urlformat =~ m/^$within_dir_urlformat_re(?:\/)*(.*)$/) {
+		$filename_urlformat = $1;
+    }
+    
+    return $filename_urlformat;
+}
+
 
 sub filename_within_collection
