I am trying to install the below script as .bat file and it works fine when I run manually. but when I try to deploy from sccm it fails to install and gives an error (WKS85001A.exe is not recognized as an external or internal command, operable program or batch file). I did checked the PATH environment variables and I do see %systemRoot%system32 path. so help me to fix this error.

Running in windows 7 64 bit platform.

Below is the batch script.(Script is toobig :(  )


@echo OFF
REM #-----------------------------------------------------------------#
REM #        Copyright (c) 2010, CA. All rights reserved.             #
REM #-----------------------------------------------------------------#
REM #            Setup script for installing PTF's Only               #
REM #                                                                 #
REM # This file is delivered with each PTF to be executed to install  #
REM # the PTF.  It uses the supplied *.CTL file for processing.       #
REM #                                                                 #
REM # Parameter 1: Path to apply PTF to.  Defaults to Gen top level   #
REM # directory.                                                      #
REM #                                                                 #
REM # Logging:                                                        #
REM # A PTF log will exist in the PTF's .BAK directory.  This log     #
REM # file will record all installs and backouts for this particular  #
REM # PTF.  This file is appended to.                                 #
REM # A Master PTF log file will exist in the PTF top level directory #
REM # (%GENxx%PTF_Backups). This log file will record all PTF         #
REM # installs and backouts for this particular release of Gen.       #
REM # This file is also appended to.                                  #
REM #-----------------------------------------------------------------#
REM # HISTORY                                                         #
REM #                                                                 #
REM # 07/07/10  RAL  Introduced for 8.0 PTF Suport                    #
REM # 10/01/10  RAL  Create new directories if needed                 #
REM # 10/07/10  RAL  Added logic to handle Plugin files containing    #
REM #                version numbers.                                 #
REM # 10/14/10  RAL  Enhance version number logic so that plugins are #
REM #                not backed up when PTF is applied.               #
REM #                Add logic so that setup can prompt for location  #
REM #                based on CTL file tag.                           #
REM #                Do not allow PTF to be applied more than once.   #
REM # 10/18/10  RAL  Provide default location for installation based  #
REM #                on TARGETDIR in control file.  If not provided,  #
REM #                then the customer will be prompted for an        #
REM #                installation location.  Also, the setup          #
REM #                can be executed with the location being passed   #
REM #                as the 1st parameter, which will override the    #
REM #                value in the control file.                       #
REM # 10/27/10  RAL  Write ok file into backup directory to indicate  #
REM #                that the PTF has been applied.  This is used to  #
REM #                prevent mulitple applications of the same PTF.   #
REM #                Ensure that backouts occur from PTF_Backups dir. #
REM # 10/28/10  RAL  Clean up zip_expanded_dir after applying PTF.    #
REM # 11/01/10  RAL  Delete any newly created directories after a     #
REM #                backout has occurred.                            #
REM # 11/04/10  RAL  Replace 'mv' with 'rename'                       #
REM # 11/05/10  RAL  Remove extraneous spaces that appear in progress #
REM #                indicator (represented by '.'s) that appear on   #
REM #                the display during install or backout.           #
REM # 11/08/10  RAL  Verify %GEN85% if provided as TARGETDIR          #
REM #                value in Control File.                           #
REM # 11/11/10  RAL  Rename setup.bat and install as uninstall.bat.   #
REM #                Cleanup if Quit selected during install.         #
REM #                Self-delete setup when needed.                   #
REM #                If nothing was installed, inform user.           #
REM #                Allow multiple installs, just don't backup if it #
REM #                has already been done.  Ok file is no longer     #
REM #                needed.                                          #
REM # 01/06/11  RAL  Correct self-delete at end of procedure.         #
REM # 01/11/11  RAL  Corrected use of case-insensitive checks.        #
REM #                Added ability to add post-processing step.       #
REM # 02/25/11  JDG (per RAL)  Removed trailing spaces from action.   #
REM # 03/29/11  RAL  Correct exiting from subroutines                 #
REM # 04/14/11  RAL  Create new directory if needed for FORCED_COPY   #
REM # 05/06/11  RAL  Added "***ERROR***" to error lines in log file   #
REM # 05/24/11  JDG (per RAL)  In zip handling, checks for trailing   #
REM #                delimiter, and if present removes it. Also, Copy #
REM #                command errors are now sent to the PTF log.      #
REM # 06/03/11  RAL  Log all post-install displayed information       #
REM # 06/10/11  RAL  Pass %PTFinstall_dir% and 2nd parameter to       #
REM #                any post install procedures                      #
REM # 06/15/11  RAL  Ensure that we are running the bat file from the #
REM #                directory it is executed from.  This can be a    #
REM #                problem when 'run as administrator' is in use.   #
REM # 06/22/11  RAL  Fix post install logging file location.          #
REM # 07/25/11  RAL  Fix post install reinstall or uninstall issue    #
REM # 08/09/11  RAL  Only indicate that no files were installed if    #
REM #                no post-processing also did not take place.      #
REM # 10/05/11  CDBA prevent "system cannot find the file specified"  #
REM #                messages when .log file does not exist.          #
REM # 11/29/11  RAL  Correct check for existence of directory         #
REM # 01/13/12  RAL  Add NOTE to stop Gen apps prior to PTF install   #
REM #                or backout.  Also added error message if error   #
REM #                occurs during install or backout.                #
REM # 01/17/12  RAL  Clean up post processing bat files (if any exist)#
REM #                prior to an install.  This will prevent appending#
REM #                to any existing post processing bat files.       #
REM # 01/27/12  RAL  Prevent additional backups when multiple installs#
REM #                are being performed.                             #
REM # 02/07/12  RAL  Handle install/backout of multiple versions of a #
REM #                plugin.                                          #
REM #-----------------------------------------------------------------#
SETLOCAL

REM #-----------------------------------------------------------------#
REM # Start of main processing logic                                  #
REM #                                                                 #
REM # 1 - Establish parameters/variables                              #
REM # 2 - Extract maintenance zip                                     #
REM # 3 - perform backup of files to be updated                       #
REM # 4 - perform install of packaged files                           #
REM # 5 - cleanup environment                                         #
REM #-----------------------------------------------------------------#

REM
REM Ensure that we are executing this bat file from the directory it
REM is invoked from.  This can be a problem when 'run as administrator'
REM is in use.
REM
chdir %~dp0

REM
REM Set local variables
REM
set PTF_batch_file_to_delete=0
set PTFname=WKS85001
set PTF_gen_release_variable=%%GEN85%%
set PTF_process=setup
set PTF_error_occurred=0
set PTF_something_was_installed=0

REM
REM See if alternative installation directory has been provided
REM as Parameter 1.  If alternative installation directory is
REM provided, then the directory provided by TARGETDIR in the
REM control file will be overwritten by this one.
REM
set PTFinstall_dir=
if not [%1] == [] set PTFinstall_dir=%1
if not [%PTFinstall_dir%] == [] set PTFinstall_dir=%PTFinstall_dir:"=%

REM
REM versionno environment variable is used a placeholder within plugin
REM filenames to represent a version number.  This version number changes,
REM so this placeholder is used to locate the correct plugin filename.
REM
set versionno=%%versionno%%

REM
REM Provide Install Flowerbox
REM
call :display_install_flowerbox

REM
REM Request action to perform
REM
call :request_action PTFaction
if %return_code%==0 goto xit
if %PTFaction% == "BACKOUT" echo Performing Backout of %PTFname%...
if %PTFaction% == "INSTALL" echo Performing Install of %PTFname%...
if %PTFaction% == "BACKOUT" goto set_needed_directories

REM
REM Validate installation directory:
REM
REM Installation Directory can be obtained one of 3 ways:
REM   Passed as Parameter 1
REM   Read from the Control File
REM   Prompted from the display
REM
REM If Installation Directory has been provided as Parameter 1, it will be one of the following:
REM   1) Empty - not supplied
REM   2) Directory path provided as literal string
REM   3) Directory path provided as valid environment variable
REM   4) Environment variable that was not evaluated (not set)
REM
REM If Installation Directory has been read from the Control File, it will be one of the following:
REM   1) Empty - TARGETDIR=<blank>
REM   2) Empty - no TARGETDIR in file
REM   3) Directory path provided as literal string
REM   4) Directory path provided as valid environment variable
REM   5) Environment variable that was not evaluated (not set)
REM
REM If Installation Directory is provided from display, it will be one of the following:
REM   1) Directory path provided as literal string
REM   2) Directory path provided as valid environment variable
REM   3) Environment variable that was not evaluated (not set)
REM
REM Do Work:
REM
REM Check if install directory has been provided as Parameter 1:
REM   a) if empty, was not provided
REM   b) If matches env var, env var not set
REM   c) If directory, validate directory
REM  
if "%PTFinstall_dir%"=="" goto call_get_install_dir_from_ctlfile
if /i "%PTFinstall_dir%"=="%PTF_gen_release_variable%" goto env_var_not_set
goto validate_install_directory
REM
REM Check for install directory from Control File:
REM   a) if empty, was not provided
REM   b) If matches env var, env var not set
REM   c) If directory, validate directory
REM  
:call_get_install_dir_from_ctlfile
call :get_install_dir_from_ctlfile
if %return_code%==0 goto xit
if "%PTFinstall_dir%"=="" goto call_get_install_dir_from_prompt
if "%PTFinstall_dir%"=="%PTF_gen_release_variable%" goto env_var_not_set
goto validate_install_directory
REM
REM Check for install directory from Control File:
REM   a) if empty, was not provided
REM   b) If matches env var, env var not set
REM   c) If directory, validate directory
REM  
:call_get_install_dir_from_prompt
call :get_install_dir_from_prompt
if /i "%PTFinstall_dir%"=="%PTF_gen_release_variable%" goto env_var_not_set
goto validate_install_directory
REM
:env_var_not_set
echo.
echo %PTF_gen_release_variable% is not defined. Installation Terminated.
goto xit
REM
:validate_install_directory
REM
REM Strip trailing delim from install dir if present
REM
if "%PTFinstall_dir:~-1%"=="\" set PTFinstall_dir=%PTFinstall_dir:~0,-1%
echo.
echo Installation Directory %PTFinstall_dir% will be used for this PTF.
call :validate_directory "%PTFinstall_dir%"
if %return_code%==1 goto set_needed_directories
echo.
echo ERROR: The provided directory %PTFinstall_dir% does not exist.
echo        Please verify the PTF installation directory and rerun this procedure.
goto xit

