Index: /main/trunk/gli/client-gli.bat
===================================================================
--- /main/trunk/gli/client-gli.bat	(revision 24828)
+++ /main/trunk/gli/client-gli.bat	(revision 24829)
@@ -1,3 +1,5 @@
 @echo off
+setlocal enabledelayedexpansion
+
 pushd "%CD%"
 CD /D "%~dp0"
@@ -94,5 +96,5 @@
 :: the client side. If we have Perl, pass in GSDLHOME for the -gsdl option and the PERLPATH.
 if "%PERLPATH%" == "" goto nogsdl
-	echo Perl and GSDLHOME (%GSDLHOME%) detected. Downloading is enabled.
+	echo Perl and GSDLHOME (!GSDLHOME!) detected. Downloading is enabled.
 	echo.
 	"%JAVA_EXECUTABLE%" -Xmx128M -cp classes/;GLI.jar;lib/apache.jar;lib/qfslib.jar org.greenstone.gatherer.GathererProg -use_remote_greenstone -gsdl "%GSDLHOME%" -perl "%PERLPATH%" %1 %2 %3 %4 %5 %6 %7 %8 %9
@@ -124,2 +126,3 @@
 set PROGABBR=
 popd
+endlocal
Index: /main/trunk/gli/findjava.bat
===================================================================
--- /main/trunk/gli/findjava.bat	(revision 24828)
+++ /main/trunk/gli/findjava.bat	(revision 24829)
@@ -1,3 +1,4 @@
 @echo off
+setlocal enabledelayedexpansion
 
 :: Environment Variables passed in: _VERSION, GLILANG and possibly also
@@ -21,14 +22,14 @@
 	:: else we look for a compiled version of search4j in a GS installation
 	if "%_VERSION%" == "2" (
-		set SEARCH4J_EXECUTABLE=%GSDLHOME%\bin\windows\search4j.exe
-		set HINT=%GSDLHOME%\packages\jre
+		set SEARCH4J_EXECUTABLE=!GSDLHOME!\bin\windows\search4j.exe
+		set HINT=!GSDLHOME!\packages\jre
 	)
-    	if "%_VERSION%" == "3" (
-		set SEARCH4J_EXECUTABLE=%GSDL3SRCHOME%\bin\search4j.exe
-		set HINT=%GSDL3SRCHOME%\packages\jre
+	if "%_VERSION%" == "3" (
+		set SEARCH4J_EXECUTABLE=!GSDL3SRCHOME!\bin\search4j.exe
+		set HINT=!GSDL3SRCHOME!\packages\jre
 	)
 	if not exist "%SEARCH4J_EXECUTABLE%" goto tryJava
 
-:setJexec    
+:setJexec
     "%SEARCH4J_EXECUTABLE%" -e -m "%MIN_VERSION%" -p "%HINT%" | winutil\setvar.exe JAVA_EXECUTABLE > %TMP%\set_java_executable.bat
     call "%TMP%\set_java_executable.bat"
@@ -56,5 +57,5 @@
     		echo WARNING: 
 		echo Java Runtime not bundled with this Greenstone installation.
-		echo Using JAVA_HOME: %JAVA_HOME%
+		echo Using JAVA_HOME: !JAVA_HOME!
 		echo ^(NOTE: this needs to be %DISPLAY_MIN_VERSION% or higher.^)
 		echo ***************************************************************************
@@ -97,2 +98,4 @@
 set MIN_VERSION=
 set DISPLAY_MIN_VERSION=
+
+endlocal & set JAVA_EXECUTABLE=%JAVA_EXECUTABLE%& set GS_JAVA_HOME=%GS_JAVA_HOME%
Index: /main/trunk/gli/gli.bat
===================================================================
--- /main/trunk/gli/gli.bat	(revision 24828)
+++ /main/trunk/gli/gli.bat	(revision 24829)
@@ -1,3 +1,5 @@
 @echo off
+setlocal enabledelayedexpansion
+
 color 0A
 set startdir=%CD%
@@ -48,9 +50,9 @@
 	echo.
 	echo Checking if the Greenstone collection directory is writable ...
-	(echo This is a temporary file. It is safe to delete it. > "%GSDLHOME%\collect\testing.tmp" ) 2>nul
+	(echo This is a temporary file. It is safe to delete it. > "!GSDLHOME!\collect\testing.tmp" ) 2>nul
 	if exist "%GSDLHOME%\collect\testing.tmp" goto deleteTempFile 
 	if "%1" == "Elevated" goto printWarning
 	echo ... FAILED
-	echo The %PROGNAME% cannot write to the collection directory (%GSDLHOME%\collect)
+	echo The %PROGNAME% cannot write to the collection directory (!GSDLHOME!\collect)
 	echo Requesting elevated status to become admin user to continue.
 	"%GSDLHOME%\bin\windows\gstart.exe" %0 Elevated %1 %2 %3 %4 %5 %6 %7 %8 %9
@@ -59,5 +61,5 @@
 :printWarning
 	echo ... FAILED
-	echo The %PROGNAME% cannot write to the log directory (%GSDLHOME%\collect). 
+	echo The %PROGNAME% cannot write to the log directory (!GSDLHOME!\collect). 
 	echo Attempting to continue without permissions.
 	goto shiftElevated
@@ -114,5 +116,5 @@
 if not "%GSDL3SRCHOME%" == "" (
     echo GSDL3SRCHOME:
-    echo %GSDL3SRCHOME%
+    echo !GSDL3SRCHOME!
 	echo.
 )
@@ -120,5 +122,5 @@
 if not "%GSDL3HOME%" == "" (
     echo GSDL3HOME:
-    echo %GSDL3HOME%
+    echo !GSDL3HOME!
 	echo.
 )
@@ -126,5 +128,5 @@
 if not "%GSDLHOME%" == "" (
     echo GSDLHOME:
-    echo %GSDLHOME%
+    echo !GSDLHOME!
 	echo.
 )
@@ -166,7 +168,7 @@
 :localLib
     if exist "%GSDLHOME%\server.exe" (
-	set locallib=%GSDLHOME%\server.exe
+	set locallib=!GSDLHOME!\server.exe
     ) else (
-	set locallib=%GSDLHOME%\gs2-web-server.bat
+	set locallib=!GSDLHOME!\gs2-web-server.bat
     )
 
@@ -211,2 +213,4 @@
 cd "%startdir%"
 set startdir=
+
+endlocal
Index: /main/trunk/gli/makejar.bat
===================================================================
--- /main/trunk/gli/makejar.bat	(revision 24828)
+++ /main/trunk/gli/makejar.bat	(revision 24829)
@@ -1,3 +1,5 @@
 echo off
+setlocal enabledelayedexpansion
+
 pushd "%CD%"
 CD /D "%~dp0"
@@ -115,6 +117,6 @@
 
   cd jar
-  "%JARPATH%\jar" xf ..\lib\apache.jar com org
-  "%JARPATH%\jar" xf ..\lib\qfslib.jar de
+  "!JARPATH!\jar" xf ..\lib\apache.jar com org
+  "!JARPATH!\jar" xf ..\lib\qfslib.jar de
 
   cd ..
@@ -152,5 +154,5 @@
 
     if not exist appletstore (
-      "%JARPATH%\keytool" -genkey -alias privateKey -keystore appletstore -storepass greenstone
+      "!JARPATH!\keytool" -genkey -alias privateKey -keystore appletstore -storepass greenstone
     )
 
@@ -158,8 +160,8 @@
     if exist appletpasswd (
       echo Using jarsigner to make signed jar file ...
-      "%JARPATH%\jarsigner" -keystore appletstore -signedjar SignedGatherer.jar GLI.jar privateKey < appletpasswd >NUL 2>NUL
+      "!JARPATH!\jarsigner" -keystore appletstore -signedjar SignedGatherer.jar GLI.jar privateKey < appletpasswd >NUL 2>NUL
       echo ... done.
     ) ELSE (
-      "%JARPATH%\jarsigner" -keystore appletstore -signedjar SignedGatherer.jar GLI.jar privateKey
+      "!JARPATH!\jarsigner" -keystore appletstore -signedjar SignedGatherer.jar GLI.jar privateKey
     )
     echo Installing SignedGatherer in ..\bin\java
@@ -169,2 +171,3 @@
 
 popd
+endlocal
Index: /main/trunk/greenstone2/bin/script/explode_metadata_database.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/explode_metadata_database.pl	(revision 24828)
+++ /main/trunk/greenstone2/bin/script/explode_metadata_database.pl	(revision 24829)
@@ -412,5 +412,5 @@
 	    my ($field, $value) = (@$pair);
 	    $field =~ s/^ex\.([^.]+)$/$1/; #remove any ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
-	    $value =~ s/\\\\/\\/g;
+	    $value =~ s/\\\\/\\/g;         # don't regex brackets () here though!
 	    my $document_file_full;
 
@@ -596,5 +596,5 @@
 	}
 	else {
-	    $orig_base_dir =~ s/\\/\\\\/g; # escape windows style slashes for the regex below
+		$orig_base_dir = &util::filename_to_regex($orig_base_dir); # escape windows style slashes for the regex below		
 	    if ($document_file_full =~ m/^$orig_base_dir.*/) {
 		# file local to metadata record
Index: /main/trunk/greenstone2/bin/script/schedule.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/schedule.pl	(revision 24828)
+++ /main/trunk/greenstone2/bin/script/schedule.pl	(revision 24829)
@@ -208,4 +208,8 @@
     $os = $ENV{'GSDLOS'};
     $path = $ENV{'PATH'}; 
+	
+	if("$gsdl" =~ m/(\\\(|\\\)| )/ ) { # () brackets or spaces in path
+		&gsprintf($out, "\n\n{schedule.filepath_warning}\n\n\n", $gsdl);
+	}
 
     my $service = "schedule"; 
Index: /main/trunk/greenstone2/bin/script/wvware.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/wvware.pl	(revision 24828)
+++ /main/trunk/greenstone2/bin/script/wvware.pl	(revision 24829)
@@ -333,4 +333,6 @@
     my $safe_reg_expression = $assoc_dir_path;
     $safe_reg_expression =~ s/\\/\\\\/g;
+	$safe_reg_expression =~ s@\(@\\(@g; # escape brackets
+	$safe_reg_expression =~ s@\)@\\)@g; # escape brackets
     $safe_reg_expression =~ s/\./\\./g;
     $safe_reg_expression =~ s/\-/\\-/g;
Index: /main/trunk/greenstone2/findjava.bat
===================================================================
--- /main/trunk/greenstone2/findjava.bat	(revision 24828)
+++ /main/trunk/greenstone2/findjava.bat	(revision 24829)
@@ -1,3 +1,5 @@
 @echo off
+
+setlocal enabledelayedexpansion
 
 :: Environment Variables passed in: _VERSION, GLILANG and possibly also
@@ -21,10 +23,10 @@
 	:: else we look for a compiled version of search4j in a GS installation
 	if "%_VERSION%" == "2" (
-		set SEARCH4J_EXECUTABLE=%GSDLHOME%\bin\windows\search4j.exe
-		set HINT=%GSDLHOME%\packages\jre
+		set SEARCH4J_EXECUTABLE=!GSDLHOME!\bin\windows\search4j.exe
+		set HINT=!GSDLHOME!\packages\jre
 	)
     if "%_VERSION%" == "3" (
-		set SEARCH4J_EXECUTABLE=%GSDL3SRCHOME%\bin\search4j.exe
-		set HINT=%GSDL3SRCHOME%\packages\jre
+		set SEARCH4J_EXECUTABLE=!GSDL3SRCHOME!\bin\search4j.exe
+		set HINT=!GSDL3SRCHOME!\packages\jre
 	)
 	if not exist "%SEARCH4J_EXECUTABLE%" goto tryJava
@@ -56,5 +58,5 @@
     		echo WARNING: 
 		echo Java Runtime not bundled with this Greenstone installation.
-		echo Using JAVA_HOME: %JAVA_HOME%
+		echo Using JAVA_HOME: !JAVA_HOME!
 		echo ^(NOTE: this needs to be %DISPLAY_MIN_VERSION% or higher.^)
 		echo ***************************************************************************
@@ -97,2 +99,4 @@
 set MIN_VERSION=
 set DISPLAY_MIN_VERSION=
+
+endlocal & set JAVA_EXECUTABLE=%JAVA_EXECUTABLE%& set GS_JAVA_HOME=%GS_JAVA_HOME%
Index: /main/trunk/greenstone2/gs2-server.bat
===================================================================
--- /main/trunk/greenstone2/gs2-server.bat	(revision 24828)
+++ /main/trunk/greenstone2/gs2-server.bat	(revision 24829)
@@ -1,3 +1,5 @@
 @echo off
+setlocal enabledelayedexpansion
+
 pushd "%CD%"
 CD /D "%~dp0"
@@ -8,9 +10,9 @@
 	echo Checking if the Greenstone log directory is writable ...
 	if not exist "%GSDLHOME%\etc\logs-gsi" goto missingLogDir
-	(echo This is a temporary file. It is safe to delete it. > "%GSDLHOME%\etc\logs-gsi\testing.tmp" ) 2>nul
+	(echo This is a temporary file. It is safe to delete it. > "!GSDLHOME!\etc\logs-gsi\testing.tmp" ) 2>nul
 	if exist "%GSDLHOME%\etc\logs-gsi\testing.tmp" goto deleteTempFile 
 	if "%1" == "Elevated" goto printWarning
 	echo ... FAILED
-	echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi)
+	echo The Greenstone server cannot write to the log directory (!GSDLHOME!\etc\logs-gsi)
 	echo Requesting elevated status to become admin user to continue.
 	"%GSDLHOME%\bin\windows\gstart.exe" %0 Elevated %1 %2 %3 %4 %5 %6 %7 %8 %9
@@ -19,5 +21,5 @@
 :missingLogDir
 	echo ... FAILED
-	echo The Greenstone log directory does not exist (%GSDLHOME%\etc\logs-gsi). Please either create this directory or reinstall Greenstone.
+	echo The Greenstone log directory does not exist (!GSDLHOME!\etc\logs-gsi). Please either create this directory or reinstall Greenstone.
 	pause
 	goto done
@@ -25,5 +27,5 @@
 :printWarning
 	echo ... FAILED
-	echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi). 
+	echo The Greenstone server cannot write to the log directory (!GSDLHOME!\etc\logs-gsi). 
 	echo Attempting to continue without permissions.
 	goto shiftElevated
