【问题标题】:Regex; moving file based on file name matching folder location正则表达式;根据文件名匹配文件夹位置移动文件
【发布时间】:2014-09-22 12:52:28
【问题描述】:

我了解如何使用正则表达式根据特定命名模式匹配文件名,我想将文件移动到目录中的特定文件夹中,该文件夹具有文件名匹配的详细信息。例如,

reportONE14073012300000.xls 

一般格式为:

(Filename\YYMMDD\HRS\MM\SS.xls)

此文件位于临时位置A:temp,但需要转到

 A:\Report\2014\ONE\July\30\1200\0030

一般格式为:

(A:\Report\2014\ONE\MM\DD\HH\MM)

【问题讨论】:

  • 你在哪里被打动了?还发布您当前的代码

标签: c# regex file move


【解决方案1】:

下面的sn-p会解析输入的文本,并将其转换为预期的路径。

string name = "reportONE14073012300000";
var match = Regex.Match(name, @"(?i)[a-z]+(\d{14})");
if(match.Success)
{
    DateTime dt = DateTime.ParseExact(match.Groups[1].Value,"yyMMddHHmmssff",CultureInfo.InvariantCulture);

    string path = string.Format(@"A:\Report\{0}\ONE\{1}\{2}\{3}\{4}",
    dt.ToString("yyyy"),
    dt.ToString("MMMM"),
    dt.ToString("dd"),
    dt.ToString("HH00"),
    dt.ToString("mm00"));
    Console.WriteLine(path);// A:\Report\2014\ONE\July\30\1200\0030
}

我把文件部分留给你自己,试试看。

【讨论】:

  • 在 dt.ToString 中应该不是 "YY" "MM" 并且在路径中我给它的目录文件夹 main 的路径应该是 @A:\report ???感谢所有帮助
  • 没有模式YYMM 将导致07 而不是July。是的路径,这是一个错字,我会更正它。
  • mscorlib.dll 中出现“System.FormatException”类型的未处理异常附加信息:日历 System.Globalization.GregorianCalendar 不支持字符串表示的 DateTime。
  • 哪一行会抛出这个异常?
  • DateTime dt = DateTime.ParseExact(match.Groups[1].Value,"yyMMddHHmmssff",CultureInfo.InvariantCulture);
【解决方案2】:

这样的事情应该对你有用。它没有经过测试,但是除了移动文件之外,它还应该注意解析文件名并创建整个存档目录树。

Archive() 方法返回一个布尔值,表示指定的文件是否被移动。

您可能需要调整正则表达式,因为它基于我对文件名格式的错误理解。

private static void ArchiveAll( DirectoryInfo dropDirectory , DirectoryInfo archiveRoot )
{
  foreach ( FileInfo file in dropDirectory.EnumerateFiles("*.xls") )
  {
    Archive( file , archiveRoot ) ;
  }
}

private static bool Archive( FileInfo file , DirectoryInfo archiveRoot )
{
  bool wasArchived = false ;
  Match m = rxFileNamePattern.Match(file.Name) ;
  if ( m.Success )
  {
    string   pfx   = m.Groups["prefix"].Value ;
    string   sfx   = m.Groups["suffix"].Value ;
    string   dtRaw = m.Groups["timestamp"].Value ;
    DateTime dt    = DateTime.ParseExact( dtRaw , "yyMMddHHmm" , CultureInfo.CurrentCulture ) ;
    string   path  = Path.Combine( "."       ,
                       pfx                   ,
                       dt.ToString( "yyyy" ) ,
                       sfx                   ,
                       dt.ToString( "MMMM" ) ,
                       dt.ToString( "dd"   ) ,
                       dt.ToString( "HH00" ) ,
                       dt.ToString( "00mm" )
                       ) ;
    DirectoryInfo archive = archiveRoot.CreateSubdirectory( path ) ;

    file.MoveTo( Path.Combine( archive.FullName , file.Name ) ) ;
    wasArchived = true ;
  }
  return wasArchived ;
}
const string fnPattern = @"
  ^                       # - start of text, followed by
  (?<prefix> \p{Ll}+) # - prefix    : 1 or more lowercase letters, followed by
  (?<suffix>    \p{Lu}+ ) # - suffix    : 1 or more uppercase letters, followed by
  (?<timestamp> \d{10}  ) # - timestamp : 10 decimal digits in the form YYMMDDhhmm, followed by
  (?<seconds>   \d{4}   ) # - seconds   : 4 decimal digits
  (?<ext>       \.xls   ) # - ext       :  the literal '.xls'
  $                       # - end of text.
  " ;
const RegexOptions fnPatternOptions = RegexOptions.IgnoreCase
                                    | RegexOptions.IgnorePatternWhitespace
                                    | RegexOptions.ExplicitCapture
                                    ;
static readonly Regex rxFileNamePattern = new Regex( fnPattern , fnPatternOptions ) ;

【讨论】:

  • 我必须声明一个主体,我试图创建一个源目录,但它不适合我。
猜你喜欢
  • 2018-10-23
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
相关资源
最近更新 更多