Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

File Rename Rev 2

Posted by Circuit Negma on May 11, 2012


Created By: Hussein Nosair

File Rename Rev 2

Auto-calculate the padding zeros based on the number of files to be renamed in the target Directory/Folder.

   1: using System;

   2: using System.Collections.Generic;

   3: using System.ComponentModel;

   4: using System.Data;

   5: using System.Drawing;

   6: using System.Linq;

   7: using System.Text;

   8: using System.Windows.Forms;

   9: using System.IO;

  10:  

  11: namespace getFiles2

  12: {

  13:     public partial class Form1 : Form

  14:     {

  15:         private string[] dirFiles;

  16:         private int count;

  17:         //private string fileExt;

  18:  

  19:         public Form1()

  20:         {

  21:             InitializeComponent();

  22:         }

  23:  

  24:         private void button1_Click(object sender, EventArgs e)

  25:         {

  26:             int tmpCount = 1;

  27:             //int max;

  28:  

  29:             //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.System;

  30:  

  31:             // Set the help text description for the FolderBrowserDialog.

  32:             this.folderBrowserDialog1.Description =

  33:                "Select the directory pf Files that you want to rename.";

  34:  

  35:             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

  36:             {

  37:                 textBox1.ResetText();                               // Clear out all text in the textbox1

  38:                 dirFiles = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*" ,SearchOption.AllDirectories);

  39:                 count = dirFiles.Length;

  40:                 Array.Sort(dirFiles);

  41:                 textBox1.AppendText("Folder Location: " + folderBrowserDialog1.SelectedPath + Environment.NewLine);

  42:                 textBox1.AppendText("Total Files: " + count + Environment.NewLine);

  43:                 textBox1.AppendText(Environment.NewLine);

  44:  

  45:                 //max = Array.

  46:                 //System.Console.WriteLine(Array.IndexOf(dirFiles, "0020"));

  47:                 //System.Console.WriteLine(Array.IndexOf(dirFiles, "0020"));

  48:  

  49:                 foreach (string file in dirFiles)

  50:                 {

  51:                     textBox1.AppendText(tmpCount + "." + Environment.NewLine);

  52:                     textBox1.AppendText("FilePath = " + file + Environment.NewLine);

  53:                     textBox1.AppendText("FileName = " + Path.GetFileNameWithoutExtension(file) + Environment.NewLine);

  54:                     textBox1.AppendText("FileExt = " + Path.GetExtension(file).ToUpper() + Environment.NewLine);

  55:                     textBox1.AppendText(Environment.NewLine);

  56:                     //System.Console.WriteLine(Path.GetDirectoryName(file));

  57:                     //System.Console.WriteLine(Path.GetFileNameWithoutExtension(file));

  58:                     //System.Console.WriteLine(count.ToString().Count());

  59:                     tmpCount++;

  60:                 }

  61:             }

  62:         }

  63:  

  64:         private void button2_Click(object sender, EventArgs e)

  65:         {

  66:             int tmpCount = 0;

  67:             int iPadingZeros;

  68:             string fldrPath;

  69:             string fileName;

  70:             string fileExt;

  71:             string newFileName;

  72:  

  73:             if (dirFiles == null)

  74:             {

  75:                 //System.Console.WriteLine("error: File Directory is undefined.");

  76:                 textBox1.AppendText("Error: Files directory is undefined." + Environment.NewLine);

  77:                 this.Invalidate();

  78:                 this.SuspendLayout();

  79:                 progressBar1.ForeColor = Color.Red;

  80:                 this.Invalidate();

  81:                 this.Refresh();

  82:                 this.ResumeLayout();

  83:                 

  84:                 progressBar1.Step = progressBar1.Maximum;

  85:                 progressBar1.Value = progressBar1.Maximum;

  86:                 return;

  87:             }

  88:  

  89:             progressBar1.Minimum = 0;

  90:             progressBar1.Maximum = dirFiles.Length;

  91:             progressBar1.Step = 1;

  92:             progressBar1.ForeColor = Color.Green;

  93:             button2.Enabled = false;

  94:             iPadingZeros = count.ToString().Length;

  95:  

  96:             foreach (string file in dirFiles)

  97:             {

  98:                 fldrPath = Path.GetDirectoryName(file);

  99:                 //System.Console.WriteLine("File Location:" + fldrPath + Environment.NewLine);

 100:                 fileName = Path.GetFileNameWithoutExtension(file);

 101:                 //System.Console.WriteLine("File Name: " + fileName + Environment.NewLine);

 102:                 fileExt = Path.GetExtension(file);

 103:                 //System.Console.WriteLine("File Ext.: " + fileExt + Environment.NewLine);

 104:                 //System.Console.WriteLine("{0,22:D4} {0,22:X4}", tmpCount);

 105:                 newFileName = fldrPath + "\\" + tmpCount.ToString("D" + iPadingZeros.ToString()) + fileExt;

 106:                 //newFileName = fldrPath + @"\" + tmpCount.ToString("D4") + fileExt;    // Same result as in above statement.

 107:                 //System.Console.WriteLine(newFileName);

 108:                 System.IO.File.Move(file, newFileName);

 109:                 progressBar1.Value = tmpCount + 1;

 110:                 tmpCount++;

 111:             }

 112:  

 113:             button2.Enabled = true;

 114:             //

 115:             // Clear all elements in the array.

 116:             //

 117:             Array.Clear(dirFiles, 0, dirFiles.Length);

 118:             dirFiles = null;

 119:             textBox1.ResetText();

 120:             textBox1.AppendText("Message -> All Clear!" + Environment.NewLine);

 121:         }

 122:     }

 123: }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: