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!
    )
)
4
  • If you skip lines that don't begin with "C0/", then why does your desired output preserve "Display menu window"? Commented Jul 24, 2015 at 4:46
  • Never mind. I see that by "skip" you mean "preserve as is" Commented Jul 24, 2015 at 4:49
  • I wonder how many intermediate "solutions" about this problem will follow: one, two, three, four... Why you don't request the real solution to your problem once for all? Commented Jul 25, 2015 at 13:46
  • I ask a question as the problem or need arises. I don't know in advance. Commented Jul 26, 2015 at 11:30

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that worked flawlessly. I ended up editing the code to allow for multiple possibilities other than just JSR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.