Rename some files in a folder

前端 未结 6 807
遇见更好的自我
遇见更好的自我 2020-12-05 05:59

I have a task of changing the names of some files (that is, adding id to each name dynamically) in a folder using C#.

Example: help.txt to 1help.txt

How can

相关标签:
6条回答
  • 2020-12-05 06:38

    The function that you are looking for is File.Move(source, destination) of the System.IO namespace. Also take a look at the DirectoryInfo class (of the same namespace) to access the contents of the folder.

    0 讨论(0)
  • 2020-12-05 06:39

    Have a look at FileInfo.

    Do something like this:

    void RenameThem()
    {
        DirectoryInfo d = new DirectoryInfo("c:/dir/");
        FileInfo[] infos = d.GetFiles("*.myfiles");
        foreach(FileInfo f in infos)
        {
            // Do the renaming here
            File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:48

    You can use File.Move, like this:

    string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
    string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");
    
    File.Move(oldFilePath, newFilePath);
    
    0 讨论(0)
  • 2020-12-05 06:51

    I'll just dump this here since I needed to write this code for my own purposes.

    using System;
    using System.IO;
    
    public static class FileSystemInfoExtensions
    {
        public static void Rename(this FileSystemInfo item, string newName)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
    
            FileInfo fileInfo = item as FileInfo;
            if (fileInfo != null)
            {
                fileInfo.Rename(newName);
                return;
            }
    
            DirectoryInfo directoryInfo = item as DirectoryInfo;
            if (directoryInfo != null)
            {
                directoryInfo.Rename(newName);
                return;
            }
    
            throw new ArgumentException("Item", "Unexpected subclass of FileSystemInfo " + item.GetType());
        }
    
        public static void Rename(this FileInfo file, string newName)
        {
            // Validate arguments.
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            else if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            else if (newName.Length == 0)
            {
                throw new ArgumentException("The name is empty.", "newName");
            }
            else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0
                || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
            {
                throw new ArgumentException("The name contains path separators. The file would be moved.", "newName");
            }
    
            // Rename file.
            string newPath = Path.Combine(file.DirectoryName, newName);
            file.MoveTo(newPath);
        }
    
        public static void Rename(this DirectoryInfo directory, string newName)
        {
            // Validate arguments.
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }
            else if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            else if (newName.Length == 0)
            {
                throw new ArgumentException("The name is empty.", "newName");
            }
            else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0
                || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
            {
                throw new ArgumentException("The name contains path separators. The directory would be moved.", "newName");
            }
    
            // Rename directory.
            string newPath = Path.Combine(directory.Parent.FullName, newName);
            directory.MoveTo(newPath);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:52

    Check out How can I rename a file in C#?. I didn't know that C# doesn't have a rename... It seems you have to use System.IO.File.Move(oldFileName, newFileName)

    0 讨论(0)
  • 2020-12-05 06:57

    On .NET Framework 4.0 I use FileInfo.MoveTo() method that only takes 1 argument

    Just to move files my method looks like this

    private void Move(string sourceDirName, string destDirName)
    {
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        FileInfo[] files = null;
    
        files = dir.GetFiles();
    
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.MoveTo(temppath);
        }
    }
    

    to rename files my method looks like this

    private void Rename(string folderPath)
    {
       int fileCount = 0;
    
       DirectoryInfo dir = new DirectoryInfo(folderPath);
    
       files = dir.GetFiles();
    
       foreach (FileInfo file in files)
       {
           fileCount += 1;
           string newFileName = fileCount.ToString() + file.Name;
           string temppath = Path.Combine(folderPath, newFileName);
    
           file.MoveTo(temppath);
       }
    }
    

    AS you can see to Rename file it syntax is almost the same as to Move it, just need to modify the filename before using MoveTo() method.

    0 讨论(0)
提交回复
热议问题