有没有更好的方法将文件和文件夹移到上一级文件夹?

huangapple go评论207阅读模式
英文:

Is there a better way to move files and directory up a folder?

问题

根据标题,有没有更好的方法将所有文件和目录移到上一级文件夹?

这是我的代码:

private static void MoveEverythingUpAFolder(string sourceDir)
{
    foreach (var item in Directory.GetFiles(sourceDir))
    {
        Directory.Move(item, Path.Combine(Path.GetDirectoryName(item), "..", Path.GetFileName(item)));
    }

    foreach (var item in Directory.GetDirectories(sourceDir))
    {
        Directory.Move(item, Path.Combine(Directory.GetParent(item).FullName, "..", new DirectoryInfo(item).Name));
    }

    Directory.Delete(sourceDir);
}

如果仅使用Directory.Move(source, dest),会出现目标目录已存在的异常...

英文:

as per title, is there a better way to move all the files and directories up a folder?

This is my code now:

private static void MoveEverythingUpAFolder(string sourceDir)
{
    foreach (var item in Directory.GetFiles(sourceDir))
    {
        Directory.Move(item, Path.Combine(Path.GetDirectoryName(item), "..", Path.GetFileName(item)));
    }

    foreach (var item in Directory.GetDirectories(sourceDir))
    {
        Directory.Move(item, Path.Combine(Directory.GetParent(item).FullName, "..", new DirectoryInfo(item).Name));
    }

    Directory.Delete(sourceDir);
}

If I only use Directory.Move(source, dest) I get an exception saying that the destination directory already exists...

答案1

得分: 1

这可能发生在你的源目录中有一个与源目录名称匹配的子目录的情况下。

例如,你试图将 c:\foo\bar\*.* 移动到 c:\foo

然而,存在一个名为 c:\foo\bar\bar 的目录。

foo/bar/bar 不能移动到 foo 并变成 foo/bar,因为就像错误消息所说的那样,foo/bar 已经存在。

你会遇到问题。或者可能会出现异常。

你可以首先将源目录重命名为在该目录中不重复的名称,执行移动操作,然后删除该目录。

baz

英文:

This could happen if you have a directory inside your source directory, that matches the source directory name.

For example, you're trying to move c:\foo\bar\*.* to c:\foo.

However, a directory exists named c:\foo\bar\bar.

foo/bar/bar cannot be moved to foo and become foo/bar, because foo/bar exists, just like the errors says.

You would be FUBAR. Or perhaps experience an exception.

You could rename the source directory first to something NOT duplicated within that directory, perform your move operations, and then remove the directory.

baz

答案2

得分: 0

答案取决于是否已知某些约束条件。

Directory.Move(string, string) 方法的文档说明:

此方法使用由 destDirName 指定的名称创建一个新目录,并将 sourceDirName 的内容(包括文件和目录)移动到新创建的目标目录。然后删除 sourceDirName 目录。

如果尝试将目录移动到已经存在的目录中,将引发 IOException。

如果您有如下的目录结构,尝试将 a\b\c 移动到 a\b 将会因为 b 已经存在而失败。

b 在更改前仅包含 c

 a───b───c─┬─d
           └─f

但是如果您知道目录 b 将只包含目录 c,那么可以执行以下步骤:

  1. a\b\c 移动到 a\c-temp
  2. 删除 b(现在应该是空的)
  3. c-temp 重命名为 b

更改后

 a───b─┬─d
       └─f

如果目录 b 可能包含其他文件夹和文件,如下所示,则查找并移动各个文件夹和文件可能是最佳选择。

b 在更改前可能包含其他文件/文件夹

 a───b─┬─c─┬─d
       │   │
       └─e └─f
  1. c 中的文件夹和文件移动到 b(要注意可能会出现与已经存在于 b 中的文件/文件夹的名称冲突)
  2. 删除 c(现在应该是空的)

更改后

 a───b─┐
       ├─e
       ├─d
       └─f
英文:

The answer depends on whether certain things are known constraints or not.

The documentation for the Directory.Move(string, string) method states:

> This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName, including files and directories, to the newly created destination directory. It then deletes the sourceDirName directory.
>
> If you try to move a directory to a directory that already exists, an IOException will occur.

If you have a directory structure like the following, attempting to move a\b\c to a\b will fail with an error because b exists.

b will only contain c - before changes

 a───b───c─┬─d
           │
           └─f

But if you know that directory b will only ever contain directory c then you can

  1. move a\b\c to a\c-temp
  2. delete b (which should now be empty)
  3. rename c-temp to b

after

 a───b─┬─d
       │
       └─f

If directory b may contain other folders and files as in the following, then finding and moving the individual folders and files may be the best.

b may contain other files/folders - before changes

 a───b─┬─c─┬─d
       │   │
       └─e └─f
  1. move folders and files in c to b (be aware there could be name collisions with files/folders already present in b)
  2. delete c (which should now be empty)

after

 a───b─┐
       │
       ├─e
       │
       ├─d
       │
       └─f

huangapple
  • 本文由 发表于 2023年6月16日 07:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定