Can't execute .bat if foldername has spece

October 3, 2011 at 00:03:37
Specs: Windows XP
I have two directories 1. New Folder 2. NewFolder. IF I exctract some.zip file which contains install.bat and etc in the directories 1 and 2. For dir 1 install.bat won't execute and for dir 2 it will work fine. So if directory name has space then install.bat is not working. Please help.
Below is script that is not working for space. The path is not hardcoded to put double quotes. so how to solve is problem.

@echo off
setlocal

:: Default options

set USE_OWN_IMAGE_FILE=no
set USE_OWN_TMP_DIR=no
set USE_OWN_JAVA=no
set USE_BATCH_MODE=no

:: Determine installation program directory

for %%i in (%0) do set INSTALLER_DIR_LONG=%%~dpi
for %%i in (%INSTALLER_DIR_LONG%) do set INSTALLER_DIR=%%~fsi

:: Default image file

if exist %INSTALLER_DIR%\core.inf set IMAGE_FILE=%INSTALLER_DIR%\core.inf

:: Check arguments

:CHECK_ARGUMENTS
if {%1}=={} goto :END_OF_ARGUMENTS
if /I {%1}=={-h} goto :USAGE
if /I {%1}=={-i} set USE_OWN_IMAGE_FILE=yes
if /I {%1}=={-i} set IMAGE_FILE=%2
if /I {%1}=={-t} set USE_OWN_TMP_DIR=yes
if /I {%1}=={-t} set TMP=%2
if /I {%1}=={-j} set USE_OWN_JAVA=yes
if /I {%1}=={-j} set JAVA=%2
if /I {%1}=={-b} (
shift
set USE_BATCH_MODE=yes
goto :END_OF_ARGUMENTS
)
shift
goto :CHECK_ARGUMENTS
:END_OF_ARGUMENTS

if {%USE_OWN_IMAGE_FILE%}=={yes} (
if {%IMAGE_FILE%}=={} goto :USAGE
)

if {%USE_OWN_TMP_DIR%}=={yes} (
if {%TMP%}=={} goto :USAGE
)

if {%USE_OWN_JAVA%}=={yes} (
if {%JAVA%}=={} goto :USAGE
)


:: Create temporary directory

echo Creating the temporary directory...
if {%USE_OWN_TMP_DIR%}=={no} (
if not defined TMP set TMP=C:\TEMP
)
for /f %%i in ('%%INSTALLER_DIR%%\util\win32\tempnam.exe %TMP% ose_') do set TMP_DIR=%%i
if not exist %TMP_DIR% mkdir %TMP_DIR%

:: Unzip Java Runtime Environment

if {%USE_OWN_JAVA%}=={yes} goto :LAUNCH_INSTALLATION_PROGRAM
echo Unzipping the Java Runtime Environment...
set JRE_VERSION=1.5.0_14
set JRE=%INSTALLER_DIR%\jre\win32\jre-%JRE_VERSION%.zip
%INSTALLER_DIR%\unzip\win32\unzip.exe -q -d %TMP_DIR% %JRE%
set JAVA=%TMP_DIR%\jre_win32\bin\java


if {%USE_BATCH_MODE%}=={yes} (
%JAVA% -Dose.installer.dir=%INSTALLER_DIR% -cp %INSTALLER_DIR%\install.jar com.ose.install.BatchInstaller %IMAGE_FILE% %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
%JAVA% -Dose.installer.dir=%INSTALLER_DIR% -cp %INSTALLER_DIR%\install.jar com.ose.install.ui.MainFrame %IMAGE_FILE%
)

:: Remove temporary directory

echo Removing the temporary directory...
rmdir /S /Q %TMP_DIR%

:: Exit

echo Finished!
goto :EOF

:: Help message

:USAGE
echo Usage:
echo install.bat [ -h ^| [ -i ^<imagefile^> ] [ -t ^<tmpdir^> ] [ -j ^<jre^> ] [ -b -p ^<installdir^> -k ^<keyfile^> [ -v ] [ -e ] [ -o ] [ -s ] [ -a ] ] ]
echo Where:
echo -h prints this help message.
echo -i ^<imagefile^> specifies installation image file.
echo -t ^<tmpdir^> specifies temporary directory to be used.
echo -j ^<jre^> use installed JRE (path to Java executable).
echo -b batch installation mode (no GUI), requires the -p and -k options.
echo -p ^<installdir^> in batch mode: specifies installation directory.
echo -k ^<keyfile^> in batch mode: specifies decryption key file.
echo -v in batch mode: verbose output.
echo -e in batch mode: ignore errors.
echo -o in batch mode: silently overwrite files.
echo -s in batch mode: create shortcuts.
echo -a in batch mode: install all packages.
goto :EOF


See More: Cant execute .bat if foldername has spece

Report •


#1
October 3, 2011 at 12:39:45
New Folder 2

Rename this so there are no spaces, such as New-Folder_2


Report •

#2
October 3, 2011 at 13:28:00
Below is script that is not working for space. The path is not hardcoded to put double quotes. so how to solve is problem.

Batch treats variables as strings almost as if they were hard coded, so add your double quotes!

Using double quotes with the if statements causes it to accept spaces:

if "%USE_OWN_IMAGE_FILE%"=="yes" (

The only issue is to ensure the variable doesn't contain double quotes. You can strip the double quotes with %var:str=rep%, but be warned that trick will only work on variables that are defined.

As for parameters, you can remove surrounding quotes by using the ~ syntax:

%~1


Report •
Related Solutions


Ask Question