Replacing a string with timestamp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nibbiotnt
Posts: 2
Joined: 15 Mar 2011 07:58

Replacing a string with timestamp

#1 Post by nibbiotnt » 15 Mar 2011 08:18

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

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Replacing a string with timestamp

#2 Post by ChickenSoup » 18 Mar 2011 10:58

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%

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replacing a string with timestamp

#3 Post by ghostmachine4 » 18 Mar 2011 18:07

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)

nibbiotnt
Posts: 2
Joined: 15 Mar 2011 07:58

Re: Replacing a string with timestamp

#4 Post by nibbiotnt » 22 Mar 2011 09:05

thx, i'll try it

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Replacing a string with timestamp

#5 Post by !k » 22 Mar 2011 14:28

Code: Select all

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Replacing a string with timestamp

#6 Post by aGerman » 22 Mar 2011 15:32

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

Post Reply