用于根据文本文件的行项目执行循环搜索的批处理文件

发布于 2024-10-08 08:14:45 字数 237 浏览 0 评论 0原文

我一直在这个论坛上阅读精彩的帖子,并接近我想做的事情,但无法弄清楚确切的代码。

我想创建一个 Windows 批处理文件来执行以下操作:

  • 对文本文件的每个行项目(这是关键字列表)执行循环搜索,以在特定目录中查找文件
  • 对于此搜索,部分匹配是可以的。
  • 每次找到文件时,将其移动到预定义的目录(例如 C:\temp\search_results),

谢谢。

I have been reading great posts in this forum and got close to what I want to do but couldn't figure out the exact code.

I want to create a windows batch file to do following:

  • Perform a looped search for each line item of a text file (this is a list of keyword) to locate files in a a specific directory
  • For this search partial match is okay.
  • Each time a file is found, move it to a predefined directory (e.g. C:\temp\search_results)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情仇皆在手 2024-10-15 08:14:45

我目前没有运行Windows,所以我只能发布一些想法,而不是解决方案。

1) 使用for /f 迭代文件内容。

2) 使用find "%Keyword%" %SourceDir%获取匹配文件列表。您必须从 find 的输出中解析出文件名。

2a) 作为替代方案,您可以迭代源目录中的文件(使用嵌套的 for)并为每个文件调用 find,丢弃其输出并使用其退出代码( %ERRORLEVEL%) 来决定文件是否匹配(如果匹配则返回 0,如果不匹配则返回非零)。像这样:

for %%F in (%SourceDir%\*) do (
    find "%Keyword%" %%F > nul
    if not errorlevel 1 (echo File %%F matches) else (echo File %%F does not match)
)

3) 使用 move 移动匹配的文件。

I'm not running Windows at the moment, so I can only post some ideas, not the solution.

1) Use for /f to iterate over file contents.

2) Use find "%Keyword%" %SourceDir% to get the list of matching files. You will have to parse out file names from the output of find.

2a) As an alternative, you can iterate over files in the source dir (with nested for) and call find for each file, discarding its output and using its exit code (%ERRORLEVEL%) to decide whether the file matches (it will return 0 if there is a match and nonzero if there is no match). Something like this:

for %%F in (%SourceDir%\*) do (
    find "%Keyword%" %%F > nul
    if not errorlevel 1 (echo File %%F matches) else (echo File %%F does not match)
)

3) Move matching files with move.

后eg是否自 2024-10-15 08:14:45

存在多个问题。

FIND /i "%A%" ... 无法工作,FOR-Varibale 的名称是 %%A
第二个问题:使用 FIND 检查文件的内容而不是名称。
并且应该使用缩进以避免过多的括号。

您最好尝试

FOR /F "tokens=*" %%A IN (%listfile%) DO (
    FOR %%f in (%searchdir%\*) do ( 
        set "filename=%%~f"
        set replaced=!filename:%%A=!
        if !replaced! NEQ !filename! (
           echo !filename! contains '%%A'
        )
    )
)

它尝试将文件名中的 %%A 替换为 .
如果替换的文件名不等于文件名,则文件名必须包含%%A

There are multiple problems.

FIND /i "%A%" ... can't work, the name of the FOR-Varibale is %%A
And the second proble: With FIND you check the content of the file not the name.
And you should use indention to avoid too much parenthesis.

You better try

FOR /F "tokens=*" %%A IN (%listfile%) DO (
    FOR %%f in (%searchdir%\*) do ( 
        set "filename=%%~f"
        set replaced=!filename:%%A=!
        if !replaced! NEQ !filename! (
           echo !filename! contains '%%A'
        )
    )
)

It tries to replace %%A inside of the filename with .
If the replaced is not equal the filename, the filename must contain %%A

顾铮苏瑾 2024-10-15 08:14:45

我编写了以下代码,但不确定我是否走在正确的轨道上。这是我的设置:
list.txt 文件内容是(我的文件名搜索关键字) --

one
two
five
ten
six

f1 文件夹包含 --

four.txt
one.txt
three.txt

我想将匹配的内容移动到 F2 文件夹,但为了代码简单,我使用 echo 代替。

我的代码是:

@ECHO OFF
SETLOCAL EnableDelayedExpansion


SET listfile=D:\batchtest\list.txt
SET searchdir=D:\batchtest\f1

FOR /F "tokens=*" %%A IN (%listfile%) DO (

FOR %%f in (%searchdir%\*) do (FIND /i "%A%" %%f
    if errorlevel 1 (
echo Search failed) else (
echo Search successful
)   
)
)
)

它正在运行,但找不到匹配的文件名。

谢谢。

I wrote the following code but not sure if I am in the right track. Here is my setup:
list.txt file contents are (my keywords for the filename search) --

one
two
five
ten
six

f1 folder contains --

four.txt
one.txt
three.txt

I want to move the matching ones to F2 folder, but the code simplicity I am using echo instead.

My code is:

@ECHO OFF
SETLOCAL EnableDelayedExpansion


SET listfile=D:\batchtest\list.txt
SET searchdir=D:\batchtest\f1

FOR /F "tokens=*" %%A IN (%listfile%) DO (

FOR %%f in (%searchdir%\*) do (FIND /i "%A%" %%f
    if errorlevel 1 (
echo Search failed) else (
echo Search successful
)   
)
)
)

It is running but not finding matching filenames.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文