如何解决SSIS脚本任务使用通配符删除文件
使用SQL Server 2014 \ VS2019。我具有以下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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。