用于比较两个文件列表并删除两个列表中显示的文件的命令行

Command line to compare two file list and delete files that shows on both lists

在 windows 7 中,我有一个包含以下文件的文件夹 1:

a.txt
b.txt
c.txt

还有另一个文件夹 2 包含以下文件:

b.txt
c.txt
d.txt

我想使用命令行从文件夹 2 中删除文件 b.txtc.txt,它们出现在两个列表中。 我了解如何使用 for /f 生成文件列表。我不知道如何在 for /f 中执行另一个循环以便删除文件。

取决于您 shell 和您的 OS。如果你使用 bash 那么我会这样想:

#!/bin/bash

folder1=a
folder2=b

for f in $folder1/*.txt; do
    g=`basename $f`
    if [ -f $folder2/$g ]; then
        echo "remove $folder2/$g"
    else
        echo "not in $folder2 $g"
    fi
done

我不确定您的意思是要从 两个 文件夹中删除文件 b.txt 和 c.txt,还是只删除一个。以下批处理文件将从 folder2 中删除它们。

@echo off
setlocal
set folder1=name_of_your_first_folder
set folder2=name_of_your_second_folder
for %%f in (%folder1%\*) do (
    if exist %folder2%\%%~nxf echo del %folder2%\%%~nxf
)
rem remove echo from above once satisfied it will do the right thing