任何工具/技巧删除Windows 7中2个文件夹之间的公共文件

时间:2014-08-01 01:11:53

标签: windows windows-7 cmd directory

我有2个内容几乎相同的文件夹,我想做的是找到每个文件不存在的文件

Folder 1: a.doc, b.doc, c.doc, d.doc Folder 2: b.doc, d.doc, e.doc

因为,每个文件夹都有b.doc, d.doc,输出变为:

Folder 1: a.doc, c.doc Folder 2: e.doc

(是的,它会自动删除它)

每个文件夹包含1000多个文件,其文件名均为韩文。

我目前正在做的是通过命令提示我通过dir获取文件名列表,但实际上它只是手动而已。

请有人帮忙吗?

2 个答案:

答案 0 :(得分:2)

这应该会在记事本中为"c:\file.bat.txt"创建examine for accuracy,然后再将其重命名为bat并执行它。

问题可能是Korean filenames,因为非英语和unicode字符始终不会被批处理文件处理好。可能需要更改代码页。

@echo off
for %%a in ("c:\folder1\*.*") do if exist "c:\folder2\%%~nxa"  >"c:\file.bat.txt" echo del "%%a"
for %%a in ("c:\folder2\*.*") do if exist "c:\folder1\%%~nxa" >>"c:\file.bat.txt" echo del "%%a"
pause

答案 1 :(得分:1)

这应该在不必处理韩文文件名的情况下完成工作。

警告此代码仅考虑文件名,删除文件夹A和B之间的所有常用文件。

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Target folders configuration
    set "folderA=%cd%\a"
    set "folderB=%cd%\b"

    rem -----------------------------------------------------

    rem Temporary work folders and output options
    set "folderA1=%temp%\%~n0.a.%random%%random%%random%.tmp"
    set "folderB1=%temp%\%~n0.b.%random%%random%%random%.tmp"
    set "rcOpt=/njh /njs /np /nfl /ndl > nul"

    rem Create a duplicate of the folders, with 0 bytes files 
    robocopy "%folderA%" "%folderA1%" /create %rcOpt%
    robocopy "%folderB%" "%folderB1%" /create %rcOpt%

    rem Remove all files in B1 present in A
    rem Overwrites the files in A that will later be removed
    robocopy "%folderB1%" "%folderA%" /xl /mov %rcOpt%

    rem Remove all files in A1 present in B
    rem Overwrites the files in B that will later be removed
    robocopy "%folderA1%" "%folderB%" /xl /mov %rcOpt%

    rem A1 contains only the non common files in A
    rem B1 contains only the non common files in B

    rem Delete from target folders the files not present in source 
    robocopy "%folderA1%" "%folderA%" /nocopy /purge %rcOpt%
    robocopy "%folderB1%" "%folderB%" /nocopy /purge %rcOpt%

    rem Cleanup
    rmdir /s /q "%folderA1%" >nul 2>nul
    rmdir /s /q "%folderB1%" >nul 2>nul