You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过程序或脚本提取文件系统新增的目录与文件?

提取目录A新增内容生成目录C的实现方案

以下分别用PowerShell、Python、Java三种工具实现需求:从目录A中提取出相对于目录B的新增文件(含对应目录结构),生成目录C,并清理C中的空目录。

PowerShell 实现

  1. 定义三个目录的路径(根据实际路径修改):
$repoRoot = "C:\path\to\repository"
$folderA = Join-Path $repoRoot "folderA"
$folderB = Join-Path $repoRoot "folderB"
$folderC = Join-Path $repoRoot "folderC"
  1. 找出Folder A中存在但Folder B中不存在的文件,复制到Folder C对应路径:
Get-ChildItem -Path $folderA -Recurse -File | ForEach-Object {
    $relativePath = $_.FullName.Substring($folderA.Length + 1)
    $bFilePath = Join-Path $folderB $relativePath
    if (-not (Test-Path $bFilePath)) {
        $cFilePath = Join-Path $folderC $relativePath
        New-Item -ItemType Directory -Path (Split-Path $cFilePath -Parent) -Force | Out-Null
        Copy-Item -Path $_.FullName -Destination $cFilePath
    }
}
  1. 清理Folder C中的空目录(递归删除,从最深层开始):
Get-ChildItem -Path $folderC -Recurse -Directory | Sort-Object FullName -Descending | ForEach-Object {
    if (-not (Get-ChildItem -Path $_.FullName -Force)) {
        Remove-Item -Path $_.FullName
    }
}

Python 实现

依赖Python标准库os和shutil,无需额外安装:

import os
import shutil

def copy_new_files(src_a, src_b, dest_c):
    # 遍历Folder A的所有文件
    for root, dirs, files in os.walk(src_a):
        # 计算当前目录相对于Folder A的路径
        relative_path = os.path.relpath(root, src_a)
        # 对应Folder B的目录路径
        b_dir = os.path.join(src_b, relative_path)
        # 对应Folder C的目录路径
        c_dir = os.path.join(dest_c, relative_path)
        
        for file in files:
            a_file = os.path.join(root, file)
            b_file = os.path.join(b_dir, file)
            # 如果Folder B中没有该文件,则复制到Folder C
            if not os.path.exists(b_file):
                os.makedirs(c_dir, exist_ok=True)
                shutil.copy2(a_file, os.path.join(c_dir, file))

def delete_empty_dirs(root_dir):
    # 递归删除空目录,从子目录开始
    for root, dirs, files in os.walk(root_dir, topdown=False):
        for dir in dirs:
            dir_path = os.path.join(root, dir)
            if not os.listdir(dir_path):
                os.rmdir(dir_path)

# 替换为实际路径
folder_a = r"C:\path\to\repository\folderA"
folder_b = r"C:\path\to\repository\folderB"
folder_c = r"C:\path\to\repository\folderC"

# 执行操作
copy_new_files(folder_a, folder_b, folder_c)
delete_empty_dirs(folder_c)

Java 实现

使用Java NIO API实现,无需额外依赖:

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class ExtractNewContent {
    public static void main(String[] args) throws IOException {
        // 替换为实际路径
        Path folderA = Paths.get("C:/path/to/repository/folderA");
        Path folderB = Paths.get("C:/path/to/repository/folderB");
        Path folderC = Paths.get("C:/path/to/repository/folderC");

        // 复制A中新增的文件到C
        Files.walkFileTree(folderA, new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path relativePath = folderA.relativize(file);
                Path bFile = folderB.resolve(relativePath);
                if (!Files.exists(bFile)) {
                    Path cFile = folderC.resolve(relativePath);
                    Files.createDirectories(cFile.getParent());
                    Files.copy(file, cFile, StandardCopyOption.REPLACE_EXISTING);
                }
                return FileVisitResult.CONTINUE;
            }
        });

        // 递归删除C中的空目录
        List<Path> emptyDirs = new ArrayList<>();
        Files.walkFileTree(folderC, new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (exc == null && Files.list(dir).count() == 0) {
                    emptyDirs.add(dir);
                }
                return FileVisitResult.CONTINUE;
            }
        });

        for (Path dir : emptyDirs) {
            Files.delete(dir);
        }
    }
}

内容的提问来源于stack exchange,提问作者Emiya Elien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:17:30