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

Insert file with name containing yesterday's Date using 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

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

启动 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

稳健的方式:

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