2013-07-10 63 views
8

我要检查,看是否有文件从SSIS一个特定的文件夹存在。我怎样才能做到这一点?SSIS脚本任务,如果在文件夹中存在或不

+1

你可以用它作为你的出发点。 http://stackoverflow.com/questions/7385251/how-to-check-if-a-file-exists-in-a-folder –

+0

我使用下面的代码 Dts.Variables( “FILEEXISTS”)。价值= File.Exists(Dts.Variables(“FileLocation”)。值) 其中FILEEXISTS是布尔变量,“FileLocation”与文件路径 字符串变量虽然文件存在于文件夹中,它仍然是给假值。 – user1429135

+0

只是好奇,如果下面给出的解决方案对你有用,如果你能解决你的问题。您的反馈意见对我以及将来遇到类似问题的访问者都会有所帮助。谢谢。 –

回答

9

变量:

文件夹 - 字符串 - Visual C :: \ TEMP \

文件 - 字符串 - 1.txt的

FILEEXISTS - 布尔 - 假

public void Main() 
{ 
    string folder = Dts.Variables["User::folder"].Value.ToString();  //@"C:\temp\"; 
    string file = Dts.Variables["User::file"].Value.ToString();   //"a.txt"; 
    string fullPath = string.Format(@"{0}\{1}", folder, file); 

    Dts.Variables["User::fileExists"].Value = File.Exists(fullPath); 

    Dts.TaskResult = (int)ScriptResults.Success; 
} 
+1

我尝试了上面的答案,但在我们的环境中运行ssis时出现了错误。目标调用引发了异常。 – user2756589

+0

请确保将这些变量传递给脚本;文件夹和文件为ReadOnly,fileExists为ReadWrite –

1

作为替代具有“走出去”的变量,你也可以更改Dts.TaskResult基于文件是否存在。如果文件不存在,下面的代码片段将使脚本任务失败。 (它还创建一个日志条目,如果启用了日志记录。)

public void Main() 
{ 
    string fileName = Dts.Variables["User::sourcePath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString(); 

    if (File.Exists(fileName)) 
    { 
     Dts.TaskResult = (int)ScriptResults.Success; 
    } 
    else 
    { 
     Dts.Log(string.Format("File {0} was not found.",fileName),0,null); 
     Dts.TaskResult = (int)ScriptResults.Failure; 
    } 

} 
4

您可以使用Foreach Loop Container,只是所有的物品放到它。它将在文件存在时执行,如果不存在则执行。非常简单:)

+0

非常好!我正在寻找一些简单易用的工具,并且工作正常。 – Vladimir

相关问题