如何在自动生成日期时间文件夹中复制我的文件夹和文件按钮单击事件 [英] How Can I Copy My Folders And Files In Auto Generate Date Time Folder On Button Click Event

查看:54
本文介绍了如何在自动生成日期时间文件夹中复制我的文件夹和文件按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (Directory.Exists(sourcedir))
                   {
                       // Copy folder structure
                       foreach (string sourceSubFolder in Directory.GetDirectories(sourcedir, "*", SearchOption.AllDirectories))
                       {
                           Directory.CreateDirectory(sourceSubFolder.Replace(sourcedir, targetdir));
                       }

                       //Copy Files Structure
                       foreach (string sourceFile in Directory.GetFiles(sourcedir, "*", SearchOption.AllDirectories))
                       {
                           string destinationFile = sourceFile.Replace(sourcedir, targetdir);
                           File.Copy(sourceFile, destinationFile);
                       }





如何为文件夹名称为日期时间的所有文件和文件夹创建一个文件夹...



how can i make one folder for all files and folder where folder name is date time...

推荐答案

var files = Directory.EnumerateFiles(path, "*.jpg", SearchOption.TopDirectoryOnly)
                     .Select(fn => new FileInfo(fn));
var fileDateGroups = files.GroupBy(fi => fi.LastWriteTime.Date);
foreach (var dateGroup in fileDateGroups)
{
    string dir = Path.Combine(@"C:\", dateGroup.Key.ToString("yyyyMMdd"));
    Directory.CreateDirectory(dir);
    foreach (var file in dateGroup)
    {
        string newPath = Path.Combine(dir, file.Name);
        File.Copy(file.FullName, newPath, true);
    }
}





编辑:如果你想搜索多个文件扩展名,你需要手动过滤它们:



If you want to search for multiple file extensions you need to filter them manually:

var allowed = new[]{ ".png", ".jpg" };
var files = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly)
                     .Where(fn => allowed.Contains(Path.GetExtension(fn)))
                     .Select(fn => new FileInfo(fn));





了解更多 http://stackoverflow.com/questions/13346775/c-get-all-files-from-a-specific-date-and-save -it-to-a-folder [ ^ ]


假设您要创建一个新的目录,您将其复制到完整的文件结构在另一个目录中,和您希望使用DateTime唯一标识符命名该新目录:
Assuming you want to create a new Directory into which you copy the complete file structure in another directory, and you want to name that new Directory with a DateTime unique identifier:
// the base folder path to put the copied directory into
private string newDirectoryFolderPath = @"C:\Users\YourComputerName\Desktop\";

// the source folder
private string sourcedir = @"C:\Users\YourComputerName\Desktop\SourceFolder";

// the directory path into which you will write the copy of the source folder
private string targetdir = @"C:\Users\YourComputerName\Desktop\TempFolder";

// button EventHandler to trigger copying directory
private void MakeDirectoryCopy_Click(object sender, EventArgs e)
{
    // your existing code for copying Directories and Files goes here

    DateTime now = DateTime.Now;

    string timeStamp = now.ToString("dd'-'MM'-'yyyy' 'HH'-'mm'-'ss' '") + now.Millisecond.ToString();

    Directory.Move(targetdir, newDirectoryFolderPath + timeStamp);
}

这里的策略是简单地使用时间戳重命名在复制过程中创建的临时文件夹,使用DateTime格式生成合法文件夹名称。



如果这是生产代码,我想我会在try-catch块中包装执行递归复制的代码以确保安全,并处理任何可能的错误。



如果您不关心复制的文件夹是否具有某些人类可读的文件名,您可以使用DateTime.Now的'Ticks属性来获取一个(希望)唯一的数字;如有必要,您可以将该数字解析回DateTime。

The strategy here is to simply re-name the temporary folder created in the copying process with a time-stamp, using a format of DateTime that results in a "legal" folder name.

If this were "production" code, I think I'd wrap the code that does the recursive copying in a try-catch block for safety, and handle any possible errors.

If you didn't care if the copied folders had somewhat "human readable" file names, you could use the 'Ticks property of DateTime.Now to get a (hopefully) unique number; and you could, when necessary, parse that number back into a DateTime.


这篇关于如何在自动生成日期时间文件夹中复制我的文件夹和文件按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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