#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL file_count AS LONG
    LOCAL i AS LONG
    LOCAL old_name AS STRING
    LOCAL new_name AS STRING
    LOCAL pattern_find AS STRING
    LOCAL pattern_replace AS STRING
    LOCAL found_pos AS LONG
    LOCAL before_str AS STRING
    LOCAL after_str AS STRING
    LOCAL renamed_count AS LONG
    LOCAL temp_str AS STRING
    LOCAL j AS LONG
    LOCAL c_val AS LONG

    ' Batch file renamer with pattern matching

    PRINT "Batch File Renamer"
    PRINT "=================="
    PRINT ""

    file_count = 8
    pattern_find = "old_"
    pattern_replace = "new_"

    PRINT "Pattern: replace '" + pattern_find + "' with '" + pattern_replace + "'"
    PRINT ""

    ' Simulated file list
    i = 0
    WHILE i < file_count
        IF i = 0 THEN old_name = "old_report.txt"
        IF i = 1 THEN old_name = "old_data.csv"
        IF i = 2 THEN old_name = "photo.jpg"
        IF i = 3 THEN old_name = "old_backup.zip"
        IF i = 4 THEN old_name = "document.docx"
        IF i = 5 THEN old_name = "old_log.txt"
        IF i = 6 THEN old_name = "old_config.ini"
        IF i = 7 THEN old_name = "summary.pdf"

        found_pos = INSTR(1, old_name, pattern_find)
        IF found_pos > 0 THEN
            before_str = LEFT$(old_name, found_pos - 1)
            after_str = MID$(old_name, found_pos + LEN(pattern_find))
            new_name = before_str + pattern_replace + after_str
            renamed_count = renamed_count + 1
            PRINT "  " + old_name + " -> " + new_name
        ELSE
            PRINT "  " + old_name + " (no match, skipped)"
        END IF

        i = i + 1
    WEND

    temp_str = STR$(renamed_count)
    PRINT ""
    PRINT "Renamed " + temp_str + " of " + STR$(file_count) + " files"

    ' Number-based renaming
    PRINT ""
    PRINT "Number-based renaming (IMG_####.jpg):"
    PRINT "-------------------------------------"
    i = 0
    WHILE i < 5
        old_name = "IMG_" + STR$(i + 1) + ".jpg"
        new_name = "photo_"
        temp_str = STR$(i + 1)
        IF i + 1 < 10 THEN temp_str = "0" + temp_str
        IF i + 1 < 100 THEN temp_str = "0" + temp_str
        new_name = new_name + temp_str + ".jpg"
        PRINT "  " + old_name + " -> " + new_name
        i = i + 1
    WEND

    ' Case conversion renaming
    PRINT ""
    PRINT "Lowercase conversion (uppercase files):"
    PRINT "--------------------------------------"
    i = 0
    WHILE i < 4
        IF i = 0 THEN old_name = "README.TXT"
        IF i = 1 THEN old_name = "MAIN.BAS"
        IF i = 2 THEN old_name = "CONFIG.SYS"
        IF i = 3 THEN old_name = "HEADER.H"
        new_name = ""
        j = 1
        WHILE j <= LEN(old_name)
            c_val = ASC(MID$(old_name, j, 1))
            IF c_val >= 65 AND c_val <= 90 THEN
                new_name = new_name + CHR$(c_val + 32)
            ELSE
                new_name = new_name + CHR$(c_val)
            END IF
            j = j + 1
        WEND
        PRINT "  " + old_name + " -> " + new_name
        i = i + 1
    WEND

    ' Extension change
    PRINT ""
    PRINT "Extension change (.dat -> .txt):"
    PRINT "-------------------------------"
    i = 0
    WHILE i < 4
        IF i = 0 THEN old_name = "data_01.dat"
        IF i = 1 THEN old_name = "data_02.dat"
        IF i = 2 THEN old_name = "output.dat"
        IF i = 3 THEN old_name = "readme.txt"

        found_pos = INSTR(1, old_name, ".dat")
        IF found_pos > 0 THEN
            new_name = LEFT$(old_name, found_pos - 1) + ".txt"
            PRINT "  " + old_name + " -> " + new_name
        END IF
        i = i + 1
    WEND

    ' Prefix/suffix operations
    PRINT ""
    PRINT "Add prefix 'backup_':"
    PRINT "--------------------"
    i = 0
    WHILE i < 3
        IF i = 0 THEN old_name = "data.txt"
        IF i = 1 THEN old_name = "config.ini"
        IF i = 2 THEN old_name = "log.txt"
        new_name = "backup_" + old_name
        PRINT "  " + old_name + " -> " + new_name
        i = i + 1
    WEND

    PRINT ""
    PRINT "Batch renamer demo complete."

    FUNCTION = 0
END FUNCTION
