在 .dat 文件中找到一个词并复制

Find a word in the .dat files and copy

假设我的文件夹是:"C:\sample\"。有两种类型的 .dat 文件。

一个是

Head #Index Name= "DbResultDataHeadStruct" TypeNo VarType = REG_DWORD 0x8 Data = "" TypeA VarType = REG_DWORD 0x8 Data = ""

另一个是:

Head #Index Name= "DbResultDataHeadStruct" TypeNo VarType = REG_DWORD 0x8 Data = "" TypeB VarType = REG_DWORD 0x8 Data = ""

如您所见,唯一的区别是 TypeA 和 TypeB。如果 .dat 文件中的 TypeA 为 "C:\sample\TypeA",我想 copy/cut 文件为 "C:\sample\TypeB",TypeB 为 "C:\sample\TypeB"。此批处理文件将始终等待新文件。 我在下面找到了这段代码,但我无法根据我的意思修复它。也许会有帮助。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "PATTERN=*.dat"
set "SOURCE=C:\sample\"
set "TARGET=C:\sample\TypeA"
set "STRING1=TypeA"
set "STRING2=TypeB"

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

我找到了使用 findstr 和 move 命令的解决方案。

我也添加了超时。它每 10 秒检查一次文件夹。

@echo OFF
setlocal enableextensions disabledelayedexpansion

set "source=C:\sample\*.dat"
set "target=C:\sample\TypeA\OpMode"
set "target2=C:\sample\TypeB\Error"
set "searchString=OpMode"
set "searchString2=Error"

:loop
set "found="
for /f "delims=" %%a in ('
    findstr /m /i /l /c:"%searchString%" "%source%" 2^>nul 
') do (
    if not defined found set "found=1"
    move "%%a" "%target%"
)



set "found="
for /f "delims=" %%a in ('
    findstr /m /i /l /c:"%searchString2%" "%source%" 2^>nul 
') do (
    if not defined found set "found=1"
    move "%%a" "%target2%"
)

timeout /T 10

goto loop