Page 1 of 1

Replacing a string with timestamp

Posted: 15 Mar 2011 08:18
by nibbiotnt
I'm using this string to backup via comand line (SQLCMD) a db on sql.

BACKUP DATABASE [NameDB] TO DISK = N'c:\SqlDB\NameDB.bak' WITH NOFORMAT, NOINIT, NAME = N'NameDB-Full DB Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10


I wish, through windows "Scheduled task", run daily backups.
Obviously the file would be overwritten.

Is it possible to scan the string with a batch file, searching the filename and appending it with a time stamp?

example NameDB.bak becomes NameDBYYYYmmddhhmm.bak (NameDB201103151400.bak )

NibbioTNT
thx

Re: Replacing a string with timestamp

Posted: 18 Mar 2011 10:58
by ChickenSoup
Well, in batch this can get you a timestamp. Not sure if this helps:

Code: Select all

echo %date:~-4%%date:~-7,2%%date:~-10,2%%time:~0,2%%time:~3,2%

Re: Replacing a string with timestamp

Posted: 18 Mar 2011 18:07
by ghostmachine4
ChickenSoup wrote:Well, in batch this can get you a timestamp. Not sure if this helps:

Code: Select all

echo %date:~-4%%date:~-7,2%%date:~-10,2%%time:~0,2%%time:~3,2%


as usual, the output of this method is dependent on regional settings on the individual computer its run on. You will have to do some tweaks to it, (that's why i say batch s**ks) , or use vbscript (powershell)

Re: Replacing a string with timestamp

Posted: 22 Mar 2011 09:05
by nibbiotnt
thx, i'll try it

Re: Replacing a string with timestamp

Posted: 22 Mar 2011 14:28
by !k

Code: Select all

for /f "skip=1 delims=." %%d in ('WMIC OS Get LocalDateTime') do echo %%d

Re: Replacing a string with timestamp

Posted: 22 Mar 2011 15:32
by aGerman
Good idea, !k. I had some problems to save the value in a variable. I guess it has to do with the fact that the output is encoded in unicode little endian. For some reason when I tried...

Code: Select all

for /f "skip=1 delims=." %%d in ('WMIC OS Get LocalDateTime') do set "timestamp=%%d"

... I found only a CR character in %timestamp%.

This worked:

Code: Select all

for /f "skip=1 delims=." %%d in ('WMIC OS Get LocalDateTime^|findstr .') do set "timestamp=%%d"

Regards
aGerman