Question

all.

I wrote an SSIS package that queries data from defined databases and puts them into CSV files in a specific location. Then, within the SSIS package, I call a pre-written batch file that creates a folder in the specified directory, renames the folder to the date in format "yyyy-mm-dd", and then move the created CSV files into the newly created folder.

Batch file code as follows:

@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%date:~6,6%-%date:~3,2%-%date:~0,2%
md \\remote_folder\%date%
move /Y \\remote_folder\*.csv \\remote_folder\%date%
exit

When running this from my SSIS package, everything works fine.

The service account that fires the agent jobs has full administrative control. When scheduling this as an automated job using the SQL Server Agent, the folder doesn't get created properly. My thinking is that the SQL Agent either has trouble firing the batch file, or that they are incompatible.

Has anyone else come across this or know of a work-around?

Was it helpful?

Solution

Use File System Tasks:

Create a variable called RmtArchPath (string)

Set its value with the expression:

"\\\\Remote_folder\\" + (DT_WSTR,4)YEAR(GETDATE()) + "-" + RIGHT("0" + (DT_WSTR,2)MONTH(GETDATE()), 2) + "-" + RIGHT("0" + (DT_WSTR,2)DAY( GETDATE()), 2) + "\\"

Create a file system task. For operation choose Create Directory. For UseDirectoryIfExists choose True. For IsSourcePathVariable choose RmtArchPath.

Create a second Variable called SrcFileName (string)

Create a ForEachLoop Container. For Collection choose the enumerator "Foreach File Enumerator", set the Folder = \\Remote_Folder, set the files = *.csv. For Variable Mappings set the variable = User::SrcFileName.

Connect the File system task to the loop. (from the FS task to the loop for your flow direction).

Create a second file system task inside the loop.

Choose Move file for the operation. Set IsDestinationPathVariable to True, Set DestinationVariable = RmtArchPath. Set IsSourcePathVariable = True, Set SourceVariable = SrcFileName.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top