Get filenames from folder...foreach loop

  • I have a folder which consists of few files. I want to get the Filenames into a temporary table in Temp DB.

    I created a package which consists of:

    1) a sql task which creates a table in Temp DB

    2) a foreach loop container

    3) a sql task in foreach loop to insert the filenames in the TempTable. but I'm getting all Null values instead of FileNames...

    can someone help me with how to create the package and use the variables.

  • you can use xp_cmdshell to run the Dir command on that folder. here you need to do some extra work to separate the filenames from the output of dir command. you can put the whole functionality in Execute SQL task.

    with best regards,

    Vijayraj Bhosale.

  • Use the xp_cmdshell with the following string:

    dir \\server\share\sourcedirectory /b /a-d >\\Server\Share\targetdirectory\filelist.txt

    This will create a text file of all files in \\server\share\sourcedirectory called filelist.txt in the directory \\Server\Share\targetdirectory - do not create this file in the source directory, or it will end up in its own list!

    The file will contain a list of files, one per line and you can then transfer this into a table using normal data import methods.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply