Page 1 of 1

Creating fold using trime date stamp

Posted: 24 Aug 2010 16:02
by Netcom
Hello All,
Im trying to modify the below script to create the folder woth a time/date stamp each time the batch file is ran. Currently the batch file stops in error if a folder exist. Whats the easiest was to accomplish this?


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.

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 %datestr%%timestr%
  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.
pause

goto :EOF

:exitError
echo.
echo Backup Failed!
echo.
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%
goto :EOF




Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 06:34
by orange_batch
...%date%?

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 06:50
by batchfileman
Pretty simply really


Code: Select all

set filename=%date:~0,14%
echo This sentence will end up in a text file on the C drive called %filename% >> "c:\%filename%.txt"


Rather than echo in the above example you can simply send whatever you want from the command line to that file. for example:

Code: Select all

set filename=%date:~0,14%
ipconfig >> "c:\%filename%.txt"


The above example will send the IP configuration output to a text file called WED08252010 (Which is the current date as I write this).

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 09:14
by Netcom
Thanks for your help, But Im not sure understand how to use that in my script. Im pretty new to this stuff.
So if I understand correctly I would use

Code: Select all

set filename=%date:~0,14%
ipconfig >> "c:\%filename%.txt"

and that would create a new folder with the currently date/time? How would I add that to my existing code?

Thanks again for your help

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 09:20
by batchfileman
Excuse me. That would create a text file with that information in it. Additionally it would make the file name the current date.

To make a folder I assume you could use the md (make directory) command with the same variable %filename%

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 10:18
by orange_batch
Hehe, I get the feeling a lot of people come here expecting someone to write or fix their script rather than lead them in the right direction. 8)

Netcom, I don't fully understand your problem or script, but if you're trying to put the current date into the name of a new directory, the script would go something like this:

Assuming %date% expands to this format: 25/08/2010

Code: Select all

set "foldername=My Folder Name"
md "C:\%foldername% %date:/=-"


This creates, as of today, C:\My Folder Name 25-08-2010

If you want to make the dates in alphanumeric order:

Code: Select all

set "foldername=My Folder Name"
for /f "delims=/ tokens=1,2,3" %%x in ("%date%") do (
md "C:\%foldername% %%z-%%y-%%x"
)


This creates, as of today, C:\My Folder Name 2010-08-25

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 10:55
by Netcom
orange_batch wrote:Hehe, I get the feeling a lot of people come here expecting someone to write or fix their script rather than lead them in the right direction. 8)

Netcom, I don't fully understand your problem or script, but if you're trying to put the current date into the name of a new directory, the script would go something like this:

Assuming %date% expands to this format: 25/08/2010

Code: Select all

set "foldername=My Folder Name"
md "C:\%foldername% %date:/=-"


This creates, as of today, C:\My Folder Name 25-08-2010

If you want to make the dates in alphanumeric order:

Code: Select all

set "foldername=My Folder Name"
for /f "delims=/ tokens=1,2,3" %%x in ("%date%") do (
md "C:\%foldername% %%z-%%y%-%x"
)


This creates, as of today, C:\My Folder Name 2010-08-25


Thanks Orange_batch,
Im sorry if it seems I want someone to write it for me however since Im a newbie I dont fully understand how to apply the examples into the existing script.

Thanks again!

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 11:19
by orange_batch
Oops, typo.

%%z-%%y%-%x

should be

%%z-%%y-%%x

I edited my previous post.

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 15:56
by Netcom
Ok I have the bat file working great. I would like to add a log file that keeps track of failures. In the event of a failure the script does the following;

Code: Select all

:exitError
echo.
echo Backup Failed!
echo.
pause
exit /B 1


could I add something like "echo failed >> c:\log.txt" to create a log file which would add a new line to the file each time it fails? Maybe include a date time?

Thank

Re: Creating fold using trime date stamp

Posted: 25 Aug 2010 16:55
by orange_batch
Yeah, that's easy.

Code: Select all

echo:Failure at: %date% %time%>>c:\log.txt


Even better if you can forward a variable with information about what failed.

Re: Creating fold using trime date stamp

Posted: 26 Aug 2010 09:31
by Netcom
Thanks that worked great!