Home > Software design >  SSIS Report: How to rename an excel file in a specific location
SSIS Report: How to rename an excel file in a specific location

Time:04-27

We get an excel file in a specific location where the name of the file may vary every time.

So every time we rename the excel file manually to "Report.xlsx" and then we have some operation to be done using a script task. The file_Path file_Name is hardcoded in the script task as

Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Open(@"D:\Desktop\Report.xlsx");

Is there a way to get the file name from the file location and pass it to the above code ?

Note: The file Path is static but the file name will vary every time. At a time there will be only one excel file in the file Path. We are already using a Sequence Container so cant change it.

Thanks in advance..

CodePudding user response:

This will let you use the actual file name. Make sure to reference System.Linq

var dir = new System.IO.DirectoryInfo(@"D:\Desktop");
var fullFilePath = dir.GetFiles("*.xlsx").Select(f => f.FullName).First();

if(fullFilePath != null)
    Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Open(fullFilePath);
  • Related