【问题标题】:Script to move files from folder to another folder using paths in text file使用文本文件中的路径将文件从文件夹移动到另一个文件夹的脚本
【发布时间】:2014-01-12 06:59:52
【问题描述】:

在 Windows 8 上,有人可以帮我创建一个脚本来将一些图像从特定文件夹移动到另一个文件夹吗?

列出我要从文件夹中移动的图像(不是所有图像)的文件路径列在此文件中:C:\Users\Emmanuel\Desktop\test.txt

包含我要删除的一些图像的文件夹出现在此文件夹中:

C:\Users\Computer\Desktop\Images1

我要将图像移动到的文件夹是这个文件夹:

C:\Users\Computer\Desktop\Images2

非常感谢您的帮助

【问题讨论】:

  • 说明您的操作系统。如果您使用的是 Windows,XCOPY 可能就是您想要的。
  • 我使用的是 Windows 8 :)

标签: windows file move


【解决方案1】:

试试这个,SourcesFile 是你的 test.txt,DestFolder 是目的地。

    public int Run()
    {
        if (!File.Exists(SourcesFile))
        {
            throw new ArgumentException("Source folder does not exist");
        }

        if (!Directory.Exists(DestFolder))
        {
            Console.WriteLine("Destination folder doesn't exist");
            Console.WriteLine("Creating destination folder...");
            Directory.CreateDirectory(DestFolder);
        }

        string[] files = File.ReadAllLines(SourcesFile);
        Console.WriteLine("Moving {0} files...", files.Length);
        foreach (string file in files)
        {
            string dest = Path.Combine(DestFolder, Path.GetFileName(file));
            if (File.Exists(dest))
            {
                string newFilename = string.Format("{0}_{1}{2}",
                    Path.GetFileNameWithoutExtension(file),
                    Guid.NewGuid().ToString("N"),
                    Path.GetExtension(file));

                string newDest = Path.Combine(DestFolder, newFilename);
                Console.WriteLine("File {0} already exists, copying file to {1}", file, newDest);
                File.Move(file, newDest);
                continue;
            }
            File.Move(file, dest);
        }
        return 0;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    相关资源
    最近更新 更多