如何将文件移动到另一个目录并将其文件名写入 SSIS 脚本任务的 C# 循环内的文件(创建和附加)?
How to move a file to another directory and write its filename to a file (create and append) within a loop in C# for SSIS Script Task?
下面是循环源目录中所有*.tar.gz文件的代码。每个文件都与变量 dayOld_7 和 dayOld_14 进行比较。如果相等,则将 dayOld_7 标志或 dayOld_14 标志设置为 True。如果文件不是其中之一,则必须将文件移动到另一个目录并将其文件名写入文件 ListOfMovedFiles。我该怎么做?我是 C# 的新手。
public void Main()
{
string dayOld_7 = DateTime.Now.AddDays(-7).ToString("yyyyMMdd") + ".tar";
string dayOld_14 = DateTime.Now.AddDays(-14).ToString("yyyyMMdd") + ".tar";
string directoryPath = Dts.Variables["User::directoryPath"].Value.ToString();
string[] sapFiles = System.IO.Directory.GetFiles(directoryPath, "*.tar.gz");
string currFile;
foreach (string fName in sapFiles)
{
FileInfo fNameInfo = new FileInfo(fName);
currFile = Path.GetFileNameWithoutExtension(fNameInfo.FullName).ToString();
if (currFile.Equals(dayOld_7))
{
Dts.Variables["User::Gzip_7"].Value = true;
}
else if (currFile.Equals(dayOld_14))
{
Dts.Variables["User::Gzip_14"].Value = true;
}
else if ( !(currFile.Equals(dayOld_7)) && !(currFile.Equals(dayOld_14)) )
{
//***What is the C# code to MOVE the file to a folder say C:/archive/ and write its filename to a file(create/append) ListOfMovedFiles.txt in the current directory?***
}
}
}
从命名空间开始:
using System.IO;
要移动文件,很简单:
File.Move(sourceFilepath, destinationFilepath);
要写入文件,taken from MS:
try
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
注意:您可以使用 CreateDirectory(dir) 创建一个目录,或者如果它已经存在,那么在命令中没有任何危害。
下面是循环源目录中所有*.tar.gz文件的代码。每个文件都与变量 dayOld_7 和 dayOld_14 进行比较。如果相等,则将 dayOld_7 标志或 dayOld_14 标志设置为 True。如果文件不是其中之一,则必须将文件移动到另一个目录并将其文件名写入文件 ListOfMovedFiles。我该怎么做?我是 C# 的新手。
public void Main()
{
string dayOld_7 = DateTime.Now.AddDays(-7).ToString("yyyyMMdd") + ".tar";
string dayOld_14 = DateTime.Now.AddDays(-14).ToString("yyyyMMdd") + ".tar";
string directoryPath = Dts.Variables["User::directoryPath"].Value.ToString();
string[] sapFiles = System.IO.Directory.GetFiles(directoryPath, "*.tar.gz");
string currFile;
foreach (string fName in sapFiles)
{
FileInfo fNameInfo = new FileInfo(fName);
currFile = Path.GetFileNameWithoutExtension(fNameInfo.FullName).ToString();
if (currFile.Equals(dayOld_7))
{
Dts.Variables["User::Gzip_7"].Value = true;
}
else if (currFile.Equals(dayOld_14))
{
Dts.Variables["User::Gzip_14"].Value = true;
}
else if ( !(currFile.Equals(dayOld_7)) && !(currFile.Equals(dayOld_14)) )
{
//***What is the C# code to MOVE the file to a folder say C:/archive/ and write its filename to a file(create/append) ListOfMovedFiles.txt in the current directory?***
}
}
}
从命名空间开始:
using System.IO;
要移动文件,很简单:
File.Move(sourceFilepath, destinationFilepath);
要写入文件,taken from MS:
try
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
注意:您可以使用 CreateDirectory(dir) 创建一个目录,或者如果它已经存在,那么在命令中没有任何危害。