Example #1
0
    private bool DoInputNewDir(string fileName)
    {
        if (fileName == "" || fileName == ".")
        {
            fileName = Directory.GetCurrentDirectory();
        }
        fileName = Path.GetFullPath(fileName);
        if (!Directory.Exists(fileName))
        {
            mainForm.Log.WriteError("Move error", "No directory: " + fileName);
            mainForm.Log.Open();
            return(true);
        }
        mainForm.Dialogs.CloseInput();
        List <Node> filesAndDirs = GetFilesAndDirs(buffer.Controller);
        PathSet     newFullPaths = new PathSet();
        PathSet     oldPostfixed = new PathSet();

        foreach (Node nodeI in filesAndDirs)
        {
            if (!string.IsNullOrEmpty(renamePostfixed))
            {
                oldPostfixed.Add(nodeI.fullPath + renamePostfixed);
            }
        }
        foreach (Node nodeI in filesAndDirs)
        {
            if (!string.IsNullOrEmpty(renamePostfixed))
            {
                oldPostfixed.Remove(nodeI.fullPath);
            }
        }
        foreach (Node nodeI in filesAndDirs)
        {
            try
            {
                if (nodeI.type == NodeType.Directory)
                {
                    DirectoryMove(nodeI.fullPath, newFullPaths.Add(Path.Combine(fileName, Path.GetFileName(nodeI.fullPath))));
                }
                else if (nodeI.type == NodeType.File)
                {
                    FileMove(nodeI.fullPath, newFullPaths.Add(Path.Combine(fileName, Path.GetFileName(nodeI.fullPath))));
                }
                if (!string.IsNullOrEmpty(renamePostfixed) && oldPostfixed.Contains(nodeI.fullPath + renamePostfixed) &&
                    File.Exists(nodeI.fullPath + renamePostfixed))
                {
                    FileMove(nodeI.fullPath + renamePostfixed, Path.Combine(fileName, Path.GetFileName(nodeI.fullPath) + renamePostfixed));
                }
            }
            catch (IOException e)
            {
                mainForm.Log.WriteError("Move error", e.Message);
                mainForm.Log.Open();
            }
        }
        Reload();
        PutCursors(newFullPaths);
        return(true);
    }
Example #2
0
    private bool DoInputNewFileName(string newText)
    {
        if (string.IsNullOrEmpty(newText))
        {
            return(true);
        }
        string[]    newFileNames = newText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
        List <Node> filesAndDirs = GetFilesAndDirsHard(buffer.Controller);

        if (newFileNames.Length != filesAndDirs.Count)
        {
            return(true);
        }
        for (int i = 0; i < newFileNames.Length; i++)
        {
            if (filesAndDirs[i].fullPath == ".." || newFileNames[i] == "..")
            {
                return(true);
            }
            newFileNames[i] = newFileNames[i].TrimStart();
            if (string.IsNullOrEmpty(newFileNames[i]))
            {
                return(true);
            }
        }
        mainForm.Dialogs.CloseRename();
        PathSet oldPostfixed = new PathSet();
        PathSet newFullPaths = new PathSet();
        List <KeyValuePair <Node, string> > pairs = new List <KeyValuePair <Node, string> >();

        for (int i = 0; i < filesAndDirs.Count; i++)
        {
            Node   nodeI    = filesAndDirs[i];
            string fileName = newFileNames[i];
            pairs.Add(new KeyValuePair <Node, string>(nodeI, fileName));
            if (!string.IsNullOrEmpty(renamePostfixed))
            {
                oldPostfixed.Add(filesAndDirs[i].fullPath + renamePostfixed);
            }
        }
        foreach (KeyValuePair <Node, string> pair in pairs)
        {
            Node   nodeI    = pair.Key;
            string fileName = pair.Value;
            if (!string.IsNullOrEmpty(renamePostfixed))
            {
                oldPostfixed.Remove(nodeI.fullPath);
            }
        }
        List <List <KeyValuePair <Node, string> > > levels = new List <List <KeyValuePair <Node, string> > >();

        foreach (KeyValuePair <Node, string> pair in pairs)
        {
            int level = GetPartsLevel(pair);
            if (level >= levels.Count)
            {
                while (levels.Count < level + 1)
                {
                    levels.Add(null);
                }
            }
            if (levels[level] == null)
            {
                levels[level] = new List <KeyValuePair <Node, string> >();
            }
            levels[level].Add(pair);
        }
        for (int i = levels.Count; i-- > 0;)
        {
            List <KeyValuePair <Node, string> > pairsI = levels[i];
            if (pairsI == null)
            {
                continue;
            }
            foreach (KeyValuePair <Node, string> pair in pairsI)
            {
                Node   nodeI    = pair.Key;
                string fileName = pair.Value;
                if (nodeI.type == NodeType.File || nodeI.type == NodeType.Directory)
                {
                    try
                    {
                        if (nodeI.type == NodeType.File)
                        {
                            FileMove(nodeI.fullPath,
                                     newFullPaths.Add(Path.Combine(Path.GetDirectoryName(nodeI.fullPath), fileName)));
                        }
                        else if (nodeI.type == NodeType.Directory)
                        {
                            DirectoryMove(nodeI.fullPath,
                                          newFullPaths.AddDirectory(Path.Combine(Path.GetDirectoryName(nodeI.fullPath), fileName), nodeI.fullPath));
                        }
                        if (!string.IsNullOrEmpty(renamePostfixed) && oldPostfixed.Contains(nodeI.fullPath + renamePostfixed) &&
                            File.Exists(nodeI.fullPath + renamePostfixed))
                        {
                            FileMove(nodeI.fullPath + renamePostfixed,
                                     Path.Combine(Path.GetDirectoryName(nodeI.fullPath), fileName + renamePostfixed));
                        }
                    }
                    catch (IOException e)
                    {
                        mainForm.Log.WriteError("Rename error", e.Message);
                        mainForm.Log.Open();
                        break;
                    }
                }
            }
        }
        mainForm.UpdateAfterFileRenamed();
        Reload();
        PutCursors(newFullPaths);
        return(true);
    }