【问题标题】:Command line to compare two file list and delete files that shows on both lists用于比较两个文件列表并删除两个列表上显示的文件的命令行
【发布时间】:2016-06-18 06:36:21
【问题描述】:

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

a.txt
b.txt
c.txt

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

b.txt
c.txt
d.txt

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

【问题讨论】:

  • 哪个平台/操作系统?

标签: windows batch-file command-line command-line-arguments


【解决方案1】:

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

@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

【讨论】:

  • 您不需要for /f %%f 开始吗?
  • 不,除非您想读取文件的内容,或者执行命令并读取其输出的内容。如果您只想浏览目录中的文件名,只需使用它而无需任何选项。输入for /? 寻求帮助。
【解决方案2】:

取决于你的外壳和你的操作系统。如果您使用的是 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

【讨论】:

  • 这是windows 7环境下的,能不能提供个解决办法,我可以用.bat/.cmd?
  • 抱歉...我现在无法访问 Windows 框。我不应该假设 bash。
  • 不用担心。如果您有机会可以查看它,我们将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多