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

时间:2017-12-22 13:31:18

标签: batch-file xcopy

我们说我的文件夹是:" 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

1 个答案:

答案 0 :(得分: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