:set_needed_directories
REM
REM Set required directories (backup, zip)
REM
call :establish_needed_directories

REM
REM Log PTF Header information to document work commencing
REM
call :log_flowerbox HEADER "%backup_dir%\%PTFname%.log" PTF

REM
REM Either install or backout
REM
if %PTFaction%=="INSTALL" goto perform_install_ptf
if %PTFaction%=="BACKOUT" goto perform_backout_ptf

:perform_install_ptf
call :install_ptf
if %return_code%==1 goto finished_processing
goto xit

:perform_backout_ptf
call :backout_ptf
if %return_code%==1 goto finished_processing
goto xit

:finished_processing
REM
REM Log PTF Footer information to document work completion
REM
call :log_flowerbox FOOTER "%backup_dir%\%PTFname%.log" PTF
call :log_flowerbox FOOTER "%ptf_master_backup_dir%\PTF_maintenance.log" MASTER
echo.
if %PTFaction%=="INSTALL" goto install_completed_statement
echo PTF %PTFname% has been successfully backed out.
echo.
echo Any original files that were backed up due to the installation of this PTF have
echo been replaced in %PTFinstall_dir%.
goto xit
:install_completed_statement
echo PTF %PTFname% has been successfully applied.
echo.
echo Any original files replaced by this PTF have been moved to the %PTFname%.BAK
echo directory under %PTFinstall_dir%\PTF_Backups.
echo To back out this PTF, run the uninstall.bat file in the directory
echo {install_dir}\PTF_Backups\WKS85001.BAK and select 'Backout'.
goto xit


REM #-----------------------------------------------------------------#
REM # install_ptf                                                     #
REM #                                                                 #
REM # 1) Expand Zip file                                              #
REM # 2) Backup files being replaced                                  #
REM # 3) Install files                                                #
REM # 4) Store file to enable a backout                               #
REM #-----------------------------------------------------------------#
:install_ptf
   REM
   REM Prior to installation, remove any post processing bat file,
   REM so that it will be created if needed.
   REM
   set PTFpreviously_installed=0
   if exist "%backup_dir%\%PTFname%_post_process.bat" del "%backup_dir%\%PTFname%_post_process.bat" > NUL 2>&1
   if exist "%backup_dir%\%PTFname%.ctl" set PTFpreviously_installed=1

   REM
   REM Expand Zip containing PTF files
   REM
   copy %PTFname%A.exe "%zip_expanded_dir%\%PTFname%A.exe" > NUL 2>&1
   del %PTFname%A.exe > NUL 2>&1
   call :unzip_ptf
   if %return_code%==0 goto install_ptf_error


   REM
   REM Backup original copies of files that will be replaced by PTF
   REM
   if %PTFpreviously_installed%==1 echo BACKUP: Backup will not be performed, since this PTF has already been applied. >> "%backup_dir%\%PTFname%.log"
   if %PTFpreviously_installed%==1 goto install_perform_install
   set phase="BACKUP"
   call :process_ctlfile
   if %return_code%==0 goto install_ptf_error

:install_perform_install
   REM
   REM Perform installation of files within PTF
   REM
   set phase="INSTALL"
   call :process_ctlfile
   if %return_code%==0 goto install_ptf_error


   REM
   REM Perform any post-processing steps.
   REM Only perform this activity once; even if reapplying PTF without uninstalling
   REM
   PUSHD .
   if exist "%backup_dir%\%PTFname%_post_process.bat" call "%backup_dir%\%PTFname%_post_process.bat" INSTALL > "%backup_dir%\%PTFname%_post_process.log" 2>&1
   if exist "%backup_dir%\%PTFname%_post_process.bat" set PTF_something_was_installed=1
   if exist "%backup_dir%\%PTFname%_post_process.log" type "%backup_dir%\%PTFname%_post_process.log" >> "%backup_dir%\%PTFname%.log"
   del "%backup_dir%\%PTFname%_post_process.log"  > NUL 2>&1
   if %return_code%==0 goto install_ptf_error
   POPD

:install_store_files
   REM
   REM Store files in backup directory so that backouts can occur
   REM
   copy uninstall.bat "%backup_dir%\uninstall.bat" > NUL 2>&1
   copy %PTFname%.ctl "%backup_dir%\%PTFname%.ctl" > NUL 2>&1
   copy README.txt "%backup_dir%\README.txt" > NUL 2>&1

   REM
   REM Remove files that were expanded from zip file in
   REM temporary location
   REM
   del %PTFname%.ctl > NUL 2>&1
   del %PTFname%A.exe > NUL 2>&1
   del uninstall.bat > NUL 2>&1
   set PTF_batch_file_to_delete=1

   call :clean_directory "%zip_expanded_dir%"
   rmdir %PTfname% > NUL 2>&1

   REM
   REM Work has completed; verify and exit
   REM
   if %PTF_error_occurred%==1 goto install_ptf_end
   if %PTF_something_was_installed%==1 goto install_ptf_end
   echo.
   echo Based on your installation, no files from this PTF were installed.

   goto install_ptf_end
:install_ptf_error
   echo.
   echo An error has occurred during the installation of this PTF.
   echo Please review the log file for additional information.
:install_ptf_end
goto:EOF