@@ -52,2 +54,3 @@
 :done
 popd
+endlocal
Index: /main/trunk/greenstone2/gs2-web-server.bat
===================================================================
--- /main/trunk/greenstone2/gs2-web-server.bat	(revision 24828)
+++ /main/trunk/greenstone2/gs2-web-server.bat	(revision 24829)
@@ -1,4 +1,4 @@
 @echo off
-SETLOCAL
+SETLOCAL enabledelayedexpansion
 
 if "%serverlang%" == "" set serverlang=en
@@ -21,6 +21,6 @@
 :: Some users may set the above line manually
 if "%gsdl2path%" == "" (
-   set GSDLHOME=%CD%
-   set gsdl2path=%CD%
+   set GSDLHOME=!CD!
+   set gsdl2path=!CD!
 )
 
@@ -29,9 +29,9 @@
 	echo Checking if the Greenstone log directory is writable ...
 	if not exist "%GSDLHOME%\etc\logs-gsi" goto missingLogDir
-	(echo This is a temporary file. It is safe to delete it. > "%GSDLHOME%\etc\logs-gsi\testing.tmp" ) 2>nul
+	(echo This is a temporary file. It is safe to delete it. > "!GSDLHOME!\etc\logs-gsi\testing.tmp" ) 2>nul
 	if exist "%GSDLHOME%\etc\logs-gsi\testing.tmp" goto deleteTempFile  
 	if "%1" == "Elevated" goto printWarning
 	echo ... FAILED
