Exemple #1
0
        protected override void ExecuteMatchCore(
            FileMatch fileMatch,
            SearchContext context,
            string?baseDirectoryPath,
            ColumnWidths?columnWidths)
        {
            string indent = GetPathIndent(baseDirectoryPath);

            List <ReplaceItem> replaceItems = ReplaceHelpers.GetReplaceItems(
                fileMatch.NameMatch !,
                Options.ReplaceOptions,
                NameFilter !.Predicate,
                context.CancellationToken);

            string path    = fileMatch.Path;
            string newPath = ReplaceHelpers.GetNewPath(fileMatch, replaceItems);
            bool   changed = !string.Equals(path, newPath, StringComparison.Ordinal);

            if (Options.Interactive ||
                (!Options.OmitPath && changed))
            {
                LogHelpers.WritePath(
                    fileMatch,
                    replaceItems,
                    baseDirectoryPath,
                    relativePath: Options.DisplayRelativePath,
                    colors: Colors.Matched_Path,
                    matchColors: (Options.HighlightMatch) ? Colors.Match : default,
Exemple #2
0
        protected override void ExecuteMatch(
            FileMatch fileMatch,
            string directoryPath)
        {
            List <ReplaceItem> replaceItems = ReplaceHelpers.GetReplaceItems(
                fileMatch.NameMatch !,
                RenameOptions,
                NameFilter?.Predicate,
                CancellationToken);

            string path    = fileMatch.Path;
            string newPath = ReplaceHelpers.GetNewPath(fileMatch, replaceItems);

            ListCache <ReplaceItem> .Free(replaceItems);

            if (string.Equals(path, newPath, StringComparison.Ordinal))
            {
                return;
            }

            if (FileSystemHelpers.ContainsInvalidFileNameChars(newPath, FileSystemHelpers.GetFileNameIndex(path)))
            {
                Report(fileMatch, newPath, new IOException("New file name contains invalid characters."));
                return;
            }

            if (File.Exists(newPath))
            {
                if (ConflictResolution == ConflictResolution.Skip)
                {
                    return;
                }
                else if (ConflictResolution == ConflictResolution.Suffix)
                {
                    newPath = FileSystemHelpers.CreateNewFilePath(newPath);
                }
                else if (ConflictResolution == ConflictResolution.Ask)
                {
                    if (!AskToOverwrite(fileMatch, newPath))
                    {
                        return;
                    }
                }
            }

            var renamed = false;

            try
            {
                if (!DryRun)
                {
                    if (fileMatch.IsDirectory)
                    {
                        Directory.Move(path, newPath);
                    }
                    else
                    {
                        File.Move(path, newPath);
                    }

                    renamed = true;
                }

                Report(fileMatch, newPath);

                Telemetry.IncrementProcessedCount(fileMatch.IsDirectory);
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                Report(fileMatch, newPath, ex);
            }

            if (fileMatch.IsDirectory &&
                renamed)
            {
                OnDirectoryChanged(new DirectoryChangedEventArgs(path, newPath));
            }
        }