如何遍历文件夹,包括子文件夹和所有,拾取特定格式文件并使用他的foldername复制到其他位置 [英] How to loop through folders including subfolders and all , pickup perticular format file and copy to other location with his foldername

查看:138
本文介绍了如何遍历文件夹,包括子文件夹和所有,拾取特定格式文件并使用他的foldername复制到其他位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想循环浏览文件夹,只想选择js文件,然后我将mizize这些文件并使用相同的名称我想复制这个新的minb文件到其他位置,



如果文件在脚本文件夹中绑定,那么在目标文件夹脚本文件夹中应该创建并在里面创建这个新的min文件,如果文件在外面,然后在目标文件夹中只有min文件被转换。



我有缩小和所有的逻辑,但没有得到如何正确的子文件夹复制到我的目的地文件



到目前为止我试过这个用于复制目的



Hi ,

I want to loop through folder and wanted to pick only js files and then i will mizize those files and with same name i wanted to copy this new minb files to other location ,

If files conbtains inside script folder then in destination folder script folder should get created and inside that this new min file should get created , if the file is outside , then in destination folder only min file gets converted .

I have logic for minification and all , but not getting how to do proper subfolder copy to my destination files

I tried this so far for copying purpose

private void button1_Click(object sender, EventArgs e)
   {
       var folderDlg = new FolderBrowserDialog { ShowNewFolderButton = true };

       // Show the FolderBrowserDialog.
       var result = folderDlg.ShowDialog();
       if (result != DialogResult.OK) return;
       textBox1.Text = folderDlg.SelectedPath;
       var root = folderDlg.RootFolder;
   }

   private void button2_Click(object sender, EventArgs e)
   {
       var arr = Directory.GetFiles(textBox1.Text, "*.js", SearchOption.AllDirectories);
       int count = 0;
       foreach (string s in arr)
       {
           var FName = s.Split('\\');
           File.Copy(s, "C:\\Users\\Nilesh\\Desktop\\WebApplication1\\WebApplication1" + FName[FName.Length - 1]);
           count++;
       }
   }

推荐答案

从不使用Split开始 - 它可能无济于事。我这样做的方法是找出保存文件的根文件夹的位置,并使用String.Replace来更改它。例如:

Start off by not using Split - it probably won't help that much. The way I would do this is to work out where the "root folder" that holds the file is, and use String.Replace to change it. For example:
string basePath = @"D:\Temp\MyPicturesAreHere";
string newPath = @"D:\NewFolderForFiles";
...
string originalFile = @"D:\Temp\MyPicturesAreHere\A Folder\Another Folder\A File.js";
string newFile = originalFile.Replace(basePath, newPath);
Console.WriteLine(newFile);



打印:


Would print:

D:\NewFolderForFiles\A Folder\Another Folder\A File.js

您似乎正在寻找的是什么。

Which is what you seem to be looking for.


这篇关于如何遍历文件夹,包括子文件夹和所有,拾取特定格式文件并使用他的foldername复制到其他位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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