利用ASP.NET,重命名Mp3文件、歌词文件为 {歌手名} - {歌曲名}.{后缀名}
2011-07-31 13:46 音乐让我说 阅读(767) 评论(0) 收藏 举报代码如下:
RenameMp3Files.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenameMp3Files.aspx.cs" Inherits="WebPutImagesTogether.RenameMp3Files" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>重命名mp3文件</title>
<style type="text/css">
.redBorder { border:solid 1px red; }
.greenBorder { border:solid 1px green; }
.marginLeft7em { margin-left: 7em; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>重命名mp3文件,比如把“周杰伦-七里香.mp3”或“周杰伦 - 七里香.mp3”为“周杰伦 - 七里香.mp3”</h2>
</div>
<div>
<asp:Label ID="lblMessage" runat="server" EnableViewState="false" />
</div>
<asp:PlaceHolder ID="phDealResult" runat="server" Visible="false">
<div class="greenBorder">
成功处理的文件夹目录为:<br /><br /><asp:Label ID="lblSuccess" runat="server" EnableViewState="false" />
</div>
<br />
<div class="redBorder">
处理失败的文件夹目录为:<br /><br /><asp:Label ID="lblError" runat="server" EnableViewState="false" />
</div>
</asp:PlaceHolder>
<div>
请输入要处理的Mp3文件夹目录:<asp:TextBox ID="txtSourceFolder" runat="server" Width="70%" /><br /><br />
<asp:Button ID="btnTest" runat="server" Text="开始测试" OnClick="btnTest_Click" />
<asp:Button ID="btnOk" runat="server" Text="开始处理" OnClick="btnOk_Click" />
</div>
</form>
</body>
</html>
RenameMp3Files.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace WebPutImagesTogether
{
public partial class RenameMp3Files : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 显示消息
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="rightFlag">是否是正确的消息,为 Null,表示不设置颜色。如果是,则消息的颜色为绿色,否则为红色</param>
protected void ShowMessage(string message, bool? rightFlag)
{
this.lblMessage.Text = message;
if (rightFlag.HasValue)
{
if (rightFlag.Value)
{
this.lblMessage.ForeColor = System.Drawing.Color.Green;
}
else
{
this.lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
else
{
this.lblMessage.ForeColor = System.Drawing.Color.Black;
}
}
/// <summary>
/// 开始测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnTest_Click(object sender, EventArgs e)
{
DealCommon(true);
}
/// <summary>
/// 开始处理
/// </summary>
/// <param name="isTest">是否是测试</param>
protected void DealCommon(bool isTest)
{
string sourceFolderPath = this.txtSourceFolder.Text;
if (!Directory.Exists(sourceFolderPath))
{
ShowMessage("要处理的Mp3文件夹目录不存在", false);
return;
}
StringBuilder messageAppend = new StringBuilder();
StringBuilder errorMessageAppend = new StringBuilder();
string[] fileTypes = new string[]{ ".mp3", ".lrc", ".wma", ".aac" }; // 常见音乐的后缀名
string[] conjunctionCharItems = new string[] { "-" }; // 歌手和歌名之间的连接符
string[] allFilePath = Directory.GetFiles(sourceFolderPath, "*.*", SearchOption.AllDirectories);
FileInfo fileItem;
string conjunctionChar; //连接符
int conjunctionCharIndex; //连接符的索引
string singer; //歌手
string musicName; //歌名
string currentDirectoryName; //当前处理的mp3文件夹所处的目录路径
string newFileNameWithoutExtension;//新文件的名称(不包含扩展名)
string newFileName; //新文件的名称
string newFilePath; //新文件的路径
foreach (string filePathItem in allFilePath)
{
fileItem = new FileInfo(filePathItem);
if (!fileTypes.Contains(fileItem.Extension, StringComparer.CurrentCultureIgnoreCase))
{
continue;
}
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileItem.FullName);
string fileExtension = Path.GetExtension(fileItem.FullName); //文件的扩展名
conjunctionChar = null; //连接符清空
conjunctionCharIndex = -1; //连接符的索引为 -1
foreach (string conjunctionCharItem in conjunctionCharItems)
{
conjunctionCharIndex = fileNameWithoutExtension.IndexOf(conjunctionCharItem);
if (conjunctionCharIndex > -1)
{
conjunctionChar = conjunctionCharItem; //保存这个连接符
break;
}
}
if (conjunctionChar == null)
{
errorMessageAppend.Append("该文件没有连接符,文件路径为:" + fileItem.FullName + "<br/>");
continue;
}
singer = fileNameWithoutExtension.Substring(0, conjunctionCharIndex).Trim();
musicName = fileNameWithoutExtension.Substring(conjunctionCharIndex + 1).Trim();
if (string.IsNullOrEmpty(singer))
{
errorMessageAppend.Append("该文件没有歌手名,文件路径为:" + fileItem.FullName + "<br/>");
continue;
}
if (string.IsNullOrEmpty(musicName))
{
errorMessageAppend.Append("该文件没有歌曲名,文件路径为:" + fileItem.FullName + "<br/>");
continue;
}
newFileNameWithoutExtension = singer + " " + conjunctionChar + " " + musicName;
newFileName = newFileNameWithoutExtension + fileExtension;
if (fileItem.Name.Equals(newFileName))
{
//相等,不需要处理
continue;
}
currentDirectoryName = Path.GetDirectoryName(fileItem.FullName);
newFilePath = currentDirectoryName + "\\" + newFileName;
if (File.Exists(newFilePath))
{
errorMessageAppend.Append("准备将 <span style=\"color:red;\">" + fileItem.FullName + "</span> 重命名为 <span style=\"color:red;\">" + newFilePath + "</span>,可是新文件已存在!" + "<br/>");
continue;
}
if (isTest)
{
messageAppend.Append("该文件可以重命名:" + fileItem.FullName + "<br/>");
}
else
{
File.Move(fileItem.FullName, newFilePath);
File.Delete(fileItem.FullName);
messageAppend.Append("该文件已重命名为:" + newFilePath + "<br/>");
}
}
this.phDealResult.Visible = true;
this.lblSuccess.Text = messageAppend.ToString();
this.lblError.Text = errorMessageAppend.ToString();
}
/// <summary>
/// 开始处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOk_Click(object sender, EventArgs e)
{
DealCommon(false);
}
}
}
效果图如下:

谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号