Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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++;
       }
   }
Posted

1 solution

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:
C#
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.
 
Share this answer
 
Comments
Torakami 1-Nov-15 0:25am    
thanks ,

below is what i have tried

private void btnMinimizer_Click(object sender, EventArgs e)
{
var arr = Directory.GetFiles(textBox1.Text, "*.js", SearchOption.AllDirectories);

if (arr.Length == 0)
{
MessageBox.Show(Constants.NoJsFilesAvailableMessage);
return;
}
foreach (var s in arr)
{
var fName = s.Split('\\');
var destinationPath = Helper.DestinationLocation;

var outputDirectory = destinationPath + fName[fName.Length - 2];

if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
string result = MiniFyJs(s, outputDirectory + "\\" + fName[fName.Length - 1]);
}
MessageBox.Show(Constants.JsConversionSucessMessage);
}

The only problem is , if the folder is presenbt inside one folder , i want that folder should get created inside that and then inside that compress files gets created .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900