查找并替换IR代码

时间:2016-12-13 09:55:27

标签: windows batch-file replace

您好,我在使用下面的代码时遇到了一些麻烦。我想用IR代码替换.txt文件中的每个数字。我正在运行Windows 10 64bit。

从代码09的IR代码。

REM 0 - 0x1067728D
REM 1 - 0x106742BD
REM 2 - 0x1067827D
REM 3 - 0x106702FD
REM 4 - 0x1067629D
REM 5 - 0x1067A25D
REM 6 - 0x106722DD
REM 7 - 0x106752AD
REM 8 - 0x1067926D
REM 9 - 0x106712ED

我从here中挖出了一个查找并替换脚本,并为每个数字创建了一个循环,但output.txt看起来像这样:↓

 {00883}  {0080x106712ED-4}  {0080x106712ED-5}  {0080x106712ED-0x106712ED-}  {00887}  {00888}

而不是这样:↓

 {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106702FD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067629D}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067A25D}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106722DD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106752AD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067926D}

输入文件如下所示:↓

{00883}  {00884}  {00885}  {00886}  {00887}  {00888}

这是我正在使用的代码,我们将非常感谢任何建议或建议。

::CallScript
@echo off
CALL :Script0
CALL :Script1
CALL :Script2
CALL :Script3
CALL :Script4
CALL :Script5
CALL :Script6
CALL :Script7
CALL :Script8
CALL :Script9
pause
goto :eof

:Script0
@echo off
set "NewStr=0x1067728D-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=0"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script1
@echo off
set "NewStr=0x106742BD-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=1"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script2
@echo off
set "NewStr=0x1067827D-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=2"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script3
@echo off
set "NewStr=0x106702FD-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=3"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script4
@echo off
set "NewStr=0x1067629D-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=4"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script5
@echo off
set "NewStr=0x1067A25D-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=5"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script6
@echo off
set "NewStr=0x106722DD-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=6"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script7
@echo off
set "NewStr=0x106752AD-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=7"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script8
@echo off
set "NewStr=0x1067926D-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=8"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

:Script9
@echo off
set "NewStr=0x106712ED-"
set "InFile=%USERPROFILE%\Desktop\MDVR IR numbers\In.txt"
set "OutPutFile=%USERPROFILE%\Desktop\MDVR IR numbers\Out.txt"
Call :FindReplace "" "%NewStr%" "%InFile%"
set "OldStr=9"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"

:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
goto :eof

3 个答案:

答案 0 :(得分:3)

起初,你的代码太长了 您了解功能,但不以正确的方式使用它们。

第二个问题是,你总是在输入文件中替换某些东西并创建一个新的输出文件,但你想逐步替换。

接下来的问题是,将数字替换为其他数字的顺序无效,因为您将所有0x1067728D替换为1,然后尝试将所有0x106742BD替换为1,但这也会替换之前0x1067728D中的MNOPQRSTUV

您必须先将所有数字替换为其他数字,而不会在替换表达式中出现(我选择@echo off set "InFile=In.txt" set "OutPutFile=Out.txt" copy "%InFile%" "%OutPutFile%" CALL :replace 0 M CALL :replace 1 N CALL :replace 2 O CALL :replace 3 P CALL :replace 4 Q CALL :replace 5 R CALL :replace 6 S CALL :replace 7 T CALL :replace 8 U CALL :replace 9 V CALL :replace M "0x1067728D," CALL :replace N "0x106742BD," CALL :replace O "0x1067827D," CALL :replace P "0x106702FD," CALL :replace Q "0x1067629D," CALL :replace R "0x1067A25D," CALL :replace S "0x106722DD," CALL :replace T "0x106752AD," CALL :replace U "0x1067926D," CALL :replace V "0x106712ED," CALL :replace ",}" } goto :eof :replace @echo off set "OldStr=%~1" set "NewStr=%~2" Call :FindReplace "%OldStr%" "%NewStr%" "%OutPutFile%" exit /b :FindReplace <OldStr> <NewStr> <InFile> <OutPutFile> SETLOCAL ENABLEEXTENSIONS, DisableDelayedExpansion rem if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF ( for /f "tokens=*" %%A in ('findstr /n "^" %3') do ( set "line=%%A" setlocal EnableDelayedExpansion set "line=!line:*:=!" if defined line ( set "line=!line:%~1=%~2!" ) (echo(!line!) endlocal ) ) > TEMPFILE.tmp del "%~3" move TEMPFILE.tmp "%~3" goto :eof )。
然后你可以用你想要的表达替换字母。

$reparr =   '0x1067728D','0x106742BD','0x1067827D','0x106702FD','0x1067629D','0x1067A25D','0x106722DD','0x106752AD','0x1067926D','0x106712ED'
$output = ""
$input = "{00883}  {00884}  {00885}  {00886}  {00887}  {00888}"
foreach ($c in $input.ToCharArray()){
    try{
        $c = [int]([string]$c)
        $c = $reparr[$c]
        $output += $c + "-"
    } Catch {
        $output += $c
    }
}
$output.replace("-}","}")

答案 1 :(得分:2)

所以首先我不是那么好,所以我无法帮助你。 我可以在powershell中使用它,这样可以更轻松。

{0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106702FD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067629D}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067A25D}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106722DD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x106752AD}  {0x1067728D-0x1067728D-0x1067926D-0x1067926D-0x1067926D}

输出:

This works for me:-

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:APP_STORE_LINK];
content.imageURL=[NSURL URLWithString:IMAGE_LINK];

FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.shareContent = content;
dialog.fromViewController = self;
dialog.mode = FBSDKShareDialogModeFeedBrowser;
[dialog show];

另外,如果你不能在powershell中做到这一点,它应该给你一个如何更容易做到的提示。 因为在您的示例中,您经常复制代码,这实际上是一个糟糕的代码质量。

答案 2 :(得分:1)

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
:: data-input file
SET "filename1=%sourcedir%\q41118105.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 CALL :REPL "%%a" 0 m 1 n 2 o 3 p 4 q 5 r 6 s 7 t 8 u 9 v m 0x1067728D- n 0x106742BD- o 0x1067827D- p 0x106702FD- q 0x1067629D- r 0x1067A25D- s 0x106722DD- t 0x106752AD- u 0x1067926D- v 0x106712ED- -} }
)
)>"%outfile%"

GOTO :EOF

:REPL
SET "outdata=%~1"
:repll
SHIFT
IF "%2"=="" ECHO %outdata%&GOTO :EOF
CALL SET "outdata=%%outdata:%1=%2%%"
SHIFT
GOTO repll

您需要更改sourcedirdestdir的设置以适合您的具体情况。

我使用了一个名为q41118105.txt的文件,其中包含我的测试数据。

生成定义为%outfile%

的文件

获取jeb的引导,此解决方案读取输入文件,并传递该行,但引用+每对替换到子例程。

子例程选择第一个项目并删除引号,然后用后面的序列替换下一个参数,使用shift处理使用过的参数,直到没有剩下的参数,结果为{{ 1}}编