-	echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi)
+	echo The Greenstone server cannot write to the log directory (!GSDLHOME!\etc\logs-gsi)
 	echo Requesting elevated status to become admin user to continue.
 	"%GSDLHOME%\bin\windows\gstart.exe" %0 Elevated %1 %2 %3 %4 %5 %6 %7 %8 %9
@@ -40,5 +40,5 @@
 :missingLogDir
 	echo ... FAILED
-	echo The Greenstone log directory does not exist (%GSDLHOME%\etc\logs-gsi). Please reinstall Greenstone.
+	echo The Greenstone log directory does not exist (!GSDLHOME!\etc\logs-gsi). Please reinstall Greenstone.
 	pause
 	goto exit
@@ -46,5 +46,5 @@
 :printWarning
 	echo ... FAILED
-	echo The Greenstone server cannot write to the log directory (%GSDLHOME%\etc\logs-gsi). 
+	echo The Greenstone server cannot write to the log directory (!GSDLHOME!\etc\logs-gsi). 
 	echo Attempting to continue without permissions.
 	goto shiftElevated
@@ -64,7 +64,7 @@
 :: If either GSDLHOME or GSDLOS is not set, need to run setup.bat first
 :: OR operations in an IF stmt: http://fixunix.com/ms-dos/21057-how-implement-if-condition-batch-file.html
