如何根据文件名移动所有文件? [英] How to move all file according to file's name?

查看:58
本文介绍了如何根据文件名移动所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应添加具有.csv的适当文件夹中文件的标记名称.



我有一个文件夹.
C:\\ Week
我的文件夹包括2个文件夹,例如A和B.
C:\\ Week \\ A C:\\ Week \\ B
而且每个文件夹都包含很多文件夹.
C:\\ Week \\ A \\ abc
并且"abc"文件夹包含一些文件.
C:\\ week \\ A \\ abc \\ abcxyz
我需要将子文件夹中的文件移动到另一个文件夹,该文件夹应具有与我的文件名相同的名称.

例如
abcxyz是文件夹.
我需要将abcxyz文件移动到abcxyz文件夹.
C \\ week \\ B \\ abcxyz


我需要比较名字.但是我不知道该怎么办.请帮帮我.
您可以检查我的代码吗?

谢谢.

查看到目前为止的OP代码:

The names of the files''s markers in appropriate folders that have .csv addition, should move.



i have one folder.
C:\\Week
My folder includes 2 folders like A and B.
C:\\Week\\A C:\\Week\\B
And each folder includes a lot of folders.
C:\\Week\\A\\abc
And "abc" folder includes some files.
C:\\week\\A\\abc\\abcxyz
I need to move my file in subfolder to another folder that folder should has a same name with my file name.

for example
abcxyz is folder.
i need to move my abcxyz file to abcxyz folder.
C\\week\\B\\abcxyz


i need to compare name. But i don''t know how to do. Please help me.
Can you check my code?

Thank you.

See OP''s code so far:

public void fileMove(string folder)
       {
           const string destPath = "C:\\Optima\\Target\\";
           string[] folders = Directory.GetDirectories(folder);
           string[] destFolders = Directory.GetDirectories(destPath);

           foreach (string fArray in folders)
           {                                                // fArray
               DirectoryInfo folderInfo = new DirectoryInfo(folder);
                         FileInfo[] files = folderInfo.GetFiles("*.csv");

                        DirectoryInfo destFolderInfo = new DirectoryInfo(destPath);
                   foreach(FileInfo file in files){
             string fileName = Path.GetFileNameWithoutExtension(file.Name);
               if (Directory.Exists(destPath+fileName)){

                   string destFolder = destPath + fileName;
                   File.Move(folder + file, destPath + fileName);

                       }

                   else{
                                           Directory.CreateDirectory(destPath +fileName);
                                       }
                   }

                    fileMove(folderInfo.FullName);


           }
       }
   }

推荐答案

使用System.IO.Path类,它有许多方法可以提取文件名的不同部分.
您还可以使用它从从源路径提取的零件中构造新路径(目标).
Use System.IO.Path class, it has many methods to extract different parts of file name.
You can also use it to construct new path (destination) from parts extracted form source path.


谢谢,我知道这个命令.

如果我的文件名是Selin.我需要移动Selin的文件夹.
如果我的文件名为x.我需要移动x的文件夹.

但是我有很多文件夹和文件.我必须使用循环.
请检查我的代码.
Thank you and i know this command.

if my file name is Selin. I need to move folder of Selin.
if my file name is x . I need to move folder of x.

But i have a lot of folder and file. I have to use loop.
Please check my code.
public static void fileMove(string folder)
       {
           string[] folders = Directory.GetDirectories(folder);
           foreach (string fArray in folders)
           {

               DirectoryInfo folderInfo = new DirectoryInfo(fArray);
               string[] files = Directory.GetFiles("*.csv", folderInfo.FullName);

              string[] destFolders = Directory.GetDirectories(@"C:\\Optima\\Target");

                foreach (string destFolderArray in destFolders)
               {
                    DirectoryInfo destFolderInfo = new DirectoryInfo(destFolderArray);

               }


               foreach( string file in files)
               {   FileInfo fileInfo = new FileInfo(file);

    }

               if (destFolderInfo.Name == fileInfo.Name){
                              File.Move(file, destFolder);
                                 }


               fileMove(folderInfo.FullName);

           }
       }
   }


这篇关于如何根据文件名移动所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