REM #-----------------------------------------------------------------#
REM # backout_ptf                                                     #
REM #                                                                 #
REM # 1) Expand Zip file                                              #
REM # 2) Backup files being replaced                                  #
REM # 3) Install files                                                #
REM # 4) Store file to enable a backout                               #
REM #-----------------------------------------------------------------#
:backout_ptf
   set backout_is_successful=1
   set phase="BACKOUT"
   call :process_ctlfile
   if %return_code%==0 goto backout_ptf_error

   REM
   REM Perform any post-processing backout steps
   REM
   PUSHD .
   if exist "%backup_dir%\%PTFname%_post_process.bat" call "%backup_dir%\%PTFname%_post_process.bat" BACKOUT > "%backup_dir%\%PTFname%_post_process.log" 2>&1
   if exist "%backup_dir%\%PTFname%_post_process.log"  type "%backup_dir%\%PTFname%_post_process.log"
   if exist "%backup_dir%\%PTFname%_post_process.log"  type "%backup_dir%\%PTFname%_post_process.log" >> "%backup_dir%\%PTFname%.log"
   del "%backup_dir%\%PTFname%_post_process.log"  > NUL 2>&1
   if %return_code%==0 goto backout_ptf_error

   POPD
   REM
   REM Remove additional files previously stored upon
   REM installation of the PTF.
   REM
   del "%backup_dir%\%PTFname%.ctl" > NUL 2>&1
   del "%backup_dir%\README.txt" > NUL 2>&1
   del "%backup_dir%\%PTFname%_plugins.dat" > NUL 2>&1
   REM
   REM Remove any/all post process batch files
   REM
   dir/b "%backup_dir%\*.bat" > "%backup_dir%\%PTFname%_bat_files.tmp" 2>&1
   for /f "usebackq tokens=*" %%x in ("%backup_dir%\%PTFname%_bat_files.tmp") do (
       if not "%%x"=="uninstall.bat" del "%%x" > NUL 2>&1
   )
   del "%backup_dir%\%PTFname%_bat_files.tmp" > NUL 2>&1
   set PTF_batch_file_to_delete=1

   if exist "%backup_dir%\%PTFname%_newdirs.dat" goto backout_ptf_remove_newdirs
   goto backout_ptf_remove_backup_dirs

:backout_ptf_remove_newdirs
   REM
   REM Remove any empty dirs that were created when the PTF was installed
   REM
   for /f "usebackq delims=#" %%d in ("%backup_dir%\%PTFname%_newdirs.dat") do (
       call :remove_directories "%%d"
       rmdir "%%d" > NUL 2>&1
   )
   del "%backup_dir%\%PTFname%_newdirs.dat" > NUL 2>&1

:backout_ptf_remove_backup_dirs
   REM
   REM Remove empty backup dirs once PTF has been backed out
   REM
   call :remove_directories "%backup_dir%"

   goto backout_ptf_end
:backout_ptf_error
   echo.
   echo An error has occurred during the backout of this PTF.
   echo Please review the log file for additional information.
:backout_ptf_end
goto:EOF


REM #-----------------------------------------------------------------#
REM # validate_directory                                              #
REM #                                                                 #
REM # Validate directory provided                                     #
REM #                                                                 #
REM # Parameter 1: directory to verify                                #
REM #-----------------------------------------------------------------#
:validate_directory
   set return_code=1
   set dir_to_validate=%1
   set dir_to_validate=%dir_to_validate:"=%
   if not exist "%dir_to_validate%\" goto validate_directory_dir_does_not_exist
   goto validate_directory_end

:validate_directory_dir_does_not_exist
   echo.
   echo Directory %dir_to_validate% does not exist.
   set return_code=0

:validate_directory_end
goto:EOF


REM #-----------------------------------------------------------------#
REM # get_install_dir_from_ctlfile                                    #
REM #                                                                 #
REM #    Scan control file for TARGETDIR token, extract directory     #
REM #    scanned directory will be returned.                          #
REM #                                                                 #
REM #    Set PTFinstall_dir if found                                  #
REM #    set PTF_gen_release_variable_found if TARGETDIR=%GEN85%      #
REM #-----------------------------------------------------------------#

:get_install_dir_from_ctlfile
   set return_code=1
REM
REM First, verify that CTL file is available.  If not, then this PTF may
REM have already been applied.  Since we have not yet established the
REM installation directory, we cannot check for the PTF ok file which
REM is used to verify if the installation has already taken place.
REM
   if exist %PTFname%.ctl goto get_install_dir_from_ctlfile_proceed
   echo.
   echo PTF Control file %PTFname%.ctl has not been found.
   echo This PTF may have already been applied.
   set return_code=0
   goto get_install_dir_from_ctlfile_found
REM
:get_install_dir_from_ctlfile_proceed
   %SystemRoot%\system32\find "TARGETDIR" < %PTFname%.ctl > %PTFname%_temp.log
   for /f "usebackq eol=# tokens=1,2 delims==" %%x in ("%PTFname%_temp.log") do (
      call :evaluate_install_dir_from_ctlfile "%%y"
      set path_read=%%y
   )
   del %PTFname%_temp.log > NUL 2>&1
   if not "%PTFinstall_dir%"=="" goto get_install_dir_from_ctlfile_found
   if "%path_read%"=="%PTF_gen_release_variable%" set PTFinstall_dir=%PTF_gen_release_variable%
:get_install_dir_from_ctlfile_found
goto:EOF

:evaluate_install_dir_from_ctlfile
   set PTFinstall_dir=%1
   if not [%PTFinstall_dir%] == [] set PTFinstall_dir=%PTFinstall_dir:"=%
goto:EOF

REM #-----------------------------------------------------------------#
REM # establish_needed_directories                                    #
REM #                                                                 #
REM # For Installs:                                                   #
REM #    Set zip_expanded_dir                                         #
REM #    Set backup_dir                                               #
REM #    Set ptf_master_backup_dir                                    #
REM # For Backouts:                                                   #
REM #    NOTE: Setup is executed from PTF_Backups\%PTFname%.BAK       #
REM #          directory.                                             #
REM #    set install_dir, backup_dir and ptf_master_backup_dir        #
REM #-----------------------------------------------------------------#

:establish_needed_directories
   if %PTFaction%=="BACKOUT" goto establish_needed_restore_directories
   set zip_expanded_dir=%cd%
   set last_char=%zip_expanded_dir:~-1%
   if "%last_char%" == "\" set zip_expanded_dir=%zip_expanded_dir:~0,-1%
   if not exist "%zip_expanded_dir%\%PTFname%" mkdir "%zip_expanded_dir%\%PTFname%"
   set zip_expanded_dir=%zip_expanded_dir%\%PTFname%
   if not exist "%PTFinstall_dir%\PTF_Backups\" mkdir "%PTFinstall_dir%\PTF_Backups"
   set ptf_master_backup_dir=%PTFinstall_dir%\PTF_Backups
   if not exist "%PTFinstall_dir%\PTF_Backups\%PTFname%.BAK\" mkdir "%PTFinstall_dir%\PTF_Backups\%PTFname%.BAK"
   set backup_dir=%PTFinstall_dir%\PTF_Backups\%PTFname%.BAK
   goto establish_needed_directories_end

:establish_needed_restore_directories
   set backup_dir=%cd%
   call :extract_path "%backup_dir%" ptf_master_backup_dir
   call :extract_path "%ptf_master_backup_dir%" PTFinstall_dir

:establish_needed_directories_end
goto:EOF


REM #-----------------------------------------------------------------#
REM # get_install_dir_from_prompt                                     #
REM #                                                                 #
REM #    Prompt user for an installation directory                    #
REM #                                                                 #
REM #    PTFinstall_dir set if provided                               #
REM #-----------------------------------------------------------------#

:get_install_dir_from_prompt
   echo.
   echo Please provide the installation directory to apply this PTF to.
   echo If directory contains spaces, please surround path with quotes.
   echo.
:get_install_dir_from_prompt_loop
   set /p PTFinstall_dir_temp="Install Dir: "
   call set PTFinstall_dir=%PTFinstall_dir_temp%
   if [%PTFinstall_dir_temp%]==[] goto get_install_dir_from_prompt_loop
   if [%PTFinstall_dir%]==[] set PTFinstall_dir=%PTFinstall_dir_temp%
   if not [%PTFinstall_dir%] == [] set PTFinstall_dir=%PTFinstall_dir:"=%
goto:EOF

REM #-----------------------------------------------------------------#
REM # request_action                                                  #
REM #                                                                 #
REM #    Prompt user to either install or backout the PTF             #
REm #    Parameter 1: action selected (returned value)                #
REM #-----------------------------------------------------------------#

:request_action
   set return_code=1
   set %1="INSTALL"
   echo NOTE: Before installation or backout, please ensure that all Gen
   echo       applications and utilities have been stopped.
   echo.
   echo Please select one of the following actions for this PTF maintenance:
   echo.
   if %PTF_process%==uninstall goto request_action_uninstall_menu
   echo (I) Install
   echo (Q) Quit
   echo.
   goto request_action_make_choice_setup
:request_action_uninstall_menu
   echo (B) Backout
   echo (Q) Quit
   echo.
   goto request_action_make_choice_uninstall

:request_action_make_choice_setup
   REM set /p choice_entered=[I,Q]?
   set choice_entered=I
   for %%? in (I i Q q) do if /i "%choice_entered%"=="%%?" goto request_action_validate_choice
   goto request_action_make_choice_setup

:request_action_make_choice_uninstall
   set /p choice_entered=[B,Q]?
   for %%? in (B b Q q) do if /i "%choice_entered%"=="%%?" goto request_action_validate_choice
   goto request_action_make_choice_uninstall

:request_action_validate_choice
   if %choice_entered%==Q goto request_action_choice_quit
   if %choice_entered%==q goto request_action_choice_quit
   if %choice_entered%==B set %1="BACKOUT"
   if %choice_entered%==b set %1="BACKOUT"
   echo.
   goto request_action_end

:request_action_choice_quit
   echo.
   echo Quitting installation
   REM
   REM Remove files that were expanded from zip file before Quit
   REM
   if %PTF_process%==uninstall goto request_action_choice_quit_uninstall
   del %PTFname%.ctl > NUL 2>&1
   del %PTFname%A.exe > NUL 2>&1
   del uninstall.bat > NUL 2>&1
   set PTF_batch_file_to_delete=1
:request_action_choice_quit_uninstall
   set return_code=0

:request_action_end
goto:EOF

REM #-----------------------------------------------------------------#
REM # UnZip PTF                                                       #
REM #                                                                 #
REM #-----------------------------------------------------------------#

:unzip_ptf
   set return_code=1
   PUSHD %zip_expanded_dir%
   %PTFname%A.exe -qq -o
   if not %ERRORLEVEL%==1 goto end_unzip_ptf

   echo.
   echo Error while extracting files, verify you have sufficient disk space.
   echo The PTF has not been installed.
   set return_code=0
:end_unzip_ptf
REM
REM Remove extraneous file that were expanded: these are duplicates
REM
   del %PTFname%.ctl > NUL 2>&1
   del setup.bat > NUL 2>&1
   del README.txt > NUL 2>&1
   POPD
goto:EOF

REM #-----------------------------------------------------------------#
REM # process_ctlfile                                                 #
REM #                                                                 #
REM # This subroutine processes the control file which directs the    #
REM # installation of the maintenance zipfile.                        #
REM #                                                                 #
REM # 1) parse the control information into "command" and "action"    #
REM #    variables. Using these variables call the function requested.#
REM #-----------------------------------------------------------------#
:process_ctlfile
   set return_code=1
   for /f "usebackq eol=# tokens=1,2 delims==" %%x in ("%PTFname%.ctl") do call :process_command_and_action %%x "%%y"
goto:EOF

REM #-----------------------------------------------------------------#
REM # process_command_and_action                                      #
REM #                                                                 #
REM # This subroutine processes each line of the control file         #
REM #                                                                 #
REM # Parameter 1: command to process                                 #
REM # Parameter 2: action to assign                                   #
REM #-----------------------------------------------------------------#
:process_command_and_action
   set cmd=%1
   set action=%2
   set action=%action:"=%

   if "%cmd%"=="PTFIN"   goto process_command_and_action_extract_ptfin
   if "%cmd%"=="PTFOUT"  goto process_command_and_action_extract_ptfout
   if "%cmd%"=="INSTALL" goto process_command_and_action_extract_install
   goto process_command_and_action_end

:process_command_and_action_extract_ptfin
REM
REM Extract subdirectory and file where previous packaged file is to be delivered: "PTFIN="
REM PTFIN is equivalent to Package Output for packaging
REM
   set PTFptfin=%action%
   goto process_command_and_action_end

:process_command_and_action_extract_ptfout
REM
REM Extract subdirectory and file where ptf'd file is to be delivered: "PTFOUT="
REM
   set PTFptfout=%action%
   goto process_command_and_action_end

:process_command_and_action_extract_install
REM
REM Extract type of installation action to perform on file "PTFOUT="
REM
REM Once extracted, perform action based on Phase
REM
   set PTFinstall_action=%action: =%
   if %phase%=="BACKOUT" call :backout_file
   if %phase%=="INSTALL" call :install_file
   if %phase%=="BACKUP"  call :backup_file

:process_command_and_action_end
goto:EOF

REM #-----------------------------------------------------------------#
REM # backout_file                                                    #
REM #                                                                 #
REM # Restore individual file called out from the ctl file            #
REM #-----------------------------------------------------------------#

:backout_file
REM
REM First, see if file contains %versionno%.   If found, file is a plugin
REM
   if "%PTFinstall_action%"=="POST" goto backout_file_done
   if "%PTFinstall_action%"=="NOOP" goto backout_file_done
   call :search_for_substring %%versionno%% "%PTFptfout%" substring_found
   if %substring_found% == 1 call :backout_file_plugin
   if %substring_found% == 1 goto backout_file_done

REM
REM Perform the following:
REM 1) If removing newly installed file, remove and exit
REM 2) If backup copy does not exist, original file was not available to be
REM    backed up originally (was not part of initial product installation)
REM 3) Restore backup copy back into installation directory
REM 4) Remove backed up copy of file
REM
   if "%PTfinstall_action%"=="NEW" goto backout_file_delete_new_file
   if "%PTfinstall_action%"=="FORCED_COPY" goto backout_file_delete_forced_copy
   if not exist "%backup_dir%\%PTFptfout%" goto backout_file_nothing_to_restore
   goto backout_file_restore_file

:backout_file_delete_forced_copy
   if not exist "%backup_dir%\%PTFptfout%" goto backout_file_delete_new_file
   goto backout_file_restore_file

:backout_file_delete_new_file
   del "%PTFinstall_dir%\%PTFptfout%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto backout_file_delete_succeeded_new_file
   goto backout_file_delete_new_failed

:backout_file_restore_file
   copy "%backup_dir%\%PTFptfout%" "%PTFinstall_dir%\%PTFptfout%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto backout_file_delete_backup_copy
   goto backout_file_restore_failed

:backout_file_delete_backup_copy
   del "%backup_dir%\%PTFptfout%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto backout_file_delete_succeeded
   goto backout_file_delete_backup_copy_failed

REM
REM Backout messages
REM

:backout_file_delete_new_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** BACKOUT: Removal of newly installed file %PTFinstall_dir%\%PTFptfout% failed >> "%backup_dir%\%PTFname%.log"
   goto backout_file_failed

:backout_file_delete_backup_copy_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** BACKOUT: Removal of file %backup_dir%\%PTFptfout% failed >> "%backup_dir%\%PTFname%.log"
   goto backout_file_failed

:backout_file_restore_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** BACKOUT: Restoring %backup_dir%\%PTFptfout% failed >> "%backup_dir%\%PTFname%.log"
   goto backout_file_failed

:backout_file_nothing_to_restore
   echo BACKOUT: Restore of %PTFinstall_dir%\%PTFptfout% was not performed.  File did not exist and was not previously backed up >> "%backup_dir%\%PTFname%.log"
   goto backout_file_done

:backout_file_delete_succeeded
   if "%PTFinstall_action%"=="NEW" goto backout_file_succeeded_new_file
   echo BACKOUT: Restore of %PTFinstall_dir%\%PTFptfout% was successful >> "%backup_dir%\%PTFname%.log"
   goto backout_file_done

:backout_file_delete_succeeded_new_file
   echo BACKOUT: Removal of newly installed file %PTFinstall_dir%\%PTFptfout% was successful >> "%backup_dir%\%PTFname%.log"
   goto backout_file_done

:backout_file_failed
   echo.
   echo An error occured in the backout process. Please check the file %backup_dir%\%PTFname%.log for the problem.
   set PTF_error_occurred=1
   set return_code=0

:backout_file_done
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1  
   if "%substring_found%" == "0" echo/|set/p=.
goto:EOF

REM #-----------------------------------------------------------------#
REM # backout_file_plugin                                             #
REM #                                                                 #
REM # Restore individual file called out from the ctl file            #
REM #                                                                 #
REM # Special Case for plugins containing build numbers:              #
REM #                                                                 #
REM # Since the version number changes between the originally         #
REM # delivered plugin and the one being delivered with the PTF, a    #
REM # special tag has been provided in the ctl file to represent the  #
REM # location of the version number.  This tag, %versionno%, will be #
REM # searched for in the filenames within the ctl file.  If found,   #
REM # special logic has to be executed to determine the actual build  #
REM # number of the original file and the file being delivered, so    #
REM # only the plugin installed by this PTF will be removed. The      #
REM # original filename needed to be retained so that the restore     #
REM # will be successful.                                             #
REM #-----------------------------------------------------------------#

:backout_file_plugin
REM
REM To properly backout the plugin, perform the following:
REM 1) find actual plugin filename that was previously applied
REM 2) delete this actual plugin, leaving the previous version of the plugin
REM
   call :collect_plugin_filename_from_file "%PTFptfout%" actual_plugin_filename plugin_filename_found
   if %plugin_filename_found% == 0 goto backout_file_plugin_not_found
   set target_file=%actual_plugin_filename%

   del "%PTFinstall_dir%\%target_file%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto backout_file_plugin_succeeded
   goto backout_file_plugin_delete_failed

REM
REM Backout messages
REM

:backout_file_plugin_delete_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** BACKOUT: Removal of file %PTFinstall_dir%\%target_file% failed >> "%backup_dir%\%PTFname%.log"
   goto backout_file_plugin_failed

:backout_file_plugin_not_found
   set versionno=*
   call set PTFptfout=%PTFptfout%
   echo BACKOUT: Removal of %PTFinstall_dir%\%PTFptfout% (wildcarded for a plugin) was not performed.  File did not exist and was not installed >> "%backup_dir%\%PTFname%.log"
   goto backout_file_plugin_done

:backout_file_plugin_succeeded
   echo BACKOUT: Removal of newly installed plugin %PTFinstall_dir%\%target_file% was successful >> "%backup_dir%\%PTFname%.log"
   goto backout_file_plugin_done

:backout_file_plugin_failed
   echo.
   echo An error occured in the backout process. Please check the file %backup_dir%\%PTFname%.log for the problem.
   set PTF_error_occurred=1
   set return_code=0

:backout_file_plugin_done
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1
   echo/|set/p=.
goto:EOF

REM #-----------------------------------------------------------------#
REM # install_file                                                    #
REM #                                                                 #
REM # Install individual file called out from the ctl file            #
REM # Actions to perform for files are:                               #
REM #                                                                 #
REM # COPY:        Copy file from PTF to Gen directory, superceeding  #
REM #              existing version of file                           #
REM # FORCED_COPY: Copy file from PTF to Gen directory, regardless of #
REM #              whether version currently exists in Gen directory  #
REM # NEW:         Copy new file from PTF to Gen directory            #
REM # POST:        Execute post-processing procedure provided         #
REM # NOOP:        No action is performed on this file.  Primarily    #
REM #              used as data for post-processing.                  #
REM #-----------------------------------------------------------------#

:install_file
REM
REM If the install action is POST, store the post process and skip any install
REM activities.
REM
REM When storing the post process, the post process procedure is written to a
REM BAT file that will be executed after the installation process has completed.
REM The post process procedure will contain lines of the following format:
REM
REM call "%zip_expanded_dir%\%PTFptfin%" "%zip_expanded_dir%" "PTFinstall_dir% >> %PTFname%_post_process.bat
REM
REM As you can see, the call to the provided post process procedure is
REM accompanied by 2 parameters:
REM
REM    The zip expanded directory used to find additional files used by the
REM        post process procedure
REM    The install directory used for applying files via the post process
REM        procedure
REM
REM An additional flag, INSTALL or BACKOUT will be passed to the top-level bat
REM file that will, in turn, execute the post process procedure. This will
REM become the 3rd parameter to the post process procedure, and will be used
REm to indicate when the post process procedure is being invoked.  This is
REM necessary to allow both install and backout features of the post process
REM procedure.
REM
REM Additional files that are utilized by the post process procedure are
REM supplied with the PTF (via the INSTALL=NOOP keyword=value pair).
REM
REM Also note that you can have more than 1 post process procedure for a PTF.
REM
REM Only perform this post process file.
REM
   if "%PTFinstall_action%"=="NOOP" goto install_file_end
   if "%PTFinstall_action%"=="POST" goto install_file_build_post_file
   goto install_file_begin
:install_file_build_post_file
   if not exist "%backup_dir%\%PTFname%_post_process.bat" echo set flag=%%1 > "%backup_dir%\%PTFname%_post_process.bat"
   copy "%zip_expanded_dir%\%PTFptfin%" "%backup_dir%\%PTFptfin%" > NUL 2>&1
   echo call  "%backup_dir%\%PTFptfin%"  "%zip_expanded_dir%"  "%PTFinstall_dir%"  %%flag%% >> "%backup_dir%\%PTFname%_post_process.bat"
   goto install_file_end

:install_file_begin
REM
REM First, see if file contains %versionno%.   If found, file is a plugin
REM
   call :search_for_substring %%versionno%% "%PTFptfout%" substring_found
   if %substring_found% == 1 call :install_file_plugin
   if %substring_found% == 1 goto install_file_end

REM
REM Either Copy, Force Copy or provide as new the file
REM
REM COPY:        Verify installed file exists before copying
REM FORCED COPY: just copy regardless of whether installed file exists
REM NEW:         Create new directory (if needed) before copying
REM
   if "%PTFinstall_action%"=="COPY" goto install_file_try_copy
   if "%PTFinstall_action%"=="FORCED_COPY" goto install_file_try_new
   if "%PTFinstall_action%"=="NEW" goto install_file_try_new
   goto install_file_invalid_action

:install_file_try_copy
REM
REM Check to see if file from install zip exists in installation directory
REM
   if not exist "%PTFinstall_dir%\%PTFptfout%" goto install_file_does_not_exist
   goto install_file_copy

:install_file_try_new
REM
REM Create new directory if needed
REM
   call :create_subdirectory "%PTFinstall_dir%\%PTFptfout%" NEW

:install_file_copy
REM
REM Copy file from PTF package into installation directory
REM
   copy /y "%zip_expanded_dir%\%PTFptfin%" "%PTFinstall_dir%\%PTFptfout%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto install_file_succeeded
   goto install_file_copy_failed

REM
REM Install messages
REM

:install_file_invalid_action
   echo ***ERROR*** INSTALL: Incorrect install action in control file on file %PTFptfout% >> "%backup_dir%\%PTFname%.log"
   goto install_file_failed

:install_file_copy_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** INSTALL: Copy of file %zip_expanded_dir%\%PTFptfin% failed >> "%backup_dir%\%PTFname%.log"
   goto install_file_failed

:install_file_does_not_exist
   echo INSTALL: File %PTFinstall_dir%\%PTFptfout% does not exist and was not installed >> "%backup_dir%\%PTFname%.log"
   goto install_file_end

:install_file_succeeded
   echo INSTALL: Installation of %PTFinstall_dir%\%PTFptfout% was successful >> "%backup_dir%\%PTFname%.log"
   set PTF_something_was_installed=1
   goto install_file_end

:install_file_failed
   echo.
   echo An error occured in the install process. Please check the file %backup_dir%\%PTFname%.log for the problem.
   set PTF_error_occurred=1
   set return_code=0

:install_file_end
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1
   if "%substring_found%" == "0" echo/|set/p=.
goto:EOF

REM #-----------------------------------------------------------------#
REM # install_file_plugin                                             #
REM #                                                                 #
REM # Install individual file called out from the ctl file            #
REM # Actions to perform for files are:                               #
REM #                                                                 #
REM # COPY:        Copy file from PTF to Gen directory, superceeding  #
REM #              existing version of file                           #
REM # FORCED_COPY: Copy file from PTF to Gen directory, regardless of #
REM #              whether version currently exists in Gen directory  #
REM # NEW:         Copy new file from PTF to Gen directory            #
REM #                                                                 #
REM # Special Case for plugins containing build numbers:              #
REM #                                                                 #
REM # Since the version number changes between the originally         #
REM # delivered plugin and the one being delivered with the PTF, a    #
REM # special tag has been provided in the ctl file to represent the  #
REM # location of the version number.  This tag, %versionno%, will be #
REM # searched for in the filenames within the ctl file.  If found,   #
REM # special logic has to be executed to determine the actual        #
REM # version number of the original file and the file being          #
REM # delivered, so that both files can exist in the installation     #
REM # directory; the reason being that a customer may have created    #
REM # a utility that requires the original plugin.                    #
REM #-----------------------------------------------------------------#

:install_file_plugin
REM
REM To properly install a plugin, perform the following:
REM 1) find actual plugin filename being installed
REM 2) Copy, Forced Copy, or copy as new plugin to install directory
REM
   set filename_version=1
   call :collect_plugin_filename "%zip_expanded_dir%" "%PTFptfin%" %filename_version% actual_plugin_filename plugin_filename_found plugin_filename_count
   call :extract_path "%PTFptfin%" extracted_path
   set source_file=%extracted_path%\%actual_plugin_filename%
   set target_file=%extracted_path%\%actual_plugin_filename%

REM
REM Either Copy, Force Copy or provide as new the file
REM
REM COPY:        Verify installed file exists before copying
REM FORCED COPY: just copy regardless of whether installed file exists
REM NEW:         Create new directory (if needed) before copying
REM
   if "%PTFinstall_action%"=="COPY" goto install_file_plugin_try_copy
   if "%PTFinstall_action%"=="FORCED_COPY" goto install_file_plugin_copy
   if "%PTFinstall_action%"=="NEW" goto install_file_plugin_try_new
   goto install_file_plugin_invalid_action

:install_file_plugin_try_copy
REM
REM Check to see if file from install zip exists in installation directory
REM
   set filename_version=1
   call :collect_plugin_filename "%PTFinstall_dir%" "%PTFptfout%" %filename_version% actual_plugin_filename plugin_filename_found plugin_filename_count
   if %plugin_filename_found% == 0 goto install_file_plugin_does_not_exist
   goto install_file_plugin_copy

:install_file_plugin_try_new
REM
REM Create new directory if needed
REM
   call :create_subdirectory "%PTFinstall_dir%\%target_file%" NEW

:install_file_plugin_copy
REM
REM Copy file from PTF package into installation directory
REM
   copy /y "%zip_expanded_dir%\%source_file%" "%PTFinstall_dir%\%target_file%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto install_file_plugin_succeeded
   goto install_file_plugin_copy_failed

REM
REM Install messages
REM

:install_file_plugin_invalid_action
   echo ***ERROR*** INSTALL: Incorrect install action in control file on file %target_file% >> "%backup_dir%\%PTFname%.log"
   goto install_file_plugin_failed

:install_file_plugin_copy_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** INSTALL: Copy of file %zip_expanded_dir%\%source_file% failed >> "%backup_dir%\%PTFname%.log"
   goto install_file_plugin_failed

:install_file_plugin_does_not_exist
   set versionno=*
   call set target_file=%PTFptfout%
   echo INSTALL: File %PTFinstall_dir%\%target_file% (a plugin) does not exist and was not installed >> "%backup_dir%\%PTFname%.log"
   set versionno=%%versionno%%
   goto install_file_plugin_end

:install_file_plugin_succeeded
   echo INSTALL: Installation of %PTFinstall_dir%\%target_file% (a plugin) was successful >> "%backup_dir%\%PTFname%.log"
   set PTF_something_was_installed=1
REM
REM Log plugin name into file that will be used for during a backout
REM
REM Only perform this activity once; even if reapplying PTF without uninstalling
REM
   if exist "%backup_dir%\%PTFname%.ctl" goto install_file_plugin_end
   echo %target_file% >> "%backup_dir%\%PTFname%_plugins.dat"
   goto install_file_plugin_end

:install_file_plugin_failed
   echo.
   echo An error occured in the install process. Please check the file %backup_dir%\%PTFname%.log for the problem.
   set PTF_error_occurred=1
   set return_code=0

:install_file_plugin_end
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1
   echo/|set/p=.
goto:EOF

REM #-----------------------------------------------------------------#
REM # backup_file                                                     #
REM #                                                                 #
REM # Save original copy of file called out from the ctl file before  #
REM # installing new version.                                         #
REM #-----------------------------------------------------------------#

:backup_file
REM
REM First, see if file contains %versionno%.  If so, skip backup.
REM
   if "%PTFinstall_action%"=="POST" goto backup_file_end
   if "%PTFinstall_action%"=="NOOP" goto backup_file_end
   call :search_for_substring %%versionno%% "%PTFptfout%" substring_found
   if %substring_found% == 1 call :backup_file_plugin
   if %substring_found% == 1 goto backup_file_end

   if "%PTFinstall_action%"=="NEW" goto backup_file_new_no_backup

REM
REM If file is not present, do not back it up
REM
   if not exist "%PTFinstall_dir%\%PTFptfout%" goto backup_file_notfound_no_backup

REM
REM Create subdirectory under backup directory if it does not exist
REM
   call :create_subdirectory "%backup_dir%\%PTFptfout%" BACKUP

REM
REM Backup file.  If already backed up, don't do it again!
REM
   if exist "%backup_dir%\%PTFptfout%" goto backup_file_already_exists
   copy /y "%PTFinstall_dir%\%PTFptfout%" "%backup_dir%\%PTFptfout%" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if %ERRORLEVEL%==0 goto backup_file_succeeded
   goto backup_file_backup_failed

REM
REM Backup messages
REM

:backup_file_notfound_no_backup
   echo BACKUP: File %PTFinstall_dir%\%PTFptfout% does not exist and was not backed up. >> "%backup_dir%\%PTFname%.log"
   goto backup_file_end

:backup_file_new_no_backup
   echo BACKUP: File %PTFinstall_dir%\%PTFptfout% is a NEW file and will not be backed up. >> "%backup_dir%\%PTFname%.log"
   goto backup_file_end

:backup_file_backup_failed
   type "%backup_dir%\%PTFname%_temp.log" >> "%backup_dir%\%PTFname%.log"
   echo ***ERROR*** BACKUP: Backup of file %PTFinstall_dir\%PTFptfout% failed >> "%backup_dir%\%PTFname%.log"
   goto backup_file_failed

:backup_file_succeeded
   echo BACKUP: Saving %PTFinstall_dir%\%PTFptfout% to %backup_dir%\%PTFptfout% was successful. >> "%backup_dir%\%PTFname%.log"
   goto backup_file_end

:backup_file_already_exists
   echo BACKUP: File %PTFinstall_dir%\%PTFptfout% has already been backed up and will not be backed up again. >> "%backup_dir%\%PTFname%.log"
   goto backup_file_end

:backout_file_failed
   echo.
   echo An error occured in the backup process. Please check the file %backup_dir%\%PTFname%.log for the problem.
   set PTF_error_occurred=1
   set return_code=0

:backup_file_end
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1
   if "%substring_found%" == "0" echo/|set/p=.
goto:EOF


REM #-----------------------------------------------------------------#
REM # backup_file_plugin                                              #
REM #                                                                 #
REM # Save original copy of file called out from the ctl file before  #
REM # installing new version.                                         #
REM #                                                                 #
REM # Special Case for plugins containing version numbers:            #
REM #                                                                 #
REM # Since the version number changes between the originally         #
REM # delivered plugin and the one being delivered with the PTF, a    #
REM # special tag has been provided in the ctl file to represent the  #
REM # location of the version number.  This tag, %versionno%, will be #
REM # searched for in the filenames within the ctl file.  If found,   #
REM # special logic has to be executed to ensure that the original    #
REM # file is NOT backed up; the reason being that a customer may     #
REM # have created a utility that requires the original plugin. In    #
REM # this case, both the original and new file must be stored in the #
REM # installation directory.                                         #
REM #-----------------------------------------------------------------#

:backup_file_plugin
   set filename_version = 0
REM
REM For plugin, skip backup
REM
   if "%PTFinstall_action%"=="NEW" goto backup_file_plugin_new_no_backup
   goto backup_file_plugin_no_backup

REM
REM plugin is new - do not attempt backup
REM

:backup_file_plugin_new_no_backup
   set filename_version = 1
   call :collect_plugin_filename "%zip_expanded_dir%" "%PTFptfout%" %filename_version% actual_plugin_filename plugin_filename_found plugin_filename_count
   call :extract_path "%PTFptfout%" extracted_path
   echo BACKUP: File %PTFinstall_dir%\%extracted_path%\%actual_plugin_filename% (a plugin) is a NEW file and will not be backed up. >> "%backup_dir%\%PTFname%.log"
   goto backup_file_plugin_end

REM
REM plugin exists, but do not backup.  Since there may be one or more versions of a plugin already installed,
REM search for any/all matches.
REM

