Passing a Date from an SSIS Package to a “Add Date Suffix to File Name” batch file routine

StackOverflow https://stackoverflow.com/questions/7444233

  •  21-01-2021
  •  | 
  •  

سؤال

I have an SSIS Package that passes a date param from a Package variable to a Batch file as a parameter. The intent of the Batch file is to rename a predetermine file in an predetermined location such a date suffix is added to the file name where teh date suffix is in the format "_YYYY_MM."

The issue that I am having is that when I run the SSIS BIDS 2005 package in the IDE, the following batch file renames the file without a leading zero in the month. however, when I run it through our scheduler on our test server, the file is properly renamed so that the month has a leading zero.

Is perhaps due to a potential difference in drivers?

Here is the code for the batch file:

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%

I am really a noob with batch files.

What I would like to do is this, if someone can help:

Adjust the code so that it tacks on a leading zero if the length of the month when evaluated as a string is < 2.

Secondly, I would like this rename file procedure to be more generic so that I can pass in the file name full path as an additional param and have a suffix tacked onto ANY file.

Can someone with more BATCH prowess assist?

هل كانت مفيدة؟

المحلول

Since SSIS sucks a little less than batch files, another option is to get the package to format the date with a leading zero in teh month:

Right("0"+ (DT_WSTR, 2) MONTH( @[User::ReportingMonth]  ) ,2) + "/" + Right("0"+ (DT_WSTR, 2) DAY( @[User::ReportingMonth]  ) ,2) + "/" + (DT_WSTR, 4) YEAR( @[User::ReportingMonth]  )  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top