Adding line number to file text

Adding line number to file text

2
NewbieNewbie
2

    Dec 15, 2007#1

    I thought this would be simple, but the harder I look the more it appears to be pretty difficult.

    I want to add the current line number in the file to the text of each line and move some text around. I'm trying to add a file number to music tracks to control play.
    I want to convert a file like this:

    #EXTM3U
    #EXTINF:309,The Who - Baba O'Riley
    \My Music\Who\Baba O'Riley.mp3
    #EXTINF:246,Who - I Can See For Miles
    \My Music\Who\I Can See For Miles.mp3
    #EXTINF:199,The Who - My Generation
    \My Music\Who\Greatest Hits\04 - My Generation.mp3
    #EXTINF:183,The Who - Pinball Wizard
    \My Music\Who\Greatest Hits\05 - Pinball Wizard.mp3

    To a file like this:
    Copy "S:\My Music\Who\Baba O'Riley.mp3" "S:\TempCopy\001 Baba O'Riley.mp3"
    Copy "S:\My Music\Who\I Can See For Miles.mp3" "S:\TempCopy\002 I Can See For Miles.mp3"
    Copy "S:\My Music\Who\Greatest Hits\04 - My Generation.mp3" "S:\TempCopy\003 04 - My Generation.mp3"
    Copy "S:\My Music\Who\Greatest Hits\05 - Pinball Wizard.mp3" S:\TempCopy\004 05 - Pinball Wizard.mp3"

    Where 001-004 are the line number of each line in the file.

    I have gotten this far with this macro:
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    Find "\My Music\"
    Replace All "Copy "S:\My Music\"
    Find ".mp3"
    Replace All ".mp3" S:\TempCopy"
    GotoLine 1
    Find "#EXTM3U"
    IfFound
    DeleteLine
    EndIf
    Loop 200
    GotoLine 1
    Find "#EXTINF:"
    IfFound
    DeleteLine
    Else
    ExitLoop
    EndIf
    EndLoop

    This Macro gives me this output:
    Copy "S:\My Music\Who\Baba O'Riley.mp3" S:\TempCopy
    Copy "S:\My Music\Who\I Can See For Miles.mp3" S:\TempCopy
    Copy "S:\My Music\Who\Greatest Hits\04 - My Generation.mp3" S:\TempCopy
    Copy "S:\My Music\Who\Greatest Hits\05 - Pinball Wizard.mp3" S:\TempCopy

    The 2 remaining things I'm struggling with is how to pickup and copy just filename (not path) and how to add the line number (with leading zero padding).

    I'm tempted to just write an app in C# to do it, but I like the idea of using a UE macro.

    I'm amazed at how complex some of the macros you guys put together are. So I thought maybe one of you could pull this one off.

    Thanks so much in advance!

    - Robert

    6,603548
    Grand MasterGrand Master
    6,603548

      Dec 15, 2007#2

      No problem to do this with a macro.

      First the macro checks if the last line of the file ends with line ending (cursor is at column 1) and if not it inserts a line ending at bottom of the file.

      Back at top of the file and after trimming all trailing spaces it deletes every line starting with #EXT.

      Depending on your version of UltraEdit which you have not posted this Find and Replace All could leave lines starting with #EXT. If this happens with your version of UE, the first Find+Replace All must be enclosed in a loop which is exited if nothing was replaced anymore. Next time please post also your version of UE so we are able to write the solution which surely works for you.

      The cursor is still a top of the file. The macro switches now to column mode and inserts a space at start of every line. The cursor position has still not changed and so the macro can run the insert number command to insert the line numbers with leading zeros. The number of necessary leading zeros (0, 1, 2, 3, ...) is automatically determined by UltraEdit.

      Back in normal text mode the cursor is moved to bottom of the file and deletes there the line number with the space from the last line.

      After moving the cursor again to top of the file a regular expression finds the last backslash before the file name and replaces it in all lines with !!! for the next replace.

      The final UltraEdit syntax regular expression find and replace all reformats the lines by tagging the line number and the space at start of every line with ^([0-9]+ ^), the file path without the last backslash with %^(*^) and the file name after !!! with ^(*^) and using these 3 selections in the replace string again where needed.

      The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNumGt 1
      InsertLine
      EndIf
      Top
      TrimTrailingSpaces
      Find MatchCase RegExp "%#EXT*^p"
      Replace All ""
      ColumnModeOn
      ColumnInsert " "
      ColumnInsertNum 1 1 LeadingZero
      ColumnModeOff
      Bottom
      DeleteToStartofLine
      Top
      Find RegExp "\^([~\^p]+^)$"
      Replace All "!!!^1"
      Find RegExp "%^([0-9]+ ^)^(*^)!!!^(*^)$"
      Replace All "Copy "S:^2\^3" "S:\TempCopy\^1^3""
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Dec 18, 2007#3

        Sorry for the delay. It did not work originally (because I was still using v10). I have used UE since early single digit versions (about 7 years), so I figured I might as well just go ahead and upgrade. So now I'm using v13.

        With v13 your Macro works great! My hat is off to you Mofi. You are a genius with these things. Thank you so much for your help.

        For anyone else that is interested, this Macro will take a WinAmp play list file and convert it to a BAT file. The BAT file will then copy various songs from your library to a single temp folder that you can burn to MP3 CD or copy to your MP3 player. Mofi's macro adds a leading sequence number so your tracks will play back in play list order instead of whatever your MP3 or CD player decides to do with them. Very nice!

        PS: I dl'ed your Macro Guide from the forum. I'll do some studying...

        Thanks again for your willingness to help the community.

        - Robert