2017-02-13 94 views
0

我想通過每兩個字符添加「0x」修改CMD中每個.txt文件的每一行,任何想法我應該使用什麼命令在for裏面?CMD命令逐行修改文件,在CMD中每兩個字符添加「0x」

Source.txt文件將是這個樣子:

602A0020B1010008B9010008BB010008 
BD0185AC8B9010008BB10008BB010008 
AC8B9010008BB100B9010008BB045809 
602A0020B1010008 

,預計在文件的Result.txt或同一源獲得的輸出格式,將以下內容:

0x60, 0x2A, 0x00, 0x20, 0xB1, 0x01, 0x00, 0x08, 0xB9, 0x01, 0x00, 0x08, 0xBB, 0x01, 0x00, 0x08, 
0xBD, 0x01, 0x00, 0x08, 0xBF, 0x01, 0x00, 0x08, 0xC1, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 
... 
+2

的[批量添加字符每x個字符可能的複製](http://stackoverflow.com/questions/272978 48/batch-adding-a-character-every-x-characters) – JosefZ

回答

0

純批次的解決方案:

@echo off 
setlocal enableDelayedExpansion 
set "file=test.txt" 
>"%file%.new" (
    for /f "usebackq delims=" %%A in ("%file%") do (
    set "ln=%%A" 
    for /l %%N in (30 -2, 2) do if "!ln:~%%N!" neq "" set "ln=!ln:~0,%%N!, 0x!ln:~%%N!" 
    echo 0x!ln!, 
) 
) 
move /y "%file%.new" "%file%" >nul 


使用JREPL.BAT

call jrepl "^..|.." "0x$&,| 0x$&," /t "|" /f test.txt /o - 

call jrepl ".." "$txt=($off==0?'0x':' 0x')+$0+','" /jq /f test.txt /o - 
+0

謝謝,只是在每行末尾都沒有逗號,但它非常完美!用echo 0x!ln!,效果很好! – Estudiante

+0

@Estudiante - 我從來沒有想到你最終會想要一個逗號,而且我從來沒有捲過你的結果去看它。我修復了所有的答案以添加尾隨逗號。 – dbenham

0
@echo off&SetLocal EnableDelayEdexpansion 

for /f "eol= delims=" %%a in (source.txt) do (
    call :showLine %%a 
) 
pause 

:showLine 
set line=%1 
for /l %%a in (0 2 100) do (
    set n=!line:~%%a,2! 
    if defined n (
    set /p=0x!n!,<nul 
) else (
    echo; 
    goto :eof 
) 
) 
goto :eof 
0

這裏是另一個純解決方案,使用goto循環:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_FILE=%~1" 

< "%_FILE%" > "%_FILE%.new" call :READ 
> nul move /Y "%_FILE%.new" "%_FILE%" 

endlocal 
exit /B 


:READ 
    rem // Read one line, stop if empty: 
    set "LINE=" 
    set /P LINE="" 
    if not defined LINE goto :EOF 
    set "BUF=" 
:LOOP 
    rem // Process line, build new one: 
    set "HEX=%LINE:~,2%" 
    set "BUF=%BUF%0x%HEX%, " 
    if "%LINE:~3%"=="" goto :NEXT 
    set "LINE=%LINE:~2%" 
    goto :LOOP 
:NEXT 
    rem // Return built line, read next one: 
    echo %BUF:~,-1% 
    goto :READ