SSIS 脚本任务使用通配符删除文件
SSIS Script Task Delete Files using a Wildcard
使用 SQL Server 2014 \ VS 2019。我有下面的 C# 脚本任务来删除文件夹中超过 x 天的文件,这有效。
public void Main()
{
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod)))
{
currFileInfo.Delete();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
但是,我该如何修改,只是说,删除文件名以 'ABC' 开头的目录中的所有文件?
在类似于 StartsWithPrefix
的字符串中定义以“PREFIX”开头的内容。使用 String.StartsWith
方法检查 FileInfo
名称是否具有满足要求所需的已定义 前缀 。
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
string StartsWithPrefix = "ABC";
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod))
&& currFileInfo.Name.StartsWith(StartsWithPrefix))
{
currFileInfo.Delete();
}
}
https://docs.microsoft.com/en-us/dotnet/api/system.string.startswith?view=netcore-3.1
https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.name?view=netcore-3.1
使用 SQL Server 2014 \ VS 2019。我有下面的 C# 脚本任务来删除文件夹中超过 x 天的文件,这有效。
public void Main()
{
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod)))
{
currFileInfo.Delete();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
但是,我该如何修改,只是说,删除文件名以 'ABC' 开头的目录中的所有文件?
在类似于 StartsWithPrefix
的字符串中定义以“PREFIX”开头的内容。使用 String.StartsWith
方法检查 FileInfo
名称是否具有满足要求所需的已定义 前缀 。
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
string StartsWithPrefix = "ABC";
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod))
&& currFileInfo.Name.StartsWith(StartsWithPrefix))
{
currFileInfo.Delete();
}
}
https://docs.microsoft.com/en-us/dotnet/api/system.string.startswith?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.name?view=netcore-3.1