Circuit Negma

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

File Rename ver. 1

Posted by Circuit Negma on May 4, 2012


Created by: Hussein Nosair

getFiles1

I needed something quick and tailored to my files. I have created this tool. the File Rename tool scan the directory and sub directories first for all files, sort them and then rename the files to “XXXX.ext”. The “XXXX” starts from 0000 to 9999.

For now, this is it. However, I will progressly update the tool and add more features.

 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 getFiles1

 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:             //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.System;

 28:

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

 30:             this.folderBrowserDialog1.Description =

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

 32:

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

 34:             {

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

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

 37:                 count = dirFiles.Length;

 38:                 Array.Sort(dirFiles);

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

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

 41:                 textBox1.AppendText(Environment.NewLine);

 42:

 43:                 foreach (string file in dirFiles)

 44:                 {

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

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

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

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

 49:                     textBox1.AppendText(Environment.NewLine);

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

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

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

 53:                     tmpCount++;

 54:                 }

 55:             }

 56:         }

 57:

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

 59:         {

 60:             int tmpCount = 0;

 61:             string fldrPath;

 62:             string fileName;

 63:             string fileExt;

 64:             string newFileName;

 65:

 66:             if (dirFiles == null)

 67:             {

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

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

 70:                 progressBar1.ForeColor = Color.Red;

 71:                 progressBar1.Step = progressBar1.Maximum;

 72:                 progressBar1.Value = progressBar1.Maximum;

 73:                 return;

 74:             }

 75:

 76:             progressBar1.Minimum = 0;

 77:             progressBar1.Maximum = dirFiles.Length;

 78:             progressBar1.Step = 1;

 79:             progressBar1.ForeColor = Color.Green;

 80:             button2.Enabled = false;

 81:

 82:             foreach (string file in dirFiles)

 83:             {

 84:                 fldrPath = Path.GetDirectoryName(file);

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

 86:                 fileName = Path.GetFileNameWithoutExtension(file);

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

 88:                 fileExt = Path.GetExtension(file);

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

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

 91:                 newFileName = fldrPath + "\\" + tmpCount.ToString("D4") + fileExt;

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

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

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

 95:                 progressBar1.Value = tmpCount + 1;

 96:                 tmpCount++;

 97:             }

 98:

 99:             button2.Enabled = true;

 100:             //

 101:             // Clear all elements in the array.

 102:             //

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

 104:             dirFiles = null;

 105:             textBox1.ResetText();

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

 107:

 108:

 109:         }

 110:     }

 111: }

 

Download Link:

Name:

getFiles1.rar

Path:

/

Size:

5.9KB (6051 bytes)

Modified:

2012-05-04 12:36:57

Uploaded:

2012-05-04 12:36:57

md5:

6ebb2ef67a92142b6c268072158c984a

Shared:

Publicly Shared

URL:

http://www.adrive.com/public/WQpj9D/getFiles1.rar

Downloads:

0

Expires:

2012-05-18

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: