批处理由当前时间获得上周时间段格式为yyyyMMdd

工作中需要用BCP导入一个分天表的一周的数据,在这个需求中用批处理自动生成bcpin脚本,请教了一位同事,是个高手啊,给我写了一段脚本,现共享出来,bcpinweek.bat:
@reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d yyyy-MM-dd /f>nul
@echo dt=weekday("%date:~0,10%",2)+6>%temp%\tmp.vbs
@echo wscript.echo dt>>%temp%\tmp.vbs
@for /f %%i in ('cscript /nologo %temp%\tmp.vbs') do @set tempweek=%%i
@set /a tempnum=%tempweek%-7
:LOOP
@echo dt=cdate("%date:~0,10%")-%tempweek%>%temp%\tmp.vbs
@echo wscript.echo dt>>%temp%\tmp.vbs
@for /f %%i in ('cscript /nologo %temp%\tmp.vbs') do @set tempdate=%%i
@echo bcp DBname..tablename in d:\databak\tablename_%tempdate:~0,4%%tempdate:~5,2%%tempdate:~8,2%.dat -Usa -P -Sservername -n
@set /a tempweek=%tempweek%-1
@if %tempweek% gtr %tempnum% goto LOOP



执行bcpinweek.bat >bcpinlog.txt
打开bcpinlog.txt内容如下:
bcp DBname..tablename in d:\databak\tablename_20100315.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100316.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100317.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100318.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100319.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100320.dat -Usa -P -Sservername -n
bcp DBname..tablename in d:\databak\tablename_20100321.dat -Usa -P -Sservername -n

你可能感兴趣的:(工作,脚本,F#)