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

问题描述 投票:0回答:1

假设我的文件夹是:“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”,我想复制/剪切文件,对于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
batch-file xcopy
1个回答
0
投票

我找到了一个使用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
© www.soinside.com 2019 - 2024. All rights reserved.