turrier.fr

Source : ct|22.09.09

< Tutorials Computer, Multimedia, Chinese

Rename a set of files with csharp or VB

Suppose we have in any directory (c:\\essai for example) a list of files (jpeg picture files for example), with any number, that we want to rename in a serie.

List of files

The number of files and name each of them being arbitrary, we assign the names:
vacances2009_ 1.jpg
vacances2009_ 2.jpg
vacances2009_ 3.jpg
etc…

With the Framework.net 2.0 and Sharpdevelop 2.2, create a C# console program called "RenommerListeFichier" for example. Place the following source code in Program.cs.

using System;
using System.IO;
namespace renommerliste
{
class Program
{
public static void Main(string[] args)
{
string[] noms;
string nom, nouveaunom, numero;
int i, n;
noms=Directory.GetFiles("c:\\essai");
n = 1 + noms.GetUpperBound(0);
for (i=0;i<n;i++)
{
numero=i.ToString();
nom = noms[i];
nouveaunom= string.Concat("c:\\essai\\vacances2009_",numero,".jpg");
File.Move(nom, nouveaunom);
}
Console.WriteLine ("Opération effectuée !");
Console.ReadKey(true);
}
} }

In VB, the equivalent code is as follows :

Imports System
Imports System.IO
Module Program
Sub Main()
Dim noms() as String
Dim nom, nouveaunom, numero As String
Dim i, n as Integer
noms=Directory.GetFiles("c:\\essai")
n = 1 + noms.GetUpperBound(0)
for i=0 to n-1
numero=i.ToString()
nom = noms(i)
nouveaunom = string.Concat("c:\\essai\\vacances2009_",numero,".jpg")
File.Move(nom, nouveaunom)
Next i
Console.WriteLine ("Opération effectuée !")
Console.ReadKey(true)
End Sub
End Module

Build the solution (F8) and then run the program.The jpg files, in the directory c:\\essai are then all automatically renamed.

Files renamed


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)