C# 当前目录- using System;
- using System.IO;
- using System.Text.RegularExpressions;
-
- class Program
- {
- static string workPath = Environment.CurrentDirectory;
- static string localtime = DateTime.Now.ToString("_MM-dd_HH-mm");
- static Regex dateRe = new Regex(@"(_\d{2}-\d{2}){2}");
-
- static void Main()
- {
- Console.WriteLine(workPath);
-
- foreach (string dir in Directory.GetDirectories(workPath, "_*"))
- {
- ChangeName(dir);
- }
-
- foreach (string file in Directory.GetFiles(workPath, "_*"))
- {
- ChangeName(file);
- }
- }
-
- static void ChangeName(string Name)
- {
- int splitIndex = Name.LastIndexOf("\\") + 1;
- string current = Name.Substring(splitIndex, Name.Length - splitIndex);
-
- if (dateRe.Match(current).Success)
- {
- try
- {
- Directory.Move(Name, Path.Combine(workPath, dateRe.Replace(current, localtime)));
- }
- catch{}
- }
- else
- {
- int Index = current.LastIndexOf(".");
- if (Index > 0 && File.Exists(Name))
- {
- try
- {
- Directory.Move(Name, Path.Combine(workPath, current.Insert(Index, localtime)));
- }
- catch{}
- }
- else
- {
- Directory.Move(Name, Name + localtime);
- }
- }
- }
- }
复制代码
|