批处理文件,使用findstr查找多个字符串并将文件复制到文件夹 [英] Batch file to find multiple string using findstr and copy files to a folder

查看:567
本文介绍了批处理文件,使用findstr查找多个字符串并将文件复制到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在目录中的文件中找到多个字符串,其中有数千个.我目前正在运行以下命令来搜索目录:

I am attempting to find multiple strings in files in a directory, there are thousands. I currently run the following command to search the directory:

findstr /s "customerid" *

现在,这使我可以找到包含该字符串的文件.我通常有两条信息,即客户ID和事件类型.一位客户最多可以进行30个相关事件,例如网站注册".

Now this allows me to find the file that contains that string. I normally have two pieces of information a customer id and an event type. One customer can have up to 30 associated event such as "website registration".

我想做的是,在目录中搜索客户ID和事件.然后将文件复制到新位置.可以在批处理文件中吗?

What I would like to do is, search the directory for both the customer id and the event. Then copy the file to a new location. Is this possible in a batch file?

推荐答案

假设您要查找包含两个单词的所有文件(在此示例中为customerevent),则可以使用以下脚本:

Supposing you want to find all files that contain both words (customer and event in this example), you could use the following script:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "PATTERN=*.txt"
set "SOURCE=."
set "TARGET=D:\Data"
set "STRING1=customer"
set "STRING2=event"

pushd "%SOURCE%" && (
    for /F "delims=" %%F in ('findstr /S /M /I /R /C:"\<%STRING1%\>" "%PATTERN%"') do (
        for /F "delims=" %%E in ('findstr /M /I /R /C:"\<%STRING2%\>" "%%F"') do (
            ECHO copy "%%E" "%TARGET%\%%~nxE"
        )
    )
    popd
)

endlocal
exit /B

测试脚本后,请删除copy命令前面的大写字母ECHO

After having tested the script, remove the upper-case ECHO in front of the copy command!

这篇关于批处理文件,使用findstr查找多个字符串并将文件复制到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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