C# 如何用文件名的前几个字母创建文件夹?

C# 如何用文件名的前几个字母创建文件夹?,c#,file,directory,C#,File,Directory,因此,我检查了基本情况,但我想做以下工作: 我有5个文件,比如说:X1_word_date.pdf,XX1_word_date.pdf,等等 我想创建一个文件夹结构,如:C:\PATH\X1、C:\PATH\XX1等 那么,如何将文件名中“\u1”前面的第一个字母转换成字符串呢 我的想法是使用Directory.CreateDirectory,然后将主路径和字符串组合在一起,从而获得文件夹 我该怎么做?谢谢你的帮助 string fileName = "X1_word_date.pdf"; st

因此,我检查了基本情况,但我想做以下工作:

我有5个文件,比如说:X1_word_date.pdf,XX1_word_date.pdf,等等

我想创建一个文件夹结构,如:C:\PATH\X1、C:\PATH\XX1等

那么,如何将文件名中“\u1”前面的第一个字母转换成字符串呢

我的想法是使用Directory.CreateDirectory,然后将主路径和字符串组合在一起,从而获得文件夹

我该怎么做?谢谢你的帮助

string fileName = "X1_word_date.pdf";
string[] tokens = fileName.Split('_');
string myPath = "C:\\PATH\\";
Directory.CreateDirectory( myPath + tokens[0]); 
这样的办法应该行得通。使用
Split()


这样的办法应该行得通。使用
Split()


假设您的
文件
是一个
列表
,其中包含文件名(X2\u word\u date.pdf,…)

或者使用正则表达式

            string mainPath = @"C:\PATH";
            string[] filenames = new string[] { "X1_word_date.pdf", "X2_word_date.pdf" };
            foreach (string filename in filenames)
            {
                Match foldernameMatch = Regex.Match(filename, "^[^_]+");
                if (foldernameMatch.Success)
                    Directory.CreateDirectory(Path.Combine(mainPath, foldernameMatch.Value));
            }
或者使用正则表达式

            string mainPath = @"C:\PATH";
            string[] filenames = new string[] { "X1_word_date.pdf", "X2_word_date.pdf" };
            foreach (string filename in filenames)
            {
                Match foldernameMatch = Regex.Match(filename, "^[^_]+");
                if (foldernameMatch.Success)
                    Directory.CreateDirectory(Path.Combine(mainPath, foldernameMatch.Value));
            }

使用简单的字符串方法,如
Split
System.IO.Path
类:

var filesAndFolders = files
.Select(fn => new
{
    File = fn,
    Dir = Path.Combine(@"C:\PATH", Path.GetFileNameWithoutExtension(fn).Split('_')[0].Trim())
});
如果要创建该文件夹并添加文件:

foreach (var x in filesAndFolders)
{
    Directory.CreateDirectory(x.Dir); // will only create it if it doesn't exist yet
    string newFileName = Path.Combine(x.Dir, x.File);
    // we don't know the old path of the file so i can't show how to move
}

使用简单的字符串方法,如
Split
System.IO.Path
类:

var filesAndFolders = files
.Select(fn => new
{
    File = fn,
    Dir = Path.Combine(@"C:\PATH", Path.GetFileNameWithoutExtension(fn).Split('_')[0].Trim())
});
如果要创建该文件夹并添加文件:

foreach (var x in filesAndFolders)
{
    Directory.CreateDirectory(x.Dir); // will only create it if it doesn't exist yet
    string newFileName = Path.Combine(x.Dir, x.File);
    // we don't know the old path of the file so i can't show how to move
}

使用仅从源目录和目标目录开始的大图。
我们可以列出所有需要移动的文件。
在此列表中,我们首先用分隔文件名。
使用simple可以获得新的目录名。
将创建目录,除非它们已经存在。
要移动文件,我们需要它的目标路径,目标目录路径和文件名的组合

string sourceDirectory = @"";
string destDirectory = @"";

string[] filesToMove = Directory.GetFiles(sourceDirectory); 

foreach (var filePath in filesToMove) {
    var fileName = Path.GetFileName(filePath);
    var dirPath = Path.Combine(destDirectory, fileName.Split('_')[0]);
    var fileNewPath= Path.Combine(dirPath,fileName);
    Directory.CreateDirectory(dirPath);// If it exist it does nothing.
    File.Move(filePath, fileNewPath);
}

使用仅从源目录和目标目录开始的大图。
我们可以列出所有需要移动的文件。
在此列表中,我们首先用分隔文件名。
使用simple可以获得新的目录名。
将创建目录,除非它们已经存在。
要移动文件,我们需要它的目标路径,目标目录路径和文件名的组合

string sourceDirectory = @"";
string destDirectory = @"";

string[] filesToMove = Directory.GetFiles(sourceDirectory); 

foreach (var filePath in filesToMove) {
    var fileName = Path.GetFileName(filePath);
    var dirPath = Path.Combine(destDirectory, fileName.Split('_')[0]);
    var fileNewPath= Path.Combine(dirPath,fileName);
    Directory.CreateDirectory(dirPath);// If it exist it does nothing.
    File.Move(filePath, fileNewPath);
}


是否要将文件移动到这些文件夹中?您好,您的解决方案路径很好。你需要的是学习基本的字符串操作。检查本教程(特别是Split和IndexOf方法):如果是两位数怎么办?像X23一样?文件名中第一个
\uu
前面的第一部分是否与路径完全匹配?或者仅仅是数字?如果您想在uu之前做任何事情,您可以将字符串拆分为一个变量,并使用目录创建,就像您所说的,将目录的起始部分存储在一个变量中,然后在它后面添加新的direcotry。但是,在尝试创建目录之前,请检查目录是否存在。是否要将文件移动到这些文件夹中?您好,您找到了解决方案。你需要的是学习基本的字符串操作。检查本教程(特别是Split和IndexOf方法):如果是两位数怎么办?像X23一样?文件名中第一个
\uu
前面的第一部分是否与路径完全匹配?或者仅仅是数字?如果您想在uu之前做任何事情,您可以将字符串拆分为一个变量,并使用目录创建,就像您所说的,将目录的起始部分存储在一个变量中,然后在它后面添加新的direcotry。但在尝试创建目录之前,请检查目录是否存在,注意[0]@ThierryV:
String处的null。Split
永远不会返回null@ThierryV在什么条件下可以在[0]处得到null?啊,我看到了,它永远不会为null:)。Thanksbecareful在[0]@ThierryV:
String处为null。Split
永远不会返回null@ThierryV在什么条件下可以在[0]处得到null?啊,我看到了,它永远不会为null:)。感谢建议使用路径。合并而不是连接。这太棒了!谢谢,但是如果我事先不知道这些信呢?我已经试着用_word uu date.pdf替换“X1_word u date.pdf”,但它说:非法字符…建议使用路径。合并而不是连接。它成功了!谢谢,但是如果我事先不知道这些信呢?我已经试着用word Updf.pdf替换“X1_word Udate.pdf”,但它说:非法字符。。。