0

I have a .txt document with over 32,000 lines of commented machine code. It looks like this:

Display menu window
C0/000E:    E220        SEP #$20       (Set 8-bit accumulator)
C0/0010:    C210        JSR $0011      (Call function X)

I need to convert it as follows:

Display menu window
C0/000E:    E220        SEP #$20       (Set 8-bit accumulator)
C0/0010:    C210        JSR C00011     (Call function X)

Specifically, that means the script must:

  1. Skip blank lines
  2. Skip lines that don't start with "C0/"
  3. Check if "JSR $" is the 25th character on the line. (Assuming my counting is right)
  4. Replace "JSR $" with "JSR C0" (preferably, replace "$" with "C0")
  5. Remove a space after "JSR C0XXXX" to keep the comments aligned, as we just added a character to the line ($ -> C3).

I have this code that I've been trying to edit to achieve this. You can fix it or write your own.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source=%1

FOR /F "tokens=1 delims=." %%t IN ("%source%") DO SET target=%%t
SET target=!target!_2.txt
TYPE NUL>!target!

FOR /F "tokens=1* delims=]" %%j in ('type "%source%" ^| find /V /N ""') DO (
    IF "%%k"=="" (
        ECHO.>>!target!
    ) ELSE (
        SET currentLine=%%k
        IF "!currentLine:~0,3!"=="C0/" (
            IF "!currentLine:~25,5"=="JSR $" (
                SET left=!currentLine:~0,24!
                SET right=!currentLine:~25!
                ECHO !right!
                SET right=!right:(=C0!
                SET right=!right:^)=!
                ECHO !right!
                SET currentLine=!left!!right!
            )
        )
        ECHO !currentline!>>!target!
    )
)
Sheldon M.
  • 49
  • 7
  • If you skip lines that don't begin with "C0/", then why does your desired output preserve "Display menu window"? – dbenham Jul 24 '15 at 04:46
  • Never mind. I see that by "skip" you mean "preserve as is" – dbenham Jul 24 '15 at 04:49
  • I wonder how many intermediate "solutions" about this problem will follow: [one](http://stackoverflow.com/questions/31306603/batch-file-to-remove-first-18-chars-from-txt-file), [two](http://stackoverflow.com/questions/31557599/batch-script-to-replace-parentheses), [three](http://stackoverflow.com/questions/31581556/batch-script-to-edit-a-txt-file), [four](http://stackoverflow.com/questions/31601390/batch-script-edit-to-replace-text)... Why you don't request _the **real** solution_ to your problem once for all? – Aacini Jul 25 '15 at 13:46
  • I ask a question as the problem or need arises. I don't know in advance. – Sheldon M. Jul 26 '15 at 11:30

1 Answers1

1

You can use %~dpn1 do get the drive, path, and base name, without the extension.

Much better to direct output just once outside the loop - no longer have to initialize it to empty, and it is faster.

No need to pipe TYPE to FIND when FIND can read the file directly.

Your counting was wrong, and your logic was simply wrong. It is fairly easy to construct the line directly without any intermediate variables.

No need for multiple ECHO statements. Simply always define currentLine, and test if it is defined (not empty).

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

>"%~dpn1_2.txt" (
  FOR /F "tokens=1* delims=]" %%j in ('find /V /N "" %1') DO (
    set "currentLine=%%k"
    IF DEFINED currentLine (
      IF "!currentLine:~0,3!"=="C0/" (
        IF "!currentLine:~24,5!"=="JSR $" (
          SET "currentLine=!currentLine:~0,28!C0!currentLine:~29,4!!currentLine:~34!"
        )
      )
    )
    ECHO(!currentline!
  )
)

Or you could make your life much easier and use JREPL.BAT - a regular expression text processing utility. JREPL.BAT is pure script (Jscript/batch hybrid) that runs natively on any Windows machine from XP onward.

@call jrepl "^(C0/.{21}JSR )\$(.*?) " "$1C0$2" /f %1 /o "%~dpn1_2.txt"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thank you very much, that worked flawlessly. I ended up editing the code to allow for multiple possibilities other than just JSR. – Sheldon M. Jul 24 '15 at 07:09