当在同一目录中生成相似类型的文件时,如何通过文件名的最后一位数字识别文件 [英] How do I identify files by last digits of the filename when similar type of file is generating in the same directory
问题描述
我的目录中有6组文件,我想更改它们的名称:
文件名如下:
对于X公司:-
HD_654,
DT_654,
TD_654
对于公司Y:-
HD_231,
DT_231,
TD_231
这些数字654、231不同,并且在不同的日子可能不同.因此,我无法进行硬编码.但是总有一件事情对于X公司来说,所有数字都是654,而对于Y公司来说,所有数字都是231.
因此,我想将公司X的数字更改为HD_MMDDYYYY,DT_MMDDYYYY和TD_MMDDYYYY,并将公司Y的数字更改为HD_YYYYDDMM,DT_YYYYDDMM,TD_YYYYDDMM
6 set of files come in my directory and I want to change their name :
File names are like below :
for company X:-
HD_654,
DT_654,
TD_654
For company Y :-
HD_231,
DT_231,
TD_231
Those numbers 654, 231 are different and can be different for different days . So I was not be able to hardcode. But one thing always for company X the numbers are same like 654 for all and for company Y numbers are same like here 231 for all.
So I want to change the digits for company X as HD_MMDDYYYY,DT_MMDDYYYY and TD_MMDDYYYY and for company Y as HD_YYYYDDMM, DT_YYYYDDMM, TD_YYYYDDMM
推荐答案
我的想法是创建一个目标文件夹中的文件,该文件定义了HD_nnnn.txt文件中的公司代码与所需日期格式之间的映射:
My thought would be to create a file in the target folder that defines the mapping between the Company Code in the HD_nnnn.txt file and the required date formatting:
00098,MMDDYYYY
50,YYYYDDMM
这样,添加更多公司代码不需要修改程序代码...
这会将来自HD_nnnn.txt文件的公司代码作为字符串进行比较((即,050与50不同),并且假定该数据段周围没有多余的字符(并且带有制表符或逗号来将其与以下任何文本分隔).
因此,这是一个几乎完整的解决方案. (需要更多错误处理...)
That way adding more Company Codes doesn''t require modifying the program code...
This compares the Company Code from the HD_nnnn.txt files as strings (i.e., 050 is not the same as 50) and assumes no extraneous characters around that piece of data (and having a tab or comma to delimit it from any following text).
So here''s an almost complete solution. (more error handling required...)
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication23
{
class Program
{
const string FormatMapFilename = "FormatMap.txt";
static Dictionary<string, string> CompanyFormatMap = new Dictionary<string, string>();
static string WorkingPath = @"D:\Temp\"; // or get from the command line...
static void Main(string[] args)
{
if (!LoadFormatMap())
return; // We can't proceed without it
foreach (string hdFilename in Directory.GetFiles(Path.Combine(WorkingPath, "HD_*.txt")))
{
ProcessFileGroup(hdFilename);
}
}
static bool LoadFormatMap()
{
foreach (var line in File.ReadLines(Path.Combine(WorkingPath, FormatMapFilename)))
{
string[] mapping = line.Split('\t', ',');
if (mapping.Length >= 2)
{
CompanyFormatMap.Add(mapping[0], mapping[1]);
}
else
{
Console.Error.WriteLine("Invalid line in: " + FormatMapFilename);
return false;
}
}
return true;
}
static void ProcessFileGroup(string hdFilename)
{
string folder = Path.GetDirectoryName(hdFilename);
string extension = Path.GetExtension(hdFilename);
string fileDigits = Path.GetFileNameWithoutExtension(hdFilename).Split('_')[1];
string dateFormat = null;
using (var hdFile = new StreamReader(hdFilename))
{
string[] splitLine = hdFile.ReadLine().Split('\t', ',');
if (!CompanyFormatMap.TryGetValue(splitLine[0], out dateFormat))
{
Console.Error.WriteLine("Unrecognized company code in: " + hdFilename);
return;
}
}
string filedate = DateTime.Today.ToString(dateFormat);
int digitsCount = fileDigits.Length;
foreach (string filePath in Directory.GetFiles(Path.Combine(folder, "*_", fileDigits, extension)))
{
string filename = Path.GetFileNameWithoutExtension(filePath);
filename = filename.Substring(0, filename.Length - digitsCount) + filedate;
File.Move(filePath, Path.Combine(folder, filename, extension));
}
}
}
}
您可能会执行以下操作(但未编译):
You might do something like this (not compiled, though):
string GetSuffix(string filePath)
{
var match = Regex.Match(filePath, @"_(\d{1,7})
"); // 只接受位数少于所选日期格式的数字 如果(匹配成功)返回匹配.Groups[ 1 ].Value; 返回 空; } 无效 ReplaceSuffix(字符串 dirPath,字符串 oldSuffix ,字符串 newSuffix) { 如果(字符串.IsNullOrEmpty(oldSuffix))返回跨度>; 字符串 pattern = " span> + Regex.Escape(oldSuffix)+ "
"); // only take numbers with less digits than the choosen date format if (match.Success) return match.Groups[1].Value; return null; } void ReplaceSuffix(string dirPath, string oldSuffix, string newSuffix) { if (string.IsNullOrEmpty(oldSuffix)) return; string pattern = "^(.*)"+Regex.Escape(oldSuffix)+"
这篇关于当在同一目录中生成相似类型的文件时,如何通过文件名的最后一位数字识别文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!