简体   繁体   中英

Copy files between folders; with file prefixes identified in a csv / excel

I'm taking the first steps in this tool and would like to copy a set of excel files from one folder to another. The source folder has a set of files with suffix YYYYMM, and I will have to copy only the files of the desired month (dependent variable of a query to BD).

The source folder contains several excel file names with "prefix_name_YYYYMM.xlsx", and the prefixes will be available in a txt file or possibly an excel (which is easier to implement!).

I've read some articles on "Foreach Loop Container" and "File System task" for all files in a folder.

But I cannot imagine how I'll get the various file names / prefixes to feed the Loop and get all the files prefix1_YYYYMM _ *. XLSx

Note: the client doesn't want to populate a sql server table with the "prefix file list".

if you want to get the various file names / prefixes to feed the Loop use c# code in the Script task

   string SourceDirectory = Dts.Variables["User::varSourceFolder"].Value.ToString();
                string[] fileEntries = Directory.GetFiles(SourceDirectory);

foreach (string fileName in fileEntries)
                {.....}

This can be achieved by using a File System Task within a Foreach Loop. The following steps outline this further. A couple things to note on the File System Task, you'll need to set DelayValidation to true, and if you intend to copy new files that will replace old ones then OverwriteDestination will also need to be set to true.

  • Add a Foreach Loop to you Control Flow, and configure it as a Foreach File Enumerator.
  • On the Collections pane select the source folder.
  • For the Files field, use your prefix with * to get all files with this prefix. For example, prefix_name_YYYYMM*.xlsx.
  • In the Variable Mappings pane, add a string variable, and set it at Index 0.
  • Within the Foreach Loop, add a File System Task, with the copy file operation. For the Source Connection, set IsSourcePathVariable to true, then add the same variable used in the mapping of the Foreach Loop as the SourceVariable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM