using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 更改檔名
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
string Serverpath = @"D:\測試資料匣";
DirectoryInfo di = new DirectoryInfo(Serverpath);
foreach (FileInfo fi in di.GetFiles())
{
//取得檔名,排除副檔名
string name = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
//去除檔名最後的2個字元
//若用DocuFreezer轉則此步驟可省略
name = name.Substring(0, name.Length - 2);
//取出檔名最後三個字
string fi_Extension = name.Substring(name.Length - 3);
//轉換為10進位數字
int aValue = Convert.ToInt16(fi_Extension);
//將10進位轉為16進位,不足3位數將前面補零,並將字母轉為大寫
//string aStr = Convert.ToString(aValue,16).PadLeft(3, '0').ToUpper();
//將10進位轉為36進位,不足3位數將前面補零
string aStr = ConvertTo36(aValue).PadLeft(3, '0');
//將aStr當作副檔名處理
string chgExtension = System.IO.Path.ChangeExtension(fi.FullName, aStr);
//直接更改檔名
fi.MoveTo(chgExtension);
//加入總檔案號後,重新組建新的檔案完整路徑
string numname = "00003093";
string newname_path = Path.GetDirectoryName(fi.FullName) +"\\"+ numname + "." + aStr;
// Move the file,最後變成的檔案放在後面.
File.Move(chgExtension,newname_path);
//紀錄
textBox1.Text += newname_path + "\r\n";
}
label1.Text = "檔名更改完成";
}
/// <summary>
/// 10進制轉36進制
/// </summary>
/// <param name="i">10進制值</param>
/// <returns>36進制值</returns>
public static string ConvertTo36(int i)
{
string s = "";
int j = 0;
while (i >= 36)
{
j = i % 36;
if (j < 10)
s += j.ToString();
else
s += Convert.ToChar(j + 87);
i = i / 36;
}
if (i < 10)
s += i.ToString();
else
s += Convert.ToChar(i + 87);
Char[] c = s.ToCharArray();
Array.Reverse(c);
return Convert.ToString(new string(c)).ToUpper();
}
}
}
沒有留言:
張貼留言