:backup_file_plugin_no_backup
   set /A filename_version+=1
   call :collect_plugin_filename "%PTFinstall_dir%" "%PTFptfout%" %filename_version% actual_plugin_filename plugin_filename_found plugin_filename_count
   if %plugin_filename_found% == 0 goto backup_file_plugin_end
   call :extract_path "%PTFptfout%" extracted_path
   echo BACKUP: File %PTFinstall_dir%\%extracted_path%\%actual_plugin_filename% is a plugin and will not be backed up. >> "%backup_dir%\%PTFname%.log"
   if %plugin_filename_count% gtr %filename_version% goto backup_file_plugin_no_backup

:backup_file_plugin_end
   echo/|set/p=.
goto:EOF


REM #-----------------------------------------------------------------#
REM # remove_directories                                              #
REM #                                                                 #
REM # Remove empty directories which were the result of a PTF install #
REM # or backout                                                      #
REM #                                                                 #
REM # Parameter 1: Directory to delete (including any subdirectories) #
REM #-----------------------------------------------------------------#

:remove_directories
REM
REM Split dir and sort into 2 steps since piping (|) has problems
REM
   dir/ad/b/s %1 > "%PTFname%_dir_list1.txt" 2>&1
   if %ERRORLEVEL% == 1 goto remove_directories_done
   %SystemRoot%\system32\sort /r > "%PTFname%_dir_list2.txt" < "%PTFname%_dir_list1.txt"
   for /f "usebackq tokens=* delims= " %%d in ("%PTFname%_dir_list2.txt") do rmdir "%%d" > NUL 2>&1
   del "%PTFname%_dir_list1.txt" > NUL 2>&1
   del "%PTFname%_dir_list2.txt" > NUL 2>&1
:remove_directories_done
goto:EOF


REM #-----------------------------------------------------------------#
REM # search_for_substring                                            #
REM #                                                                 #
REM # checks for existence of substring in string                     #
REM # Parm 1 is substring                                             #
REM # Parm 2 is string to search                                      #
REM # Parm 3 is substring found indicator (returned)                  #
REM #-----------------------------------------------------------------#

:search_for_substring
   set %3=0
   set substring=%1
   set string=%2
   set string=%string:"=%
   echo %string% > "%backup_dir%\%PTFname%_searchstring.txt"
   %SystemRoot%\system32\find "%substring%" < "%backup_dir%\%PTFname%_searchstring.txt" > NUL 2>&1
   if %ERRORLEVEL% == 0 set %3=1
   del "%backup_dir%\%PTFname%_searchstring.txt" > NUL 2>&1
goto:EOF

REM #-----------------------------------------------------------------#
REM # collect_plugin_filename                                         #
REM #                                                                 #
REM # Collect actual plugin filename                                  #
REM # Parm 1: path contain file to check                              #
REM # Parm 2: plugin file containing %versionno% and subdirectory     #
REM #         (%PTFptfout%)                                           #
REM # Parm 3: version count identifier, indicating which version of   #
REM #         the filename to collect (when multiple versions exist)  #
REM # Parm 4: actual plugin filename (returned)                       #
REM # Parm 5: plugin filename found indicator (returned)              #
REM # Parm 6: version count, indicating how many versions of the      #
REM #         filename where found (returned)                         #
REM #-----------------------------------------------------------------#

:collect_plugin_filename
   set %5=0
   set versionno=*
   set plugin_dir=%1
   set plugin_dir=%plugin_dir:"=%
   call set plugin_filename=%2
   set plugin_filename=%plugin_filename:"=%
   set version_count_identifier=%3

REM
REM Collect actual plugin filename
REM
   dir/b "%plugin_dir%\%plugin_filename%" > "%backup_dir%\%PTFname%_actual_plugin_filename.txt" 2>&1
   if %ERRORLEVEL% == 1 goto collect_plugin_filename_done
   set counter=0
   for /f "usebackq tokens=*" %%x in ("%backup_dir%\%PTFname%_actual_plugin_filename.txt") do (
      call :collect_specified_plugin_filename %%x
   )
   set %5=1
   set %6=%counter%
   set %4=%temp_plugin_filename%

:collect_plugin_filename_done
   set versionno=%%versionno%%
   del "%backup_dir%\%PTFname%_actual_plugin_filename.txt" > NUL 2>&1
goto:EOF

:collect_specified_plugin_filename
   set /a counter+=1
   if %version_count_identifier% == %counter% set temp_plugin_filename=%1
goto:EOF

REM #-----------------------------------------------------------------#
REM # collect_plugin_filename_from_file                               #
REM #                                                                 #
REM # Collect actual plugin filename from file containing a list of   #
REM # plugins that were installed with a given PTF.                   #
REM #                                                                 #
REM # Parm 1: plugin file containing %versionno% and subdirectory     #
REM #         (%PTFptfout%)                                           #
REM # Parm 2: actual plugin filename (returned)                       #
REM # Parm 3: plugin filename found indicator (returned)              #
REM #-----------------------------------------------------------------#

:collect_plugin_filename_from_file
REM
REM If list of plugins file does not exist (none were installed),
REM set 'not found' flag and return
REM
   set %3=0
   if not exist "%backup_dir%\%PTFname%_plugins.dat" goto collect_plugin_filename_done

   set versionno=*
   call set plugin_filename=%1
   set plugin_filename=%plugin_filename:"=%

REM
REM At this point, plugin_filename looks like "plugin-name.*.jar"
REM Need to remove "*.jar" so that the find will match the actual
REm plugin name stored in the file
REM
   call :extract_from_plugin "%plugin_filename%" extracted_plugin

REM
REM Collect actual plugin filename
REM
   %SystemRoot%\system32\find "%extracted_plugin%" < "%backup_dir%\%PTFname%_plugins.dat" > "%backup_dir%\%PTFname%_temp.log" 2>&1
   if not exist "%backup_dir%\%PTFname%_temp.log" goto collect_plugin_filename_done
   if %ERRORLEVEL% == 1 goto collect_plugin_filename_done
   set/p temp_plugin_name=< "%backup_dir%\%PTFname%_temp.log"
   del "%backup_dir%\%PTFname%_temp.log" > NUL 2>&1

REM
REM Remove trailing space in plugin name (appended from file extraction)
REM
   set temp_plugin_name=%temp_plugin_name%##
   set temp_plugin_name=%temp_plugin_name: ##=%
   set %2=%temp_plugin_name%
   set %3=1

:collect_plugin_filename_done
   set versionno=%%versionno%%
goto:EOF

REM #-----------------------------------------------------------------#
REM # log_flowerbox                                                   #
REM #                                                                 #
REM # Log Header and Footer information                               #
REM #                                                                 #
REm # Parameter 1: 'HEADER' or 'FOOTER'                               #
REM # Parameter 2: Log file name (can be PTF log file or Master PTF   #
REM #              log file                                           #
REM # Parameter 3: 'PTF' or 'MASTER'                                  #
REM #-----------------------------------------------------------------#

:log_flowerbox
REM
REM Obtain timestamp
REM
   time /t > "%PTFname%_time_temp.out"
   for /f "usebackq tokens=1 delims={" %%a in ("%PTFname%_time_temp.out") do set temp_time=%%a
   del "%PTFname%_time_temp.out" > NUL 2>&1
   date /t > "%PTFname%_date_temp.out"
   for /f "usebackq tokens=1 delims={" %%a in ("%PTFname%_date_temp.out") do set temp_date=%%a
   del "%PTFname%_date_temp.out" > NUL 2>&1
REM
REM Log info
REM
   if %3==MASTER goto log_master_flowerbox
   echo --------------------------------------------------- >> %2
   echo %1 MAINTENANCE REPORT for %PTFname% >> %2
   echo DATE: %temp_date% %temp_time% >> %2
   echo PLATFORM: Windows >> %2
   if %PTFaction%=="BACKOUT" echo ACTION: BACKOUT >> %2
   if %PTFaction%=="INSTALL" echo ACTION: INSTALL >> %2
   echo --------------------------------------------------- >> %2
   if %1==HEADER goto log_flowerbox_complete
   echo =================================================== >> %2
   echo =================================================== >> %2
   goto log_flowerbox_complete

:log_master_flowerbox
REM
REM Log info to Master Log only
REM
   echo -------------------------------------------------------- >> %2
   if %PTFaction%=="BACKOUT" echo %PTFname% has been backed out on %temp_date% %temp_time% >> %2
   if %PTFaction%=="INSTALL" echo %PTFname% has been installed on %temp_date% %temp_time% >> %2
   echo -------------------------------------------------------- >> %2

