Beispiel #1
0
        static bool InsertIntoTable( SqlServerFactory sq,  string INSERT_SQL, FileInfo fi, string location )
        {
            var path = Path.GetDirectoryName(fi.FullName) + @"\";
            var fileName = Path.GetFileNameWithoutExtension(fi.FullName);
            var dateCreated = fi.CreationTimeUtc.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
            var dateModified = fi.LastWriteTimeUtc.ToString("s", System.Globalization.CultureInfo.InvariantCulture);

            sq.SetSqlCommand(INSERT_SQL);
            sq.AddParm("@file",         SqlDbType.NChar, fileName);
            sq.AddParm("@extension",    SqlDbType.NChar, fi.Extension);
            sq.AddParm("@path",         SqlDbType.NChar, path);
            sq.AddParm("@location",     SqlDbType.NChar, location);
            sq.AddParm("@dateCreated",  SqlDbType.NChar, dateCreated);
            sq.AddParm("@dateModified", SqlDbType.NChar, dateModified);

            return sq.ExecuteNonQuery();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            CommandLineArgs.CmdLine cl = new CommandLineArgs.CmdLine();

            // Add as many header(n) lines as necessary for help.
            cl.ArgumentKeys.Add("header1", "Enumerate files and write file info into SQL Server database.");

            //// Add one line per argument key providing a short description.
            cl.ArgumentKeys.Add("-rootFolder", "Root folder at which to start file info collection.");
            cl.ArgumentKeys.Add("-logFile", "Fully qualified log file name (including extension.");
            cl.ArgumentKeys.Add("-clearDBFirst", "Clear all rows from target table first (true or false).");
            cl.ArgumentKeys.Add("-location", "General source location of data collection (ie, computer name).");
            cl.ArgumentKeys.Add("-DBLocation", "SQL Server CE DB location");

            cl.Initialize(args);

            string startingLocation = cl.GetArgumentValue( "-rootFolder", @"c:\" );
            string logFile = cl.GetArgumentValue("-logFile", @"C:\Users\roger\logfiles\file-list.csv" );
            bool clearDB = cl.GetArgumentValue( "-clearDBFirst", "false" ) == "true" ? true : false;
            string location = cl.GetArgumentValue("-location", "No location provided");
            string DBLocation = cl.GetArgumentValue("-DBLocation", @"C:\Users\roger\SQLServerCE");

            Console.WriteLine( "Starting at root folder: {0}", startingLocation );
            Console.WriteLine( "Writing log file at {0}", logFile );

            System.IO.StreamWriter file = new System.IO.StreamWriter(logFile);

            file.WriteLine( "File,Extension,Path,DateCreated,DateModified" );

            var DBPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            DBPath = DBLocation;
            var connectionString = String.Format( @"Data Source='{0}\FileCatalog.sdf'", DBPath );

            var sq = new SqlServerFactory( connectionString );
            sq.OpenConnection();

            if ( clearDB ) {
                sq.SetSqlCommand("delete from [files]");
                sq.ExecuteNonQuery();
            }

            var totalCounter = 0;
            var subCounter = 0;
            const int INCREMENTER = 500;

            const string INSERT_SQL =
                @"INSERT INTO [Files] (
                    [File],
                    [Extension],
                    [Path],
                    [Location],
                    [DateCreated],
                    [DateModified] )
                values (
                    @file,
                    @extension,
                    @path,
                    @location,
                    @dateCreated,
                    @dateModified )";

            DirectoryInfo diTop = new DirectoryInfo( startingLocation );
            try {
                foreach (var fi in diTop.EnumerateFiles()) {
                    try {
                        if ( ! InsertIntoTable(sq, INSERT_SQL, fi, location) ) {
                            file.WriteLine(CreateOutputLine(fi,location));
                        }
                        else {
                            totalCounter += 1;
                            subCounter +=1;
                        }
                    }
                    catch (UnauthorizedAccessException UnAuthTop) {
                        // file.WriteLine("{0}", fi.FullName);
                    }
                }

                foreach (var di in diTop.EnumerateDirectories("*")) {
                    try {
                        if ( ! di.Name.Contains("$RECYCLE.BIN")) {
                            foreach (var fi in di.EnumerateFiles("*", SearchOption.AllDirectories)) {
                                try {
                                    if (!InsertIntoTable(sq, INSERT_SQL, fi, location)) {
                                        file.WriteLine(CreateOutputLine(fi,location));
                                    }
                                    else {
                                        totalCounter += 1;
                                        subCounter += 1;
                                        if ( subCounter % INCREMENTER == 0 ) {
                                            subCounter = 0;
                                            Console.WriteLine( totalCounter.ToString( "#,##0" ));
                                        }
                                    }
                                }
                                catch (UnauthorizedAccessException UnAuthFile) {
                                }
                            }
                        }
                    }
                    catch (UnauthorizedAccessException UnAuthSubDir) {
                    }
                }
            }
            catch (DirectoryNotFoundException DirNotFound) {
            }
            catch (UnauthorizedAccessException UnAuthDir) {
            }
            catch (PathTooLongException LongPath) {
            }

            file.Close();
            sq.CloseConnection();
            Console.WriteLine( "Total = " + totalCounter.ToString("#,##0"));
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }