Wednesday, June 20, 2012

SSIS Q001- How to get file names in a specified directory and its sub directory

Question - How to get the file name list from a directory and sub-directory ?

We can use script component in SSIS make a simple test.


public void Main()
        {
            string[] fileNames = Directory.GetFiles(@"D:\","*.*",SearchOption.AllDirectories);

            foreach (string filename in fileNames)
            {
                MessageBox.Show(filename);
            }
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }

string[] fileNames = Directory.GetFiles(@"D:\","*.*",SearchOption.AllDirectories);


It gets all files under disk D including the sub directories. The file type can be any type - *.*. Of course, we change the filter of file type to be '*.txt' or '*.doc'. 


Run package,sometimes we will get an error 




See error information from 'Execute Results'



Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.UnauthorizedAccessException: Access to the path 'D:\System Volume Information' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
   at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
   at ST_d1ce901e2c294acd880507ab2aac6c74.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()



The key information is - Access to the path 'D:\System Volume Information' is denied.


We can search this information by Google,you can find many questions and solutions,but I just want to try my way. I think this hidden directory we can not access just because I didn't get the administration right to run this package. 


There're some system directories we really needn't to read,so if we want to load files from a directory and its sub-directories,we at least need to create a directory under the disk root.  



  public void Main()
        {
            string[] fileNames = Directory.GetFiles(@"D:\software\","*.*",SearchOption.AllDirectories);

            foreach (string filename in fileNames)
            {
                MessageBox.Show(filename);
            }
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }

Test it, all files are shown in message box. 

1 comment: