比较目录名字符串 [英] Compare directory name to string

查看:177
本文介绍了比较目录名字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了用于测试的概念,我希望能利用的缘故这个非常简单的批处理文件。我需要递归删除所有的一种类型的文件,除非是具有特定名称的文件夹。这里是我的code:

I've created this very simple batch file for the sake of testing a concept I'm hoping to utilize. I need to recursively delete all of one type of file except in folders with a specific name. Here's my code:

:recur
FOR /f %%a IN ('DIR /b') DO (
IF EXIST %%a\NUL (
    IF ["%%a" NEQ "subtest2"] (
        ECHO %%a
        CD %%a
        CALL :recur
        CD ..
    )
)
COPY "*.cfm" "*_copy.cfm"
REM DEL "*_copy*.cfm"
)

现在我使用的副本,而不是删除只是测试。基本上,这应该创建所有的.CFM文件的副本,除了在文件夹subtest2。现在它是递归制作的复印件到处都是,包括subtest2。如何停止呢?

Right now I'm just testing using copy instead of delete. Basically, this should create a copy of all the .cfm files except in the folder "subtest2". Right now it's recursively making the copies everywhere, including subtest2. How do I stop this?

我的基本目录的结构是:

The structure of my base directory is:

TestFolder结果
--- subtest1结果
------的test.pdf结果
------ test.txt的结果
------ test.cfm结果
--- subtest2结果
------的test.pdf结果
------ test.txt的结果
------ test.cfm结果
---的test.pdf结果
--- test.txt的结果
--- test.cfm结果
--- recur.bat

TestFolder
---subtest1
------test.pdf
------test.txt
------test.cfm
---subtest2
------test.pdf
------test.txt
------test.cfm
---test.pdf
---test.txt
---test.cfm
---recur.bat

推荐答案

方括号不会在IF两侧对比平衡,所以它永远无法比拟的。括号不是IF语法的一部分。如果present,他们简直成了用来比较的字符串的一部分。这同样适用于任何封闭引号真。去掉方括号,而且它会工作(假设没有其他问题)

The square brackets are not balanced on both sides of the IF comparison, so it can never match. The brackets are not part of the IF syntax. If present, they simply become part of the string that is compared. The same is true for any enclosing quotes. Remove the square brackets, and it will work (assuming there are no other problems)

下面是实现你的目标的简单方法。我已经pfixed DEL命令与ECHO用于测试目的$ P $:

Here is a simple method to accomplish your goal. I've prefixed the DEL command with ECHO for testing purposes:

for /r /d %%F in (*) do echo %%F\|findstr /liv "\\subtest2\\" >nul && echo del "%%F\*.cfm"

在FOR / R / D只是递归的所有文件夹。每个文件夹的完整路径是通过管道输送到FINDSTR命令查找不包含\\ subtest2文件夹路径。如果\\ subtest2 \\文件夹没有在路径中只执行了ECHO DEL命令。

The FOR /R /D simply recurses all folders. The full path of each folder is piped into a FINDSTR command that looks for paths that do not contain a \subtest2 folder. The ECHO DEL command is only executed if the \subtest2\ folder is not found in the path.

删除最后ECHO当你确认命令给出正确的结果。

Remove the last ECHO when you have confirmed the command gives the correct results.

修改 %%˚F%F 如果你想运行的命令行的命令,而不是在一个批处理文件。

Change %%F to %F if you want to run the command on the command line instead of in a batch file.

这篇关于比较目录名字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