SSIS 小脚本 - 文件路径验证
2013-01-14 11:00
363 查看
之前项目中经常有文件的读取或者输出操作,其中最重要的就是在处理文件输入/输出之前验证文件的路径是否存在,如果不存在就输出错误.
上面的 User::IncomingFile 在传入Script Component之前通过变量表达式就已经将文件夹路径, 文件名路径拼写在一起形成一个完整的文件路径, 所以进来后直接去验证和处理.
有时如果在输出文件之时,文件的输入目录和文件夹地址并不是固定的,而是通过变量来维护的. 在文件输出之前需要检查下文件输出的的目录和文件夹是否存在, 是否能够构成一个有效的输出路径,因此需要这样来检查下.
/* Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using System.IO; namespace ST_TEST.csproj { [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { #region VSTA generated code enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; #endregion public void Main() { string sFilePath; string sPackagename; // Get the package name from SSIS Variables sPackagename = Dts.Variables["System::PackageName"].Value.ToString(); // Get the file path from SSIS Variables sFilePath = Dts.Variables["User::IncomingFile"].Value.ToString(); try { // Check for existence of file if ( !File.Exists( sFilePath ) ) { Dts.Events.FireError( 0, sPackagename, "File at file path: " + sFilePath + " does not exist", "", 0 ); } } catch ( System.Exception e ) { Dts.Events.FireError(0, sPackagename, "Exception occurred check for file at file path: " + sFilePath + " with error: " + e.Message.ToString(), "", 0); } } } }
上面的 User::IncomingFile 在传入Script Component之前通过变量表达式就已经将文件夹路径, 文件名路径拼写在一起形成一个完整的文件路径, 所以进来后直接去验证和处理.
有时如果在输出文件之时,文件的输入目录和文件夹地址并不是固定的,而是通过变量来维护的. 在文件输出之前需要检查下文件输出的的目录和文件夹是否存在, 是否能够构成一个有效的输出路径,因此需要这样来检查下.
string directory = Dts.Variables["User::OutgoingDirectory"].Value.ToString(); string folder = Dts.Variables["User::OutgoingFolder"].Value.ToString(); string folderPath = Path.Combine(directory,folder); if (!Directory.Exists(folderPath)) { Dts.Events.FireError(0,sPackageName,"Cannot find this folder path "+folderPath+" ","",0); return; }
相关文章推荐
- cmd脚本命令--之脚本文件自身路径信息
- SSIS 文件系统任务无法使用变量配置目标路径
- JS获取当前脚本文件的绝对路径
- shell脚本自动查找指定路径下包含某关键字的所有文件
- bash脚本筛选出根目录下所有ELF文件,并将路径输出
- 在python中,如何获取脚本文件所在路径-2
- 正则表达式文件路径验证js版
- python运行路径和脚本文件所在路径
- 解决:无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次
- SSIS 脚本任务读取文件
- PHP 获取 主机地址 和脚本文件的绝对路径
- linux shell脚本获得当前文件路径
- 获取python脚本文件所在路径
- 文件上传漏洞演示脚本之js验证
- JS魔法堂:获取当前脚本文件的绝对路径
- python中脚本所在的文件路径对执行脚本的影响
- 请问怎样书写SQL脚本,以判断某个路径下的某个文件是否存在?
- Cmake 脚本对项目输出路径和输出头文件的路径定义
- Python语言获取脚本文件所在路径
- java project 和 javaWeb的读取文件路径问题(整理自百度经验,有空验证下)