:log_flowerbox_complete
goto:EOF

REM #-----------------------------------------------------------------#
REM # clean_directory                                                 #
REM #                                                                 #
REM # Remove any files and subdiretories within a given directory     #
REM #-----------------------------------------------------------------#

:clean_directory
   set dir_to_clean=%1
   set dir_to_clean=%dir_to_clean:"=%
REM
REM Remove all files in the directory structure
REM Split dir and sort into 2 steps since piping (|) has problems
REM
   dir/a-d/b/s "%dir_to_clean%" > dir_to_clean_filelist1.txt
   %SystemRoot%\system32\sort /r > dir_to_clean_filelist2.txt < dir_to_clean_filelist1.txt
   for /f "usebackq tokens=* delims= " %%d in ("dir_to_clean_filelist2.txt") do del "%%d" > NUL 2>&1
   del dir_to_clean_filelist1.txt > NUL 2>&1
   del dir_to_clean_filelist2.txt > NUL 2>&1
REM
REM Remove all (now) empty subdirectories in the directory structure
REM Split dir and sort into 2 steps since piping (|) has problems
REM
   dir/ad/b/s "%dir_to_clean%" > dir_to_clean_dirlist1.txt
   %SystemRoot%\system32\sort /r > dir_to_clean_dirlist2.txt < dir_to_clean_dirlist1.txt
   for /f "usebackq tokens=* delims= " %%d in ("dir_to_clean_dirlist2.txt") do rmdir "%%d" > NUL 2>&1
   del dir_to_clean_dirlist1.txt > NUL 2>&1
   del dir_to_clean_dirlist2.txt > NUL 2>&1

goto:EOF

REM #-----------------------------------------------------------------#
REM # extract_filename                                                #
REM #                                                                 #
REM # Given a path containing a filename, extract the filename        #
REM #                                                                 #
REM # Parameter 1: path containing filename                           #
REM #-----------------------------------------------------------------#

:extract_filename
   set pathname=%1
   set pathname=%pathname:"=%
   set extracted_filename=

:extract_filename_remove_path
   if "%pathname:~-1%"=="\" goto extract_filename_complete
   set extracted_filename=%pathname:~-1%%extracted_filename%
   set pathname=%pathname:~0,-1%
   goto extract_filename_remove_path

:extract_filename_complete
goto:EOF

REM #-----------------------------------------------------------------#
REM # extract_path                                                    #
REM #                                                                 #
REM # Given a path containing a filename, extract the path            #
REM #                                                                 #
REM # Parameter 1: path containing filename                           #
REM # Parameter 2: extracted path (returned)                          #
REM #-----------------------------------------------------------------#

:extract_path
   set pathname=%1
   set pathname=%pathname:"=%

:extract_path_remove_trailing_filename
   if "%pathname:~-1%"=="\" goto extract_path_trailing_delim_found
   set pathname=%pathname:~0,-1%
   goto extract_path_remove_trailing_filename

:extract_path_trailing_delim_found
   set %2=%pathname:~0,-1%
goto:EOF

REM #-----------------------------------------------------------------#
REM # extract_from_plugin                                             #
REM #                                                                 #
REM # Given path containing a plugin, extract the portion before the  #
REM # wildcard ('*').                                                 #
REM #                                                                 #
REM # Parameter 1: path containing plugin                             #
REM # Parameter 2: extracted plugin (returned)                        #
REM #-----------------------------------------------------------------#

:extract_from_plugin
   set pathname=%1
   set pathname=%pathname:"=%

:extract_from_plugin_remove_trailing_wildcard
   if "%pathname:~-1%"=="*" goto extract_from_plugin_trailing_wildcard_found
   set pathname=%pathname:~0,-1%
   goto extract_from_plugin_remove_trailing_wildcard

:extract_from_plugin_trailing_wildcard_found
   set %2=%pathname:~0,-1%
goto:EOF

REM #-----------------------------------------------------------------#
REM # create_subdirectory                                             #
REM #                                                                 #
REM # Create subdirectory referenced in pathname if it does not exist #
REM # Pathname will be fully qualified and contain a trailing file:   #
REM # ie. d:\directory\subdirectory\filename                          #
REM #                                                                 #
REM # Parameter 1: pathname                                           #
REM # Parameter 2: Directory Flag ('NEW' or 'BACKUP').  If 'NEW',     #
REM #              store directory name.                              #
REM #-----------------------------------------------------------------#

:create_subdirectory
   set pathname=%1
   set pathname=%pathname:"=%

:create_subdirectory_remove_trailing_filename
   if "%pathname:~-1%"=="\" goto create_subdirectory_trailing_delim_found
   set pathname=%pathname:~0,-1%
   goto create_subdirectory_remove_trailing_filename

:create_subdirectory_trailing_delim_found
   set pathname=%pathname:~0,-1%
   if exist "%pathname%\" goto create_subdirectory_complete
   mkdir "%pathname%"
   if %2==NEW echo %pathname%>> "%backup_dir%\%PTFname%_newdirs.dat"

:create_subdirectory_complete
goto:EOF

:display_install_flowerbox
   set choice=install
   if %PTF_process%==uninstall set choice=backout
   echo.
   echo ______________________________________________________________________________
   echo.
   echo                 CA Gen Windows PTF procedure for %PTFname%
   echo.
   echo This procedure will %choice% PTF %PTFname%.
   if %PTF_process%==uninstall goto display_install_flowerbox_backout_info
   echo.
   echo An Install will copy and/or provide new files that have been delivered in
   echo this PTF. Original copies of these files will be backed up under a matching
   echo PTF subdirectory under the PTF_Backups directory.
   goto display_install_flowerbox_logfile_info
:display_install_flowerbox_backout_info
   echo.
   echo A Backout will restore the set of files that have been backed up by this PTF
   echo when it was applied.
:display_install_flowerbox_logfile_info
   echo.
   echo A log file will exist in the PTF subdirectory under the PTF_Backups directory
   echo that will detail the activities that occurred as a result of the %choice%
   echo of the PTF.  Also note that a master log file will reside in the PTF_Backups
   echo directory that list a summary of all PTF activity.
   echo.
   echo ______________________________________________________________________________
   echo.
goto:EOF

:xit
if %PTF_error_occurred%==1 start %WINDIR%\system32\notepad %backup_dir%\%PTFname%.log
if %PTF_batch_file_to_delete%==1 goto self_delete
ENDLOCAL
@echo OFF
goto end
:self_delete
REM pause > nul | echo press any key to exit
if %PTF_process%==uninstall goto self_delete_uninstall
ENDLOCAL
@echo OFF
start /min cmd /c del /q setup.bat
goto end
:self_delete_uninstall
ENDLOCAL
@echo OFF
start /min cmd /c del /q uninstall.bat
:end

0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Answers

0
That means WKS85001A.exe file is not copied to the expected location (from where it has to be executed)
Remove @echo OFF
Use Pause command and find where is the mistake..

Answered 08/14/2015 by: jagadeish
Red Belt

Please log in to comment
0
Sorry, I lost interest in trying to figure out the code as it's so long.
Perhaps if you were to indicate exactly where in the script the code is going wrong, it would help, as would line numbers for that matter !!

Chances are that when running under the localsystem account, something that is present with a normal user account is missing for the system account. Do bear in mind that the system path and environment settings are a combination of the system and user paths and the system and user environment variables. If you are relying on any **user** values, they will not be available to the localsystem account.

Why not add some debugging information which writes values, as they are obtained, to a text file,  eg by adding: ECHO  blah  > c:\temp\logfile.txt  
Make sure the C:\temp folder exists.

Finally, do bear in mind that on a 64 bit operating system the file locations may be different to a 32 bit operating system.
Answered 08/14/2015 by: EdT
Red Belt

Please log in to comment
0
I agree on the logging front. For a script as complex as this, not having a log is asking for trouble.

As for the complexity, why anyone would even *start* something like this in batch is beyond me.

Lastly, @OP, please apply the 'Code' style to your post. Scrolling it at the moment is beyond bearable.
Answered 08/17/2015 by: VBScab
Red Belt

Please log in to comment
0
To much text!
Perhaps you should consider building a proper MSI.

Answered 08/17/2015 by: dedenker
Third Degree Black Belt

Please log in to comment
Answer this question or Comment on this question for clarity