예제 #1
0
        // Set the static properties based on the controls.
        private void ApplyTextSelection()
        {
            bool      wasOn = TextFilterOn;
            MatchType matchType;

            _includeChecked = chkContain.Checked && !string.IsNullOrEmpty(txtContains.Text);
            _includeText    = txtContains.Text;

            _excludeChecked = chkDoesNotContain.Checked && !string.IsNullOrEmpty(txtDoesNotContain.Text);
            _excludeText    = txtDoesNotContain.Text;

            _caseChecked  = chkCase.Checked;
            _wildChecked  = radWildcard.Checked;
            _regexChecked = radRegex.Checked;

            // TODO: radNormal.Checked?

            if (_regexChecked)
            {
                matchType = MatchType.RegularExpression;
            }
            else if (_wildChecked)
            {
                matchType = MatchType.Wildcard;
            }
            else
            {
                matchType = MatchType.Simple;
            }

            if (_includeChecked)
            {
                _includeMatcher = new StringMatcher(_includeText, chkCase.Checked, matchType);
            }

            if (_excludeChecked)
            {
                _excludeMatcher = new StringMatcher(_excludeText, chkCase.Checked, matchType);
            }

            if (TextFilterOn != wasOn)
            {
                OnTextFilterOnOff(this);
            }
        }
예제 #2
0
        public string MakeReady()
        {
            // Set MustMatch and MustNotMatch to use for testing strings.
            // Set them even if not Enabled in order to verify regular expressions are valid.

            try
            {
                if (ContainsText == null)
                {
                    MustMatch = null;
                }
                else
                {
                    MustMatch = new StringMatcher(ContainsText, MatchCase, MatchType);
                }

                if (LacksText == null)
                {
                    MustNotMatch = null;
                }
                else
                {
                    MustNotMatch = new StringMatcher(LacksText, MatchCase, MatchType);
                }

                Debug.Assert(!Enabled || MustMatch != null || MustNotMatch != null);
            }
            catch (Exception ex)
            {
                Enabled = false;
                string msg = "An error occurred in rule \"{0}\": {1}";
                return(string.Format(msg, Name, ex.Message));
            }

            return(null);
        }
예제 #3
0
        public static List <string> DeleteMany(string fileSpec, string folderSpec, DateTime minTimestamp, DateTime maxTimestamp, long minSize, long maxSize, bool tx1Files, bool txtFiles, bool deleteEmptyFolderAndParents, bool listOnly, Logger Log, WindowsIdentity userToImpersonate = null)
        {
            using (Log.InfoCall())
            {
                var result = new List <string>();

                try
                {
                    Log.Info("fileSpec = ", fileSpec);
                    Log.Info("folderSpec = ", folderSpec);
                    Log.Info("minTimestamp = ", minTimestamp);
                    Log.Info("maxTimestamp = ", maxTimestamp);
                    Log.Info("minSize = ", minSize);
                    Log.Info("maxSize = ", maxSize);
                    Log.Info("tx1Files = ", tx1Files);
                    Log.Info("txtFiles = ", txtFiles);
                    Log.Info("deleteEmptyFolderAndParents = ", deleteEmptyFolderAndParents);
                    Log.Info("listOnly = ", listOnly);

                    // Create a list of all distinct folder names from the two history files.

                    IEnumerable <string> folderList = null;

                    // Check for the presence of wild-cards.

                    if (folderSpec.IndexOfAny(new char[] { '*', '?' }) == -1)
                    {
                        // No wild-cards so just use the folder name as-is even if it isn't listed in the history files.
                        folderList = new string[] { folderSpec };
                    }
                    else
                    {
                        // The wild-carded folder spec is applied to the list of
                        // folders in the history files so start by getting that list.

                        folderList = GetHistoricalFolderNames(Log);

                        if (folderSpec != "*")
                        {
                            // Since we're not simply processing all known folders we'll use the
                            // StringMatcher class in Wildcard mode to find the matching folders. In
                            // wildcard syntax, the '\' char is an escape char. However there is no
                            // legitimate use for escape chars in the folder spec since the only
                            // escapable characters are '*', '?', and '\' which are not valid in
                            // folder (or file) names. Thus, any '\' chars present must be subdir
                            // separators which must be escaped to prevent StringMatcher from
                            // treating them as escape chars and discarding them.

                            folderSpec = folderSpec.Replace("\\", "\\\\");
                            var matcher = new StringMatcher(folderSpec, false, MatchType.Wildcard);
                            folderList = folderList.Where(f => matcher.Matches(f));
                        }
                    }

                    foreach (string folder in folderList)
                    {
                        // Search for .txt and/or .tx1 files matching the fileSpec and delete them.  If folder becomes empty, delete it.

                        DirectoryInfo dirInfo = null;

                        try
                        {
                            dirInfo = new DirectoryInfo(folder);
                        }
                        catch
                        {
                            // Invalid path.
                            dirInfo = null;
                        }

                        if (dirInfo != null && dirInfo.Exists)
                        {
                            List <FileInfo> filesInFolder = new List <FileInfo>();
                            bool            leftSome      = false;

                            if (tx1Files)
                            {
                                filesInFolder.AddRange(dirInfo.GetFiles(fileSpec + ".tx1"));
                            }
                            if (txtFiles)
                            {
                                filesInFolder.AddRange(dirInfo.GetFiles(fileSpec + ".txt"));
                            }

                            foreach (FileInfo file in filesInFolder)
                            {
                                // For compatibility with the old DOS 8.3 file name format,
                                // GetFiles("*.tx1") will return files whose extension STARTS WITH
                                // ".tx1", such as "x.tx123". To exclude such files, verify we have a
                                // 4 character extension (includes the '.') in addition to checking
                                // the other criteria.

                                if (file.Extension.Length == 4 &&
                                    file.LastWriteTime >= minTimestamp && file.LastWriteTime <= maxTimestamp &&
                                    file.Length >= minSize && file.Length <= maxSize)
                                {
                                    if (listOnly)
                                    {
                                        result.Add(file.FullName);
                                    }
                                    else
                                    {
                                        using (userToImpersonate?.Impersonate())
                                        {
                                            TryDeleteFile(Log, result, file);
                                        }
                                    }
                                }
                                else
                                {
                                    leftSome = true;
                                }
                            }

                            if (!listOnly && !leftSome)
                            {
                                using (userToImpersonate?.Impersonate())
                                {
                                    TryDeleteFolder(dirInfo, Log);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Exception in DeleteMany(): ", ex);
                    throw;
                }

                return(result);
            }
        }