因此,我有一个这样的目录结构:
parent/
└── sub1
| └── sub1.1
| | └── source
| | └── something1
| | └── something2
| |
| └── sub1.2
| └── source
| └── something3
| └── something4
└── sub2
└── sub2.1
| └── source
| └── something5
| └── something6
|
└── sub2.2
| └── source
| └── something7
| └── something8
|
└── sub2.3
└── source
└── something9
└── something10
我想将所有文件(文件名不同)从名为 source 的所有目录移动到其相对上级/父目录。因此,结果应该是这样的:
parent/
└── sub1
| └── sub1.1
| | └── something1
| | └── something2
| |
| └── sub1.2
| └── something3
| └── something4
└── sub2
└── sub2.1
| └── something5
| └── something6
|
└── sub2.2
| └── something7
| └── something8
|
└── sub2.3
└── something9
└── something10
有一个 Linux 示例,但我想要的是批处理版本。
编辑:谢谢,这应该可行。我最初有以下方法,但它只能处理一个子文件夹深度。
@echo off
for /D %%I in ("%~dp0*") do (
if exist "%%I\source\*" (
move /Y "%%I\source\*" "%%I\" 2>nul
rd "%%I\source" 2>nul
)
)
答案1
这是一个小型 C# 程序。
using System;
using System.IO;
namespace Move {
class Program {
static void Main(string[] args) {
Console.Write("Folder: ");
string path = Console.ReadLine();
foreach (var folders in Directory.GetDirectories(path)) {
Console.WriteLine(folders);
foreach (var subs in Directory.GetDirectories(folders)) {
Console.WriteLine($"\t{subs}");
foreach (var source in Directory.GetDirectories(subs)) {
Console.WriteLine($"\t\t{source}");
foreach (var item in Directory.GetFiles(source)) {
Console.WriteLine($"\t\t\t{item}");
string filename = Path.GetFileName(item);
string newPath = $@"{subs}\{filename}";
File.Move(item, newPath);
}
Directory.Delete(source);
}
}
}
Console.ReadLine();
}
}
}