在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
代码如下: Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebPutImagesTogether._Default" %> <!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>把多个图片文件夹归类,归类到一起</title> <style type="text/css"> .redBorder { border:solid 1px red; } .greenBorder { border:solid 1px green; } .marginLeft7em { margin-left: 7em; } </style> </head> <body> <form > <div> <h1>把多个图片文件夹归类,归类到一起</h1> </div> <div> <a href="RenameFiles.aspx" target="_blank">把文件夹“Root”下的所有文件夹“Parent”下的文件重命名为“ParentXXX”</a> </div> <div> <asp:Label ID="lblMessage" runat="server" /> </div> <asp:PlaceHolder ID="phDealResult" runat="server" Visible="false"> <div class="greenBorder"> 成功处理的文件夹目录为:<br /><br /><asp:Label ID="lblSuccess" runat="server" /> </div> <div class="redBorder"> 处理失败的文件夹目录为:<br /><br /><asp:Label ID="lblError" runat="server" /> </div> </asp:PlaceHolder> <div> 请输入要处理的图片文件夹目录:<asp:TextBox ID="txtSourceFolder" runat="server" Width="70%" /><br /><br /> 请输入<span class="marginLeft7em"></span>输出目录:<asp:TextBox ID="txtDestinationFolder" 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>
Default.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 _Default : 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; string destinationFolderPath = this.txtDestinationFolder.Text; if (!Directory.Exists(sourceFolderPath)) { ShowMessage("要处理的图片文件夹目录不存在", false); return; } if (!Directory.Exists(destinationFolderPath)) { ShowMessage("输出目录不存在", false); return; } if (destinationFolderPath[destinationFolderPath.Length - 1] != '\\') { destinationFolderPath += '\\'; } DirectoryInfo destinationFolder = new DirectoryInfo(destinationFolderPath); string[] sourceAllChildFolders = Directory.GetDirectories(sourceFolderPath); string folderSuffixName = "_files"; string[] sourceRightChildFolders = Directory.GetDirectories(sourceFolderPath, "*" + folderSuffixName, SearchOption.TopDirectoryOnly); StringBuilder messageAppend = new StringBuilder(); StringBuilder errorMessageAppend = new StringBuilder(); DirectoryInfo currentDir; string trueFolderName; foreach (string item in sourceRightChildFolders) { currentDir = new DirectoryInfo(item); trueFolderName = currentDir.Name.Substring(0, currentDir.Name.Length - folderSuffixName.Length); string endWithNumRegex = @"[1-9]\d*$"; //以数字结尾 if (Regex.IsMatch(trueFolderName, endWithNumRegex)) { trueFolderName = Regex.Replace(trueFolderName, endWithNumRegex, string.Empty); } if (isTest) { messageAppend.Append(trueFolderName + "<br/>"); } else { FileInfo[] imgFiles = currentDir.GetFiles("*.jpg"); string newImgFilePath; foreach (FileInfo imgFileItem in imgFiles) { newImgFilePath = destinationFolder.FullName + trueFolderName + imgFileItem.Name; if (!File.Exists(newImgFilePath)) { imgFileItem.CopyTo(newImgFilePath); } else { errorMessageAppend.Append("准备复制时,可目标文件已经存在!文件路径:" + newImgFilePath + "<br/>"); } } } } foreach (string item in sourceAllChildFolders) { if (!sourceRightChildFolders.Contains(item)) { errorMessageAppend.Append("无法处理这样的文件夹:" + item + "<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); } } }
谢谢浏览! |
请发表评论