Page 1 of 1

Using a Varible with Set command

Posted: 27 Aug 2010 14:13
by Netcom
Hello All
Is it possible to use a varible with the set command? Im trying to create a directory to backup files into. So Im using the "set BACKUP_DIR=\ESM_Backup_5_x\Latest" however I would like to have the script name the folder with the current date and time. The problem is throughout the script Im calling BACKUP_DIR. If the path or name of the folder is changing how would this work. I have included the script im using below.

Code: Select all

@echo off

:
:      BackupESM.bat
:
:      This batch file will save a backup of the following Enterprise Service Manager information:
:         - Configuration files
:         - Database files:       
:         - Registry settings
:         - ESM default process inbox files    
:         - ESM default process shared files
:
:      This information will be stored on the same drive as the ESM, in a folder called
:      \ESM_Backup_5_x\Latest.

setlocal
set BACKUP_DIR=\ESM_Backup_5_x\Latest
echo.

net stop "mnwatchdog"

rem     Getting the installation directory...
start /W regedit /E %TEMP%\ESM_Reg.reg "HKEY_LOCAL_MACHINE\SOFTWARE\MarchNetworks\ESM"
if exist %TEMP%\ESM_Reg.reg goto gotFolder


rem     Getting the installation directory again (such as from Win2008 R2)
start /W regedit /E %TEMP%\ESM_Reg.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MarchNetworks\ESM"
if not exist %TEMP%\ESM_Reg.reg (
    echo Error: Could not read registry of the Enterprise Service Manager.
    goto exitError
)

:gotFolder
rem     Figure out the installation folder and drive
for /F "tokens=2 delims==" %%l in ('type "%TEMP%\ESM_Reg.reg" ^| find "InstallationDir"') do (
  set INSTALL_DIR=%%l
  set INSTALL_DRIVE=%%~dl
)

if '%INSTALL_DIR%' == '' (
  echo Error: Could not determine the Installation folder of the Enterprise
  echo        Service Manager.
  goto exitError
)

rem     Strip the quotes from the installation directory so that we can append paths to it
set INSTALL_DIR=%INSTALL_DIR:"=%

rem     Prepend the backup directory with the installation drive
set BACKUP_DIR=%INSTALL_DRIVE%%BACKUP_DIR%
set RESTORE_FILE=%BACKUP_DIR%\Restore.bat

rem     Check for the installation directory
if not exist "%INSTALL_DIR%" (
  echo The Enterprise Service Manager could not be found in
  echo "%INSTALL_DIR%"
  goto exitError
)

rem     Make sure that the backup folder doesn't already exist
if exist %BACKUP_DIR% (
  echo The folder %BACKUP_DIR% already exists.
  echo Rename it and run this script again.
  goto exitError
)
if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%
if not exist %BACKUP_DIR% (
  echo There was an error creating the %BACKUP_DIR% folder.
  echo Do you have the appropriate permissions?
  goto exitError
)

rem     Enable read permissions on Configuration folder...
cacls "%INSTALL_DIR%\Configuration" /T /E /G Everyone:F >NUL

call :CreateRestore

echo Copying files to the backup folder...
call :Backup "%INSTALL_DIR%\Configuration" %BACKUP_DIR%\Configuration
call :Backup "%INSTALL_DIR%\Database" %BACKUP_DIR%\Database
call :Backup "%INSTALL_DRIVE%\esm\sharedFiles" %BACKUP_DIR%\sharedFiles
call :Backup "%INSTALL_DRIVE%\esm\Inbox" %BACKUP_DIR%\Inbox

rem Backup the registry information, doesn't support auto restore in
rem Resotre.bat because of safe reason
copy %TEMP%\ESM_Reg.reg %BACKUP_DIR%\ > NUL
del /f /q %TEMP%\ESM_Reg.reg

echo. >>%RESTORE_FILE%
echo echo Restore complete >>%RESTORE_FILE%
echo pause >>%RESTORE_FILE%

echo.
echo The Enterprise Service Manager configuration and database files have
echo been backed up to %BACKUP_DIR%.
echo Please rename this folder to something meaningful to you.
echo.

rem pause
set "foldername=ESM_Backup"
for /f "delims=/ tokens=1,2,3" %%x in ("%date%") do (
ren C:\ESM_Backup_5_x\Latest "%foldername% %%z-%%y-%%x"
)

echo:Back Up Successfu: %date% %time%>>C:\ESM_Backup_5_x\log.txt
net start "mnwatchdog"
goto :EOF

:exitError
echo.
echo Backup Failed!
echo.
echo:Back Up Failure: %date% %time%>>C:\ESM_Backup_5_x\log.txt
rem pause
exit /B 1

:Backup
   if not exist %1 (
     echo    Warning: Cannot find %1
      goto :EOF
   )
   mkdir %2
   echo    %1
   xcopy /q /e %1 %2 >NUL
   : write to the Restore file
   echo if not exist %1 mkdir %1 >>%RESTORE_FILE%
   echo xcopy /y /q /e %~n2 %1 >NUL >>%RESTORE_FILE%
goto :EOF

:CreateRestore
   echo @echo off >%RESTORE_FILE%
   echo. >>%RESTORE_FILE%
   echo : >>%RESTORE_FILE%
   echo :      Restore.bat >>%RESTORE_FILE%
   echo : >>%RESTORE_FILE%
   echo :      This batch file will restore the Enterprise Service Manager that is >>%RESTORE_FILE%
   echo :      in this folder. >>%RESTORE_FILE%
   echo. >>%RESTORE_FILE%
   echo echo Are you sure you want to restore the backed-up ESM files? >>%RESTORE_FILE%
   echo echo Press Ctrl-C to quit. >>%RESTORE_FILE%
   echo pause >>%RESTORE_FILE%
   echo. >>%RESTORE_FILE%
   echo rem regedit /s ESM_Reg.reg >>%RESTORE_FILE%
   echo cacls "%INSTALL_DIR%\Configuration" /T /E /G Everyone:F >NUL >>%RESTORE_FILE%
   echo. >>%RESTORE_FILE%

rem call C:\ESM_Backup_5_x\rename.bat



goto :EOF


Re: Using a Varible with Set command

Posted: 31 Aug 2010 13:53
by alan_b
Your script was too long and complicated for me to consider when I came by a few days ago.

I have just had a second look and am still not willing to get stuck into it,
BUT HERE may be the answer to the question you posed :-

Code: Select all

SET BACKUP_DIR="\ESM_Backup_5_x\%DATE%-%TIME%"


There-after where-ever you invoke
%BACKUP_DIR%
it will be replaced by the appropriate directory path including the time stamp as it had been when those values were frozen into SET BACKUP_DIR=etc.

Another possibility is to use brackets

Code: Select all

(
ECHO %DATE% %TIME%
PAUSE
ECHO %DATE% %TIME%
)
ECHO %DATE% %TIME%

There is a time freeze between the brackets and immediate recovery after the closing bracket.

Alan