如何递增所选文件? [英] How do I increment the selected files?

查看:74
本文介绍了如何递增所选文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

20170505001.jpg

20170505001R.tif

20170505001F.jpg



我想要的只是增加:

20170505002R.tif

20170505002F.jpg

20170505003R.tif

20170505003F.jpg



我不希望这个增加:20170505001.jpg



< b>我尝试了什么:



string [] f = System.IO.Directory.GetFiles(sourcePath);

foreach(f中的var srcPath)

{

FileInfo fileinfo = new FileInfo(srcPath);

string sExtension = Path.GetFileNameWithoutExtension(fileinfo .Name)。Last()。ToString();

string newFileName = fileName + sExtension + Path.GetExtension(fileinfo.Name);

string newFilePath = Path.Combine (directory,newFileName);

System.IO.File.Copy(srcPath,newFilePath,true);

}

I have:
20170505001.jpg
20170505001R.tif
20170505001F.jpg

What I want is only to increment:
20170505002R.tif
20170505002F.jpg
20170505003R.tif
20170505003F.jpg

I dont want this to increment: 20170505001.jpg

What I have tried:

string[] f = System.IO.Directory.GetFiles(sourcePath);
foreach (var srcPath in f)
{
FileInfo fileinfo = new FileInfo(srcPath);
string sExtension = Path.GetFileNameWithoutExtension(fileinfo.Name).Last().ToString();
string newFileName = fileName + sExtension + Path.GetExtension(fileinfo.Name);
string newFilePath = Path.Combine(directory, newFileName);
System.IO.File.Copy(srcPath, newFilePath, true);
}

解决方案

尝试



try

string[] files = { "20170505001.jpg", "20170505001R.tif", "20170505001F.jpg" };
           files = files.Where(k => char.IsLetter(Path.GetFileNameWithoutExtension(k).Last())).ToArray();  //"20170505001R.tif", "20170505001F.jpg"

           List<string> lst = new List<string>();
           foreach (string file in files)
           {
               string temp = Path.GetFileNameWithoutExtension(file);
               string lastChar = temp.Last().ToString();
                 temp = temp.Remove(temp.Length - 1);
                 decimal value = decimal.Parse(temp) + 1;
                 string increamentedFileName = string.Format("{0}{1}{2}", value, lastChar, Path.GetExtension(file));
                 lst.Add(increamentedFileName);
           }

           //"20170505002R.tif", "20170505002F.jpg"


为什么不呢如果您想要做的只是版本编号,请考虑修改文件的元数据?请参阅:[ ^ ]。



我假设创建具有递增名称的新文件不是问题。
Why not consider modifying the files' meta-data if all you want to do is version numbering ? See: [^].

I assume that creating new files with "incremented" names is not a problem.
private List<char> acceptCodes = new List<char>{'R','F'};

private List<string> GetFilesToIncrement(string directorypath)
{
    DirectoryInfo di = new DirectoryInfo(directorypath);

    return di.EnumerateFiles("*.*", SearchOption.AllDirectories)
        .Where(fi =>
            acceptCodes.Contains
            (Path.GetFileNameWithoutExtension
                (fi.Name).Last()))
                    .Select(fl => fl.Name)
                        .ToList();
}


这篇关于如何递增所选文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