Rename folders in c# Form

て烟熏妆下的殇ゞ 提交于 2019-12-12 05:59:22

问题


i try to make a Form program in c# to rename a lot of folders. The Customers has to choose the folder (and the name to rename), where all the folders are include, for example in c:\folders\ are this folders:

1991 - title1
1992 - title2
1993 - title3
1994 - title4

And now i will change the year into a name, like this:

name - title1
name - title2
name - title3
name - title4

Example pic:

I hope you unterstand what i want :-) and you can help me with this.

Regards, matthias


回答1:


A few hints:

  1. Get all files (recursively if you want) with

    Directory.GetFiles(@"c:\folder\")

  2. Rename all files with

    File.Move(@"C:\folder\oldname", @"C:\folder\newname");

If your file format is like that you can simply split the filename at "-" & replace the first part with your name.




回答2:


You can use the FolderBrowserDialog class to select the directory, the Directory class to rename it and String.Split or a RegEx to modify the name.

Store the selected directories and their names to a collection of your choice.




回答3:


The Directory Class will do this.

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

You should also take a look at Path to help figure out what to replace.

http://msdn.microsoft.com/en-us/library/system.io.path_members(v=VS.71).aspx

// Move the directory.
Directory.Move(path, target);



回答4:


Use others answers and for renaming i would do a RegEx replace like this.

string new_folder_name = Regex.Replace(/*old folder name*/, @"\d\d\d\d", /*user provided name*/)



回答5:


In the command prompt the following

for /L %n in (1991,1,2010) do ren "(%n) - *" "name - *"

should do what you want.



来源:https://stackoverflow.com/questions/4907572/rename-folders-in-c-sharp-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!