简体   繁体   中英

Rename files using regex

I have a set of files that are all in the same format somethingname___someotherstuff.txt

I want to get ride of the ___someotherstuff bit.

( ___ = 3 underscores)

It is a combination of letter and numbers

How do i do it?

EDIT: OS = Windows 7

EDIT AGAIN: the 'someotherstuff' bit is not the same in every file. A combination of different numbers and characters

EDIT ONCE AGAIN : somebody answered it and then withdrew it. Why? you had the right answer. _ \\w+ thanks. put it back if you like and I'll mark yours as the answer

You can do it roughly like this:

string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
    if (file.IndexOf("___") != -1)
    {
        File.Move(file, Regex.Replace(file, "___.*\.txt$", ""));
    }
}

以下正则表达式模式可能满足要求...

string newFileName = Regex.Replace(fileName, @"___\w*(?=\.)", "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM