我需要创建一个SSIS脚本来从文件名中删除日期。例如文件名是:TestFile_122413.CSV我需要将其重命名为TestFile.CSV。我不知道如何保持文件扩展名以及如何处理文件中的日期更改。我每天都收到这个文件。这是我的代码:
`public void Main() // TODO:在这里添加您的代码
const string DIRECTORY_PATH = @"E:\ScriptsTest";
//const string FILE_NAME_TEMPLATE = "SSS_PROF_010113.CSV";
const string FILE_NAME_TEMPLATE = "*.CSV";
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
File.Move(filePath, filePath.Replace(FILE_NAME_TEMPLATE, FILE_NAME_TEMPLATE.Substring(0,8)));
}
}
}
}`
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个,它编译但我还没有运行它:
public class Foo
{
public void Main()
{
const string DIRECTORY_PATH = @"E:\ScriptsTest";
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
// Get the file name
string fileName = Path.GetFileName(filePath);
// Get the file extension
string fileExtension = Path.GetExtension(filePath);
// Get the file name without the date part
string fileTitle = fileName.Substring(0, fileName.IndexOf("_"));
File.Move(filePath, DIRECTORY_PATH + @"\" + fileTitle + "." + fileExtension);
}
}
}
}
}
答案 2 :(得分:0)
这应该有效。顺便说一句,您是否尝试过使用ForEach任务?这可能更简单。
public void Main()
{
const string DIRECTORY_PATH = @"C:\temp\";
const string FILE_NAME_TEMPLATE = "*_??????.CSV";
int underscoreAt = 0;
if (Directory.Exists(DIRECTORY_PATH))
{
string[] filePathList = Directory.GetFiles(DIRECTORY_PATH,FILE_NAME_TEMPLATE);
foreach (string filePath in filePathList)
{
if (File.Exists(filePath))
{
underscoreAt = filePath.LastIndexOf('_');
string newName = string.Format ("{0}.CSV", filePath.Substring(0, underscoreAt));
File.Move(filePath,newName );
}
}
}
}