append date to osql made backup with batch
thumbs up for this forum and am glad there's so many volunteers around on line.
I'm not familiar with batch, so maybe someone would be so kind to enlighten me with answer. Be very thankfull.
I try to backup many sql db with osql and then rename them with appended date ( and time)( .bak to .dat is optional too) wanna have all code in one batch so that I would be able to schedule it
Found the way to backup sql db with osql and it works fine. Also found the way to rename file in batch code and it works fine too.
But when I try to join two of them together it backups files but with no rename part. I'm stucked and seems to not find the way out of this hole. Can someone help me, please, my eyes cannot see over that obstacle.
:: BAT file
echo off
osql -U** -P** -Sserver\SQLEXPRESS2005 -ddb -u -w250 -n -l 30 -ib开发者_StackOverflowack.txt -ooutput.txt
FOR /F "TOKENS=1,2*" %%A IN ('DATE/T') DO SET DATE=%%B
SET DATE=%DATE:/=%
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A
SET TIME=%TIME::=%
set TODAY=%DATE%%TIME%
echo %TODAY%
rename "C:\back\TEST.bak" "TEST%TODAY%.dat"
@echo on
Thanks in advance
%DATE% and %TIME% are system environment variables and should not be reset. You should use different variables.
I like this approach:
:: BAT file
echo off
osql -U** -P** -Sserver\SQLEXPRESS2005 -ddb -u -w250 -n -l 30 -iback.txt -ooutput.txt
FOR /f "tokens=2,3,4 delims=/ " %%a in ('echo %DATE%') do (
set "Month=%%a"
set "Day=%%b"
set "Year=%%c"
)
FOR /f "tokens=1,2,3,4 delims=:." %%a in ('echo %TIME%') do (
set "HOUR=%%a"
set "MINUTE=%%b"
set "SECOND=%%c"
set "HUNDREDTHS=%%d"
)
SET Today=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%%HUNDREDTHS%
echo %TODAY%
rename "C:\back\TEST.bak" "TEST%TODAY%.dat"
@echo on
精彩评论