-for %%i in ("%GSDLOS%" "%GSDLHOME%") do if %%i == "" set OR=True
+for %%i in ("!GSDLOS!" "!GSDLHOME!") do if %%i == "" set OR=True
 if "%OR%" == "True" (
-   pushd "%gsdl2path%"
+   pushd "!gsdl2path!"
    call setup.bat
 rem   echo **** GSDLOS: %GSDLOS% and GSDLHOME: %GSDLHOME%
@@ -97,5 +97,5 @@
 if exist "%GSDLHOME%\llssite.cfg" goto cfgfile
 if exist "%GSDLHOME%\llssite.cfg.in" (
-   copy "%GSDLHOME%\llssite.cfg.in" "%GSDLHOME%\llssite.cfg"
+   copy "!GSDLHOME!\llssite.cfg.in" "!GSDLHOME!\llssite.cfg"
 ) else (
    echo Warning: could not find llssite.cfg.in to create llssite.cfg from.
@@ -129,5 +129,5 @@
 if not "%CLASSPATH%" == "" if not "%CLASSPATH%" == "%test_cpath%" (
    echo   - CLASSPATH already correct:
-   echo %CLASSPATH%
+   echo !CLASSPATH!
    goto :eof
 )
@@ -173,14 +173,14 @@
    echo.
    echo Server log messages go to:
-   echo    "%GSDLHOME%\etc\logs-gsi\server.log"
+   echo    "!GSDLHOME!\etc\logs-gsi\server.log"
    echo.
    echo Using Apache web server located at:
-   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\bin\httpd"
+   echo    "!GSDLHOME!\apache-httpd\!GSDLOS!\bin\httpd"
    echo The Apache error log is at:
-   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\logs\error_log"
+   echo    "!GSDLHOME!\apache-httpd\!GSDLOS!\logs\error_log"
    echo The Apache configuration file template is at:
-   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf.in"
+   echo    "!GSDLHOME!\apache-httpd\!GSDLOS!\conf\httpd.conf.in"
    echo This is used to generate:
-   echo    "%GSDLHOME%\apache-httpd\%GSDLOS%\conf\httpd.conf"
+   echo    "!GSDLHOME!\apache-httpd\!GSDLOS!\conf\httpd.conf"
    echo    each time Enter Library is pressed or otherwise activated.
    echo ***************************************************************
Index: /main/trunk/greenstone2/gsicontrol.bat
===================================================================
--- /main/trunk/greenstone2/gsicontrol.bat	(revision 24828)
+++ /main/trunk/greenstone2/gsicontrol.bat	(revision 24829)
@@ -1,5 +1,6 @@
 @echo off
 
-setlocal
+setlocal enabledelayedexpansion
+
 ::set testdone=0
 set startdir=%CD%
@@ -25,5 +26,5 @@
 :: directory (after replacing spaces, colons and backslashes)
 set signal=%GSDLHOME: =_%
-for /F "delims=*" %%T in ('"echo %signal%| %_sed% s@[\\:]@_@g"') do set signal=%%T
+for /F "delims=*" %%T in ('"echo !signal!| !_sed! s@[\\:]@_@g"') do set signal=%%T
 set GS2_APACHE_HTTPD_SIGNAL=GS2_APACHE_HTTPD_%signal%
 set signal=
@@ -164,9 +165,9 @@
 	echo ^<enabled^>true
 	echo ^<groups^>administrator,colbuilder,all-collections-editor
-	echo ^<password^>%encrypted_password%
+	echo ^<password^>!encrypted_password!
 	echo ^<username^>admin
-	) > "%GSDLHOME%\etc\users.txt"
-	type "%GSDLHOME%\etc\users.txt" | txt2db -append "%GSDLHOME%\etc\users.gdb"
-	del "%GSDLHOME%\etc\users.txt"	
+	) > "!GSDLHOME!\etc\users.txt"
+	type "!GSDLHOME!\etc\users.txt" | txt2db -append "!GSDLHOME!\etc\users.gdb"
+	del "!GSDLHOME!\etc\users.txt"	
 ) else (
 	echo Did not set password 
@@ -185,8 +186,8 @@
 
 set gshome=
-FOR /F "tokens=*" %%G IN ('findstr /R ^gsdlhome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do (call :concat %%G)
+FOR /F "tokens=*" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
 
 :: The following doesn't work if there are spaces in the gsdlhome filepath
-::FOR /F "tokens=2" %%G IN ('findstr /R ^gsdlhome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do set gshome=%%G
+::FOR /F "tokens=2" %%G IN ('findstr /R ^gsdlhome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set gshome=%%G
 :: before string comparison, remove any quotes around gsdlhome value defined in config file
 :: if not [%gshome%] == [] set gshome=%gshome:"=%
@@ -201,10 +202,10 @@
 :: The path to the included perl at the top of gliserver.pl needs to use the new gsdlhome
 :: On windows, the path in gliserver.pl uses backslashes
-for /F "delims=*" %%T in ('"echo %gshome%| %_sed% s@\\@\\\\@g"') do set safeoldhome=%%T
-for /F "delims=*" %%T in ('"echo %gsdlhome%| %_sed% s@\\@\\\\@g"') do set safenewhome=%%T
+for /F "delims=*" %%T in ('"echo !gshome!| !_sed! s@\\@\\\\@g"') do set safeoldhome=%%T
+for /F "delims=*" %%T in ('"echo !gsdlhome!| !_sed! s@\\@\\\\@g"') do set safenewhome=%%T
 if exist "%GSDLHOME%\bin\%GSDLOS%\perl" (
-  copy "%cgibin%\gliserver.pl" "%cgibin%\gliserver.pl.bak"
-  type "%cgibin%\gliserver.pl.bak" | %_sed% "s@%safeoldhome%@%safenewhome%@g" > "%cgibin%\gliserver.pl"
-  del "%cgibin%\gliserver.pl.bak"
+  copy "!cgibin!\gliserver.pl" "!cgibin!\gliserver.pl.bak"
+  type "!cgibin!\gliserver.pl.bak" | !_sed! "s@!safeoldhome!@!safenewhome!@g" > "!cgibin!\gliserver.pl"
+  del "!cgibin!\gliserver.pl.bak"
 )
 set safenewhome=
@@ -212,5 +213,5 @@
 set gshome=
 
-for /F "delims=*" %%T in ('"echo %GSDLHOME%| %_sed% s@\\@\/@g"') do set safepath=%%T
+for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
 
 :: Also re-initialise the log4j.properties and force regeneration of 
@@ -247,5 +248,5 @@
 echo. >> "%cgibin%\gsdlsite.cfg"
 
-for /F "delims=*" %%T in ('"echo %GSDLHOME%| %_sed% s@\\@\\\\@g"') do set safepath=%%T
+for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\\\\@g"') do set safepath=%%T
 %_sed% "s@\*\*GSDLHOME\*\*@\"%safepath%\"@g" "%cgibin%\gsdlsite.cfg.in" >> "%cgibin%\gsdlsite.cfg"
 set safepath=
@@ -266,12 +267,12 @@
 ::configure-apache
 :cfgapach
-if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file %configfile% does not exist. Using default llssite.cfg)
+if not "%configfile%" == "" if exist "%configfile%" (goto cfgport) else (echo Config file !configfile! does not exist. Using default llssite.cfg)
 
 if exist "%GSDLHOME%\llssite.cfg" (
-  set configfile=%GSDLHOME%\llssite.cfg
+  set configfile=!GSDLHOME!\llssite.cfg
   goto cfgport 
 )
 if not exist "%GSDLHOME%\llssite.cfg.in" (
-  echo Unable to proceed as neither %GSDLHOME%\llssite.cfg nor %GSDLHOME%\llssite.cfg.in could be found
+  echo Unable to proceed as neither !GSDLHOME!\llssite.cfg nor !GSDLHOME!\llssite.cfg.in could be found
   goto exit
 )
@@ -282,8 +283,8 @@
 echo Configuring the apache webserver...
 :: See http://ss64.com/nt/for_cmd.html, http://ss64.com/nt/findstr.html (and http://ss64.com/nt/find.html)
-FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^portnumber "%configfile%"') do set port=%%G
-FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hostIP "%configfile%"') do set hostIP=%%G
-FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hosts "%configfile%"') do set hosts=%%G
-FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^externalaccess "%configfile%"') do set allowfromall=%%G
+FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^portnumber "!configfile!"') do set port=%%G
+FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hostIP "!configfile!"') do set hostIP=%%G
+FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^hosts "!configfile!"') do set hosts=%%G
+FOR /F "tokens=2 delims==" %%G IN ('findstr /R ^externalaccess "!configfile!"') do set allowfromall=%%G
 
 if "%allowfromall%" == "1" set allowfromall="Allow"& goto portcon
@@ -313,19 +314,18 @@
    goto exit
 )
-if "%safepath%" == "" for /F "delims=*" %%T in ('"echo %GSDLHOME%| %_sed% s@\\@\/@g"') do set safepath=%%T
+if "%safepath%" == "" for /F "delims=*" %%T in ('"echo !GSDLHOME!| !_sed! s@\\@\/@g"') do set safepath=%%T
 
 :: Doesn't work if there are spaces in the collecthome path in gsdlsite.cfg
-::if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do set COLLECTHOME=%%G
+::if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=2" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do set COLLECTHOME=%%G
 
 :: variable in subroutine concat is called gshome, 
 :: so forced to use it here for collecthome
 set gshome=
-if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=*" %%G IN ('findstr /R ^collecthome "%GSDLHOME%\%cgibin%\gsdlsite.cfg"') do (call :concat %%G)
+if exist "%GSDLHOME%\%cgibin%\gsdlsite.cfg" FOR /F "tokens=*" %%G IN ('findstr /R ^collecthome "!GSDLHOME!\!cgibin!\gsdlsite.cfg"') do call :concat %%G
 set COLLECTHOME=%gshome%
 set gshome=
 
 if "%COLLECTHOME%" == "" set COLLECTHOME=%GSDLHOME%\collect
-for /F "delims=*" %%T in ('"echo %COLLECTHOME%| %_sed% s@\\@\/@g"') do set safecollectpath=%%T
-
+for /F "delims=*" %%T in ('"echo !COLLECTHOME!| !_sed! s@\\@\/@g"') do set safecollectpath=%%T
 
 echo Port: %port%
Index: /main/trunk/greenstone2/perllib/inexport.pm
===================================================================
--- /main/trunk/greenstone2/perllib/inexport.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/inexport.pm	(revision 24829)
@@ -1035,5 +1035,5 @@
 
 	$situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
-	$situated_dir =~ s/\\/\\\\/g;  # need to protect windows slash \ in regular expression
+	$situated_dir = &util::filename_to_regex($situated_dir); # need to escape windows slash \ and brackets in regular expression
 	
 	# Go through existing_files, and mark anything that is contained
Index: /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24829)
@@ -481,12 +481,9 @@
     foreach my $ek (@extrametakeys) {
 	my ($subdir_re,$extrakey_dir) = &File::Basename::fileparse($ek);
-	$extrakey_dir =~ s/\\\./\./g; # remove RE syntax for .
-	$extrakey_dir =~ s/\\\\/\\/g; # remove RE syntax for \
+	$extrakey_dir = &util::unregex_filename($extrakey_dir);
 
 	my $dirsep_re = &util::get_re_dirsep();
 
-	my $ek_non_re = $ek;
-	$ek_non_re =~ s/\\\./\./g; # remove RE syntax for .
-	$ek_non_re =~ s/\\\\/\\/g; # remove RE syntax for \
+	my $ek_non_re = &util::unregex_filename($ek);
 	if ($ek_non_re =~ m/$dirsep_re/) { # specifies at least one directory
 	    my $md = $extrametadata{$ek};
Index: /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm	(revision 24829)
@@ -735,5 +735,5 @@
 #	    if (defined $collect_dir) {
 #		my $collect_dir_re_safe = $collect_dir;
-#		$collect_dir_re_safe =~ s/\\/\\\\/g;
+#		$collect_dir_re_safe =~ s/\\/\\\\/g; # use &util::filename_to_regex()
 #		$collect_dir_re_safe =~ s/\./\\./g;##
 
Index: /main/trunk/greenstone2/perllib/plugouts/DSpacePlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/DSpacePlugout.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/plugouts/DSpacePlugout.pm	(revision 24829)
@@ -300,7 +300,6 @@
 	$real_filename =~ s/^\\(.*)/$1/i;
 	if (-e $real_filename) {
-		# escape backslashes in path for upcoming regex match
-		my $escaped_source_filename = $source_filename;
-		$escaped_source_filename =~ s/\\/\\\\/g;		
+		# escape backslashes and brackets in path for upcoming regex match
+		my $escaped_source_filename = &util::filename_to_regex($source_filename);
 	    if ($real_filename =~ m/$escaped_source_filename$/) {
 		next;
Index: /main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm	(revision 24829)
@@ -613,5 +613,5 @@
 	
 	my $gsdl_href = &util::filename_cat($working_dir, $fname);
-	$collectparent =~ s/\\/\\\\/g;             # escape reserved metacharacter \ in path (by replacing it with \\) for substitution
+	$collectparent = &util::filename_to_regex($collectparent); # escape reserved metacharacter \ in path (by replacing it with \\) for substitution
 	$gsdl_href =~ s/^$collectparent(\/|\\)?//; # remove the collectparent path in gsdl_href and any trailing slash
 	$gsdl_href =~ s/\\/\//g;                   # make sure we have url paths (which only use / not \)
@@ -744,5 +744,5 @@
 
 	       my $gsdl_href = &util::filename_cat($working_dir,$assfilePath);
-	       $collectparent =~ s/\\/\\\\/g;             # escape reserved metacharacter \ in path (by replacing it with \\) for substitution
+	       $collectparent = &util::filename_to_regex($collectparent); # escape reserved metacharacter \ in path (by replacing it with \\) for substitution
 	       $gsdl_href =~ s/^$collectparent(\/|\\)?//; # remove the collectparent path in gsdl_href and any trailing slash
 	       $gsdl_href =~ s/\\/\//g;                   # make sure we have url paths (which only use / not \)
Index: /main/trunk/greenstone2/perllib/strings.properties
===================================================================
--- /main/trunk/greenstone2/perllib/strings.properties	(revision 24828)
+++ /main/trunk/greenstone2/perllib/strings.properties	(revision 24829)
@@ -332,4 +332,5 @@
 schedule.frequency.daily:Re-build every day
 schedule.frequency.weekly:Re-build every week
+schedule.filepath_warning:**** Warning: schedule.pl may not work when Greenstone is installed in a path containing brackets and/or spaces: %s.
 schedule.action:How to set up automatic re-building
 schedule.action.add:Schedule automatic re-building
Index: /main/trunk/greenstone2/perllib/util.pm
===================================================================
--- /main/trunk/greenstone2/perllib/util.pm	(revision 24828)
+++ /main/trunk/greenstone2/perllib/util.pm	(revision 24829)
@@ -839,6 +839,24 @@
     # need to put single backslash back to double so that regex works
     if ($ENV{'GSDLOS'} =~ /^windows$/i) {
-	$filename =~ s/\\/\\\\/g;
-    }
+		$filename =~ s/\\/\\\\/g;
+    }
+	
+	# note that the first part of a substitution is a regex, so RE chars need to be escaped,
+	# the second part of a substitution is not a regex, so for e.g. full-stop can be specified literally
+	$filename =~ s/\./\\./g; # in case there are extensions/other full stops, escape them
+	$filename =~ s@\(@\\(@g; # escape brackets
+	$filename =~ s@\)@\\)@g; # escape brackets
+	
+    return $filename;
+}
+
+sub unregex_filename {
+    my $filename = shift (@_);
+
+    # need to put doubled backslashes for regex back to single
+    $filename =~ s/\\\\/\\/g; # remove RE syntax for \    
+	$filename =~ s/\\\./\./g; # remove RE syntax for .
+	$filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
+	$filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
     return $filename;
 }
@@ -928,6 +946,5 @@
     if ($ENV{'GSDLOS'} =~ /^windows$/i) 
     {
-	my $escaped_val = $val;
-	$escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
+	my $escaped_val = &filename_to_regex($val); # escape any Windows backslashes and brackets for upcoming regex
 	if (!defined($ENV{$var})) {
 	    $ENV{$var} = "$val";
@@ -953,6 +970,5 @@
     if ($ENV{'GSDLOS'} =~ /^windows$/i) 
     {
-	my $escaped_val = $val;
-	$escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
+	my $escaped_val = &filename_to_regex($val); # escape any Windows backslashes and brackets for upcoming regex
 	if (!defined($ENV{$var})) {
 	    $ENV{$var} = "$val";
@@ -1048,6 +1064,5 @@
     }
 	
-    $within_dir =~ s/\\/\\\\/g; # escape DOS style file separator
-
+	$within_dir = &filename_to_regex($within_dir); # escape DOS style file separator and brackets	
     if ($filename =~ m/^$within_dir(.*)$/) {
 	$filename = $1;
