使用BCP插入名称包含昨天日期的文件

时间:2016-02-17 12:05:59

标签: sql-server windows batch-file command-line bcp

我有一个BCP命令,其中包含一个硬编码文件名ID_Customer_160216.csv。文件名以格式为yymmdd

的日期结束
bcp sfnav.dbo.Customer in "C:\Users\TSL\Desktop\TSL Data\ID_Customer_151124.csv" -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz

我想让它变得动态:用给定格式的昨天日期替换它。

2 个答案:

答案 0 :(得分:0)

启动Powershell ISE并粘贴以下内容。我认为这就是你所追求的......?我知道它不是cmd但是,现在是时候从这种事情继续前进了;)

$yesterdaysDateExtension = (Get-Date).AddDays(-1).ToString("yyMMdd")
$fileName = "ID_Customer_$yesterdaysDateExtension.csv"

$filePath = "C:\Users\TSL\Desktop\TSL Data\$fileName"

Write-Host "Attempting bcp with file $filePath"
bcp sfnav.dbo.Customer in $filePath -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz

答案 1 :(得分:0)

一种强有力的方式:

<强> RunBCP.bat

@echo off

::Creating the VBS code that give yesterday date
echo wscript.echo DateAdd("d", -1, date(^)^)>Day.vbs

::Getting yesterday date with day.vbs
for /f "tokens=1-3 delims=/" %%a in ('cscript //nologo Day.vbs') do set "$date=%%c%%b%%a"
del Day.vbs 2>nul

::setting date to YYMMDD
set "$Date=%$Date:~2%"

::Running BCP with the substitued Date
bcp sfnav.dbo.Customer in "C:\Users\TSL\Desktop\TSL Data\ID_Customer_%$Date%.csv" -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz

然后您必须运行 RunBCP.bat