Jump to content

Exporting SIMS backup dumps with date and timestamp affix/suffix from batch


Recommended Posts

Posted (edited)

Hi Everyone,

 

Does anyone by any chance do SIMS backups using a batch script?

Up til now I've managed to rename files I saved to a linux box so never had issues with scripting scheduled renames but it seems I'm missing something to do it on Server 2012, perhaps the date script format has changed from when I last did it donkeys years ago?

 

eg if I try using:

 

for /f "tokens=2-4* delims=/ " %%a in ('DATE /T') do set THEDATE=%%c%%a%%b

 

set hours=%time:~0,2%
set minutes=%time:~2,3%
set hours=%hours::=%
set minutes=%minutes::=%

 

And then try using it with dbattach.exe....

 

path=C:\WHATEVER_PATH\SIMSBACKUP\%THEDATE%_%hours%_%minutes%_SIMS.bak

 

I end up with files named like:

082017_14_00_SIMS.bak

 

So I'm missing the date of the month, just got the month,year and then time.

 

Also if it runs in the wee small hours then I get just 082017_ and nothing else, it only seems to work after midday up until midnight :)

Edited by houlta
Posted

Definitely not, which surprises me as I used to use something similar but it was a long time ago.

 

The script outputs as "set THEDATE=082017" and of course will echo '082017'

Posted

When I have to create batch files with the date and time I normally use Ritchie Lawrence's batch functions. They've never failed.

 

The following code will do what you want. :)

 

yX0pHT.png

 

@echo off & setlocal ENABLEEXTENSIONS
call :GetDate y m d
call :GetTime h n s t
[color="#FF0000"]echo %y%-%m%-%d%_%h%.%n%[/color]
goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By:   Ritchie Lawrence, 2002-06-15. v1.0
:: Func: Loads local system date components into args 1 to 3.
:: Args: %1 var to receive year, 4 digits (by ref)
::       %2 var to receive month, 2 digits, 01 to 12 (by ref)
::       %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetTime hh nn ss tt
::
:: By:   Ritchie Lawrence, updated 2007-05-12. v1.3
:: Func: Loads local system time components into args 1 to 4.
:: Args: %1 Var to receive hours, 2 digits, 00 to 23 (by ref)
::       %2 Var to receive minutes, 2 digits, 00 to 59 (by ref)
::       %3 Var to receive seconds, 2 digits, 00 to 59 (by ref)
::       %4 Var to receive centiseconds, 2 digits, 00 to 99 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
for /f "tokens=5-8 delims=:,. " %%a in ('echo/^|time') do (
 set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  • Thanks 1
Posted (edited)
is that possible to call from within a batch script?

It sure is. :)

 

First of all copy the code below and paste it at the bottom of your batch file.

 

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By:   Ritchie Lawrence, 2002-06-15. v1.0
:: Func: Loads local system date components into args 1 to 3.
:: Args: %1 var to receive year, 4 digits (by ref)
::       %2 var to receive month, 2 digits, 01 to 12 (by ref)
::       %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetTime hh nn ss tt
::
:: By:   Ritchie Lawrence, updated 2007-05-12. v1.3
:: Func: Loads local system time components into args 1 to 4.
:: Args: %1 Var to receive hours, 2 digits, 00 to 23 (by ref)
::       %2 Var to receive minutes, 2 digits, 00 to 59 (by ref)
::       %3 Var to receive seconds, 2 digits, 00 to 59 (by ref)
::       %4 Var to receive centiseconds, 2 digits, 00 to 99 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
for /f "tokens=5-8 delims=:,. " %%a in ('echo/^|time') do (
 set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

Next, add the following three lines right at the top of your batch file. If you already had an "@echo off" delete this.

 

@echo off & setlocal ENABLEEXTENSIONS
call :GetDate y m d
call :GetTime h n s t

 

Then set your THEDATE variable so that it is using the variables from the batch functions. e.g.

 

set THEDATE=%y%-%m%-%d%_%h%.%n%

 

That should be all you need to do.

Edited by Arthur
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...