有没有办法从另一个文件夹中的文件夹中删除文件?

Dar*_*e13 24 windows windows-explorer

假设我从文件夹 A 复制和粘贴文件,其中包括:

文件夹A:

file1.cfg  
file2.txt  
file3.esp  
file4.bsa  
Run Code Online (Sandbox Code Playgroud)

进入文件夹B,更新后,它有:

文件夹 B:

apples.mp3  
file1.cfg    *
file2.txt    *
file3.esp    *
file4.bsa    *
turtles.jpg
Run Code Online (Sandbox Code Playgroud)

有没有办法从文件夹A中删除文件夹B中的所有文件(用*标记)?除了手动选择每个并删除它,或者在复制粘贴后立即按Ctrl-Z

我更喜欢 Windows 方法或一些可以执行此操作的软件

谢谢!

phy*_*fox 37

有免费软件叫做WinMerge。您可以使用此软件来匹配重复项。首先,使用FileOpen,然后选择两个目录,左侧是包含要保留的文件的文件夹,右侧是不包含的文件夹。然后,转到View,并取消选择Show Different ItemsShow Left Unique ItemsShow Right Unique Items。这将只留下列表中的相同文件。之后,选择EditSelect All,右键单击任何文件,然后单击DeleteRight。这将从右侧文件夹中删除重复项。

WinMerge 演示


LPC*_*hip 25

这可以通过使用命令通过命令行完成 forfiles

假设您将文件夹 A 位于 中c:\temp\Folder A,文件夹 B 位于c:\temp\Folder B

该命令将是:

c:\>forfiles /p "c:\temp\Folder A" /c "cmd /c del c:\temp\Folder B\@file"
Run Code Online (Sandbox Code Playgroud)

完成此操作后,文件夹 B 将删除文件夹 A 中存在的所有文件。请记住,如果文件夹 B 包含名称相同但内容不同的文件,它们仍将被删除。

也可以将其扩展到子文件夹中的文件夹,但出于担心这会变得不必要的复杂,我决定不发布它。它需要 /s 和 @relpath 选项(并进一步测试 xD)


Ben*_*n N 12

您可以使用此 PowerShell 脚本:

$folderA = 'C:\Users\Ben\test\a\' # Folder to remove cross-folder duplicates from
$folderB = 'C:\Users\Ben\test\b\' # Folder to keep the last remaining copies in
Get-ChildItem $folderB | ForEach-Object {
    $pathInA = $folderA + $_.Name
    If (Test-Path $pathInA) {Remove-Item $pathInA}
}
Run Code Online (Sandbox Code Playgroud)

希望它是不言自明的。它查看文件夹 B 中的每个项目,检查文件夹 A 中是否存在同名的项目,如果有,则删除文件夹 A 的项目。请注意,\文件夹路径中的最后一个很重要。

单线版:

gci 'C:\Users\Ben\test\b\' | % {del ('C:\Users\Ben\test\a\' + $_.Name) -EA 'SilentlyContinue'}
Run Code Online (Sandbox Code Playgroud)

如果您不在乎控制台中是否会出现大量红色错误,则可以删除-EA 'SilentlyContinue'.

将其另存为.ps1文件,例如dedupe.ps1. 在运行 PowerShell 脚本之前,您需要启用它们的执行:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)

然后,.\dedupe.ps1当您位于包含它的文件夹中时,您就可以使用它来调用它。


Has*_*tur 5

rsync

rsync是一个用来同步目录的程序。从您拥有的许多(非常多)选项中,有自我解释的--ignore-non-existing, --remove-source-files--recursive.

你可以做

rsync -avr --ignore-non-existing --recursive --remove-source-files   B/ A -v
Run Code Online (Sandbox Code Playgroud)

如果我们假设您在目录 A (4) 和 B (4+2) 中有文件。

A       B
??? a   ??? a
??? b   ??? b
??? c   ??? c
??? d   ??? d
        ??? e
        ??? f     # Before


A       B
??? a   ??? e
??? b   ??? f
??? c   
??? d             # After
Run Code Online (Sandbox Code Playgroud)