ssis renaming a file wthout knowing existing name - csv

I have set up part of my ssis package to check a folder and move the csv file to another folder using a file system task.
Is it possible to rename a file without knowing what the source name will be? as I have been told it will be a new name on a daily bases. can I use a wildcard in a file system task? would I be best renaming before moving what is best practise?

For an unknown file name you can wrap your file system task in a foreach loop
wrap your file system task in a foreach loop and set to file enumerate
set the folder to your source location
set search string to *.csv
set to full file path
map to a variable called fname
use fname as variable in file system task for source
Make sure you delay validation on connection manager

Related

SSIS to get the particular file from multiple files and copy to different folder

my question is how we will create SSIS package to get the particular file from multiple files and copy to different folder
Need Help
You can use File System Task in your package control flow.
Configure the properties as below:
DestinationConnection: Enter the full file path of the folder you want to move your file to e.g c:\Users\ToTest
Operation: Change to Move file.
SourceConnection: Enter the full file path of the folder where your file is, including the file name and extension e.g c:\Users\Test\testfile.csv
You'll need at least two components.
Use a Foreach Loop container with the default Foreach File Enumerator on the Collection tab. Set Folder: to \YourFolder and build an expression under Files: to identify the particular file you're interested in.
Inside the Foreach Loop container, add a File System Task to Copy the file from the Source folder to the Destination folder.

SSIS: How to change metadata on flat file connection manager

I am working in BIDS. I have an SSIS package that loops over all files in a given directory and imports all of them to a table. So, my control flow has a Foreach Loop Container with a Data Flow Task inside of it. The Data Flow Task goes straight from a Flat File Source to an OLE DB Destination. Because the directory can change, I have made the Foreach Directory dynamic (i.e., I gave it a package variable, which I can change when the directory changes). The Files field has something like stuff*.pip, such that all files in the directory that match this pattern get imported. The Variable Mappings tab has a file name variable, which is the same variable used in the Expressions of the flat file connection manager.
When I originally set up the flat file connection manager, I had to point it to an already existing file to pick up the file's metadata. I then changed the file connection manager to dynamic (i.e., I added a file name variable in the Expressions property) such that the Foreach Loop Container will pick up all files (that match the pattern).
Since then, the metadata on the file has changed. So, when I open the solution file, it throws a warning on the Flat File Source in the Data Flow Task. To resolve this, I have to temporarily change the flat file connection back to a hard-coded path such that I can change the metadata on the new file. I then make it dynamic again (i.e., use the file name variable in the Expressions, which overrides the hard-coded file path). (Finally, I double-click on the Flat File Source in the Data Flow Task to automatically update the metadata in the Flat File Source.)
Is this the correct way to update the metadata on a flat file? It seems a bit clunky to me.
You can prevent this error from occurring by editing the properties on the Flat File Manager and setting ValidateExternalMetaData=False.
You may also need to set DelayValidation=True on the Data Flow Task that contains the Flat File Manager.
This will prevent the warning - but if the file's metadata changes for some reason (columns change, data types change) and you do not remember to update the metadata then you will get a runtime error.

Dynamic Flat File Connection to import new file everyday

I have to create a job which imports data from .csv file into database table everyday. I have created the job and it works fine as long as the filename is same.
The file that comes in everyday has a different name, so i am trying to set up dynamic flat file connection. As i have only one file to load everyday i am not trying to use For Each Loop Container and also, i am not good at script task.
I was trying to see if there is any other way to achieve this using SSIS.
I have created a variable for my path "C:\Daily Files\" as #[User::MyFilePath]. I am then using this variable in the ConnectionString Expression property of the Flat File Connection Manager.
But it does not work. I get an error saying the Cannot open the datafile.
Can someone tell me what am i missing here?
If you need to create a connection manager to a file that changes name everyday, you will have to write some kind of expression to do so. On the other hand, the easier way out is to use the For Each Loop container as it does not care what the name of the file is as long as you give it *.csv in the qualifying field.

SSIS For Each Loop crashes the flat file connection

I created a simple SSIS package to import a flat file (.txt) into a database table. Tested that and it works perfectly. Since I have several files to import, I added a foreach loop to go through all the files, added the variables as recommended in several examples found on the net but now my flat file connection manager returns an error of "A valid file name must be selected." and the package will not run. I have so far been unsuccessful in finding the solution for this issue and would appreciate any suggestions by the SSIS gurus of this forum. Many thanks in advance!
Here is what I have in the way of variables:
SourceFileFolder which is the path to the folder that contains the files
FileName a string containing one of the names of the files I am seeking to import
SourceFilePath which is an expression driven variable that incorporates the previous two variables concatenated together. I can click "Evaluate Expression" and copy and paste it into windows explorer and open the file
ArchivePath which is an expression driven variable that creates the path to archive the file to once it is processed.
As the message says this is related to your connection manager not gathering the connection string. This can be handled using the following:
First of all clear the expression on the SourceFilePath variable.
With your Foreach Loop Container, set it up as follows:
This will use your variable SourceFileFolder as the Folder, you could also just hardcode the folder name C:\ for instance. Also make sure your folder is qualified correctly, I.E. make sure it finishes with a slash C: won't work but C:\ will work.
Next you need to map the fully qualified name to your other variable SourceFilePath
This should now store the full name of the file the loop has found into the SourceFilePath variable. For Instance C:\File.txt, you can now use this as a connection string expression on your file connection manager.
Under the properties of the connection manager make sure the expression is set to ConnectionString and then use the SourceFileName variable.
ALSO MAKE SURE DELAY VALIDATION IS SET TO TRUE
This hopefully should mean you can loop through the files.

SSIS Flat File Wildcard Filename

I want to load a .csv file that is dropped in a specific directory but the filename is suffixed with YYYYMMDD, eg. MyFile_YYYYMMDD.csv.
The directory is cleared out everyday and a new file is dropped, ie. there is only ever 1 file in the directory.
Is there a (simple?) way to code a wildcard filename, eg. MyFile_*.csv, that can be used by the Connection Manager without having to use a Foreach Loop Container or a Script?
Yes, with the Foreach Loop Container you can use the Foreach File Enumerator to loop through a certain directory. In the Files property of that enumerator you can use wildcards, e.g., MyFile_*.csv.
Map a variable to the fully qualified file name and push that variable to your connection manager with an expression.