#COMPILE EXE #DIM ALL %MAXDIRSIZE = 16384 %VERBOSE = 1 FUNCTION PBMAIN () AS LONG LOCAL x$ PRINT "FixCBtype" PRINT "Makes sure that the extension of all comic book archives (ie. CBZ or CBR)" PRINT "matches the archive type (ie. RAR or ZIP) and renames them if mismatched." PRINT "Beginning in " & CURDIR$ PRINT "OK? (y/N)" x$ = WAITKEY$ IF LCASE$(x$) = "y" THEN STDOUT "Made " & FORMAT$(DoDirectory(CURDIR$), "0,") & " changes" ELSE PRINT "Aborted" END IF PRINT "Bye" END FUNCTION FUNCTION DoDirectory(startDir$) AS LONG LOCAL numentries&, temp$, ctr&, res&, fileheader$ 'Move to appropriate directory IF %VERBOSE THEN STDOUT "Scanning directory """ & startDir$ & """ "; CHDIR startDir$ 'Scan the current directory DIM Listing(%MAXDIRSIZE) AS DIRDATA temp$ = DIR$("*.*", %NORMAL OR %SUBDIR TO Listing(numentries&)) WHILE LEN(temp$) AND numentries& < %MAXDIRSIZE INCR numentries& temp$ = DIR$(NEXT, TO Listing(numentries&)) WEND IF %VERBOSE THEN STDOUT " [" & FORMAT$(numentries&, "0,") & " entries]" 'Check and correct files FOR ctr& = 1 TO numentries& IF Listing(ctr&).FileAttributes AND %NORMAL = %NORMAL THEN 'This will skip hidden and system files temp$ = TRIM$(Listing(ctr&).FileName) OPEN temp$ FOR BINARY AS #1 GET$ #1, 2, fileheader$ 'Start of file is "PK" for zip or "Rar!" for rar files (although we just grab the "Ra") CLOSE #1 IF UCASE$(RIGHT$(temp$, 4)) = ".CBR" AND fileheader$ = "PK" THEN 'It's labelled as a CBR but is actually a CBZ INCR res& STDOUT " Renamed to CBZ: """ & temp$ & """" NAME temp$ AS LEFT$(temp$, LEN(temp$) - 3) & "cbz" ELSEIF UCASE$(RIGHT$(temp$, 4)) = ".CBZ" AND fileheader$ = "Ra" THEN 'It's labelled as a CBZ but is actually a CBR INCR res& STDOUT " Renamed to CBR: """ & temp$ & """" NAME temp$ AS LEFT$(temp$, LEN(temp$) - 3) & "cbr" ELSEIF UCASE$(RIGHT$(temp$, 4)) = ".CBR" OR UCASE$(RIGHT$(temp$, 4)) = ".CBZ" THEN IF fileheader$ <> "PK" AND fileheader$ <> "Ra" THEN 'Might be a damaged file? STDOUT " Warning: " & temp$ & " appears damaged" END IF END IF END IF NEXT 'Recurse FOR ctr& = 1 TO numentries& IF Listing(ctr&).FileAttributes = %SUBDIR THEN res& = res& + DoDirectory(startDir$ & "\" & TRIM$(Listing(ctr&).Filename)) END IF NEXT FUNCTION = res& END FUNCTION