如何在不覆盖现有文件的情况下复制文本文件? [英] How to a copy text file without overwrite existing file?

查看:112
本文介绍了如何在不覆盖现有文件的情况下复制文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本文件从一个位置复制到另一个位置而不覆盖现有文件,当我们将其复制到另一个位置(如file1.txt,file2.txt)时,应将其重命名为不同的文件名... Plz告诉我如何去做吧。我正在使用Windows应用程序c#.net。 Thanx提前...

解决方案

首先,检查文件是否存在 - 这是微不足道的:

  if (File.Exists(path))
{
...
}

查找下一个免费号码更复杂:

 ... 
string path = @ D:\ Temp \ myPic.jpg;
string pathOnly = path.Substring( 0 ,path.LastIndexOf(' \\'));
string [] files = Directory.GetFiles(pathOnly,Path.GetFileNameWithoutExtension(path)+ * + Path.GetExtension(path));

int x = files.Max(f = > GetFileNumber(f ));
...

private 正则表达式fileNumber = 正则表达式( \\d +


);
private int GetFileNumber( string file)
{
匹配m = fileNumber.Match(Path.GetFileNameWithoutExtension(file));
if (!m.Success) return 0 ;
return int .Parse(m.Value);
}

然后您可以使用File.Move重命名文件!





好的,我检查了那个链接及其如此复杂的代码。你能不能给我任何其他代码来轻松完成它?我需要重命名文件并在文件名中添加数字,如mytext1.txt,mytext2.txt.Plz帮助。







这一点都不复杂! :笑:

如果你是初学者,那么将来请说 - 它允许我们以不同的方式与你说话,让你缺乏经验。



代码非常简单,唯一复杂的部分是Regex(如果你还没有遇到它们,它们只是字符串在类固醇上找到并替换!)和使用Linq Max扩展的行和一个lambda表达式 - 暂时忽略它们。



如果你将代码视为黑盒子方法,你可以从你的代码中调用,那么它很漂亮简单地做你想做的事:只需调用方法GetFreeFileNumber,并指向你想要保存文件的路径。



整个代码并不复杂:

  private   void  Mybutton_Click( object  sender,EventArgs e)
{
string newFile = @ D:\ MyPic.jpg;
string destName = @ D:\\ \\Temp\MyPic.jpg;
if (File.Exists(destName))
{
string destNameWithNumber = GetFreeFileNumber(destName);
File.Move(destName,destNameWithNumber);
}
File.Copy(newFile,destName);
}

/// < 摘要 >
/// 查找字符串末尾的数字
/ // < / summary >
private 正则表达式fileNumber = 正则表达式( \\d +


,RegexOptions.Compiled);
/// < 摘要 >
/// 获取给定基本文件名的下一个免费文件编号
/// < span class =code-comment> < / summary >
/// < param name =路径 > 基本文件的路径< / PARAM >
/// < 返回 > 路径免费文件名< / returns >
private string GetFreeFileNumber( string path)
{
string pathOnly = path.Substring( 0 ,path.LastIndexOf(' \\')+ 1 );
string nameOnly = Path.GetFileNameWithoutExtension(path);
string \\ textOnly = Path.GetExtension(path);
string [] files = Directory.GetFiles(pathOnly,nameOnly + * + extOnly);
int largest = files.Max(f = > GetFileNumber(f));
return string .Format( {0} {1} {2} {3},pathOnly,nameOnly,maximum + 1 ,extOnly);
}
/// < 摘要 >
/// 获取文件名末尾的数字(如果有)
/ // < / summary >
/// < span class =code-summarycomment>< param name =file > < / param >
/// < span class =code-summarycomment>< 返回 > < / returns >
private int GetFileNumber( string 文件)
{
匹配m = fileNumber.Match(Path.GetFileNameWithoutExtension(file));
if (!m.Success) return 0 ;
return int .Parse(m.Value);
}



使用数字备份将D:\ MyPy.jpg复制到D:\ Temp \,这就是你要做的全部......


I want to copy a text file from one location to another without overwrite existing file and it should be renamed to different file name when we copy it to another location like file1.txt, file2.txt... Plz tell me how to do it. I am using windows application c#.net. Thanx in advance...

解决方案

First, check if the file exists - that's trivial:

if (File.Exists(path))
   {
   ...
   }

Finding the next free number is more complex:

    ...
    string path = @"D:\Temp\myPic.jpg";
    string pathOnly = path.Substring(0, path.LastIndexOf('\\'));
    string[] files = Directory.GetFiles(pathOnly, Path.GetFileNameWithoutExtension(path) + "*" + Path.GetExtension(path));

    int x = files.Max(f => GetFileNumber(f));
    ...

private Regex fileNumber = new Regex("\\d+


"); private int GetFileNumber(string file) { Match m = fileNumber.Match(Path.GetFileNameWithoutExtension(file)); if (!m.Success) return 0; return int.Parse(m.Value); }

You can then use File.Move to rename the file!


"Ok, i have checked that link and its so complicated code. Can you plz give me any other code to do it easily? I need to rename file and add number to filename like mytext1.txt, mytext2.txt. Plz help."



That isn't complicated at all! :laugh:
If you're a beginner, then in future please say - it allows us to talk differently to you, allowing for your lack of experience.

The code is pretty simple, the only complicated parts are the Regex (if you haven't met them, they are just string "find and replace" on steroids!) and the line using the Linq Max extension and a lambda expression - just ignore them both for the moment.

If you treat the code as a "black box" method you can call from your code, then it's pretty simple to do what you want: just call the method GetFreeFileNumber with the path to where you want to save the file.

The whole code is not complex:

private void Mybutton_Click(object sender, EventArgs e)
    {
    string newFile = @"D:\MyPic.jpg";
    string destName = @"D:\Temp\MyPic.jpg";
    if (File.Exists(destName))
        {
        string destNameWithNumber = GetFreeFileNumber(destName);
        File.Move(destName, destNameWithNumber);
        }
    File.Copy(newFile, destName);
    }

/// <summary>
/// Find the number at the end of the string
/// </summary>
private Regex fileNumber = new Regex("\\d+


", RegexOptions.Compiled); /// <summary> /// Get the next free file number given a base file name /// </summary> /// <param name="path">Path to base file</param> /// <returns>Path to free file name</returns> private string GetFreeFileNumber(string path) { string pathOnly = path.Substring(0, path.LastIndexOf('\\') + 1); string nameOnly = Path.GetFileNameWithoutExtension(path); string extOnly = Path.GetExtension(path); string[] files = Directory.GetFiles(pathOnly, nameOnly + "*" + extOnly); int largest = files.Max(f => GetFileNumber(f)); return string.Format("{0}{1}{2}{3}", pathOnly, nameOnly, largest + 1, extOnly); } /// <summary> /// Get the number (if any) at the end of a filename /// </summary> /// <param name="file"></param> /// <returns></returns> private int GetFileNumber(string file) { Match m = fileNumber.Match(Path.GetFileNameWithoutExtension(file)); if (!m.Success) return 0; return int.Parse(m.Value); }


To copy D:\MyPic.jpg to D:\Temp\ with numeric backups, that's all you have to do...


这篇关于如何在不覆盖现有文件的情况下复制文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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