在文件夹中的txt文件列表中搜索“keyword”,如果找到“keyword”,则将txt文件移动到另一个文件夹(批处理脚本)

时间:2017-04-05 05:41:11

标签: batch-file scripting

下面是我的伪代码

For loop in a folder where contains a list of txt files
if (keyword is found in the txt file) && (keyword2 is found in the txt file) 
move the txt file to another folder 
log the current txt file name to temp file

keyword和keyword2不在同一行

如何编写批处理脚本?

2 个答案:

答案 0 :(得分:0)

未经测试:

@echo off
setlocal enableDelayedExpansion
for %%# in (*txt) do (
    set "line1="
    set "line2="
    if "%%~nx#" neq "%~nx0" (
        for /f "tokens=2* delims=[]" %%a in ('find /n /i "keyword1" "%%#"') do set line1=%%a
        for /f "tokens=2* delims=[]" %%a in ('find /n /i "keyword2" "%%#"') do set line2=%%a

        if defined line1 if defined line2 if "!line1!" neq "!line2!"  (
            echo move /y "%%~f#" "c:\destination"
        )
    )
)

在此行echo move /y "%%~f#" "c:\destination"中,您可以看到要移动的文件。如果可以,请删除echo字词。

答案 1 :(得分:0)

我会在同一个循环中运行它:

@Echo Off
For /F "Delims=" %%A In (
    'FindStr/IMC:"SecondString" *.log^|FindStr/IMC:"FirstString" /F:/') Do (
    (Echo=%%~fA)>>"TempFile.log"
    Move /Y "%%~fA" "C:\Destination")
Timeout -1

根据需要替换FirstStringSecondStringC:\Destination