将日期从 SSIS 包传递到"Add Date Suffix to File Name"批处理文件例程



我有一个SSIS包,它将日期参数从包变量传递到批处理文件作为参数。批处理文件的目的是重命名位于预定位置的预定文件,例如在文件名中添加日期后缀,其中日期后缀的格式为"_YYYY_MM"。"

我遇到的问题是,当我在IDE中运行SSIS BIDS 2005包时,下面的批处理文件在月份中将文件重命名为没有前导零。但是,当我通过测试服务器上的调度程序运行它时,该文件被正确地重命名,以便月份有一个前导零。

可能是由于驱动程序的潜在差异?

下面是批处理文件的代码:
ECHO OFF
SET CurrentFolder=%~dp0
SET ThisFileName=%~n0
REM The following line populates shared path variables used below
CALL %CurrentFolder%Membob_SetPaths.cmd
SET LogFile=%LogFolder%%ThisFileName%.log
ECHO %date% %time%  - *** BOJ %CurrentFolder%%ThisFileName%.cmd *** >> %LogFile%
SET ReportingDate=%1 
ECHO Logging to: %LogFile%
ECHO ReportingDate=%ReportingDate% >> %LogFile%
REM Rename the file from MEMBOB.csv to MEMBOB_YYYY_MM.csv, using the passed ReportingDate 
REM But before we rename, delete any file that might already have that target name.
@For /F "tokens=1,2,3 delims=/ " %%A in ("%ReportingDate%") do @( 
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
REM if %Month% LSS 10 Set Month=0%Month%
SET NewFileName=MEMBOB_%Year%_%Month%.csv
if exist %ExportFolder%%NewFileName% del %ExportFolder%%NewFileName%
REM Rename the file as described above
ECHO Rename %ExportFolder%Membob.csv to %NewFileName% >> %LogFile%
rename %ExportFolder%Membob.csv %NewFileName% 
ECHO %date% %time%  - *** EOJ %CurrentFolder%%ThisFileName%.cmd *** >> %LogFile%

我对批处理文件真的是一个新手。

如果有人能帮忙,我想做的是:

调整代码,以便在作为字符串求值时,如果月份的长度为<2 .

其次,我希望这个重命名文件过程更加通用,以便我可以将文件名完整路径作为附加参数传递,并在任何文件上添加后缀。

能有更有BATCH能力的人帮忙吗?

由于SSIS比批处理文件更糟糕,因此另一种选择是让包在月份中使用前导零来格式化日期:

Right("0"+ (DT_WSTR, 2) MONTH( @[User::ReportingMonth]  ) ,2) + "/" + Right("0"+ (DT_WSTR, 2) DAY( @[User::ReportingMonth]  ) ,2) + "/" + (DT_WSTR, 4) YEAR( @[User::ReportingMonth]  )  

最新更新