DEFLNG a-z FUNCTION PBMAIN() cmd$ = TRIM$(COMMAND$) IF cmd$ = "" THEN STDOUT "RegRen" STDOUT "This software creates REGEX-based batch commands for file processing." STDOUT "by Shannon Larratt - glider.bmezine.com" STDOUT STDOUT "Usage: RegRen [regex] u|l|r|c [""command""] [-opt] [-opt]" STDOUT " regex = Regular expression to match (all files if omitted)" STDOUT " u = Command: convert matches to uppercase" STDOUT " l = Command: convert matches to lowercase" STDOUT " r = Command: Do regex rename" STDOUT " ""command"" = Regex to replace with regex, in form ""x with y""" STDOUT " c = Command: Execute command macro (or just list files if none)" STDOUT " ""command"" = Command string to execute; *file* is filename" STDOUT " -d = Option: Operate on directories, not filenames" STDOUT " -c = Option: Suppress CHDIR commands" STDOUT " -q = Option: Put quotes around names" STDOUT " -l = Option: Large mode (250,000)" STDOUT " -g = Option: Giant mode (1,000,000)" STDOUT " -i = Option: Output additional info" STDOUT " >file.ext = Output filename (ie. use piping)" EXIT FUNCTION END IF infomode = 0 IF INSTR(cmd$, "-i") > 0 THEN infomode = 1 reregex: regex$ = PARSE$(cmd$, " ", 1) IF regex$ = "u" OR regex$ = "l" OR regex$ = "c" OR regex$ = "r" THEN cmd$ = ".* " & cmd$ GOTO reregex END IF IF infomode = 1 THEN STDOUT "Processing Regex: " & regex$ rootdir$ = CURDIR$ IF infomode = 1 THEN STDOUT "Root dir: " & rootdir$ SELECT CASE PARSE$(cmd$, " ", 2) CASE "u" thisaction = 1 IF infomode = 1 THEN STDOUT "Action: Uppercase" CASE "l" thisaction = 2 IF infomode = 1 THEN STDOUT "Action: Lowercase" CASE "c" thisaction = 3 firstquote = INSTR(cmd$, """") secondquote = INSTR(firstquote+1, cmd$, """") IF secondquote > firstquote+1 THEN cmdstring$ = MID$(cmd$, firstquote + 1, secondquote - firstquote - 1) ELSE cmdstring$ = "*file*" END IF IF infomode = 1 THEN STDOUT "Action: Command """ & cmdstring$ & """" CASE "r" thisaction = 4 firstquote = INSTR(cmd$, """") secondquote = INSTR(firstquote+1, cmd$, """") IF secondquote > firstquote+1 THEN cmdstring$ = MID$(cmd$, firstquote + 1, secondquote - firstquote - 1) IF INSTR(cmdstring$, " with ") > 1 THEN reg1$ = TRIM$(PARSE$(cmdstring$, " with ", 1)) REPLACE "~~" WITH " " IN reg1$ reg2$ = TRIM$(PARSE$(cmdstring$, " with ", 2)) REPLACE "~~" WITH " " IN reg2$ END IF IF reg1$ = "" OR reg2$ = "" THEN STDOUT "Error: ""regex with regex"" string appears incomplete or corrupted" EXIT FUNCTION END IF ELSE STDOUT "Error: ""regex with regex"" string not found" EXIT FUNCTION END IF IF infomode = 1 THEN STDOUT "Action: RegRepl """ & reg1$ & """ with """ & reg2$ & """" END SELECT IF INSTR(cmd$, "-d") > 0 THEN filemode = 2 IF infomode = 1 THEN STDOUT "Mode: Directory" ELSE filemode = 1 'directory mode IF infomode = 1 THEN STDOUT "Mode: File" END IF IF INSTR(cmd$, "-c") > 0 THEN includechdir = 2 IF infomode = 1 THEN STDOUT "ChDir: Suppressed" ELSE includechdir = 1 'chdir? IF infomode = 1 THEN STDOUT "ChDir: Included" END IF IF INSTR(cmd$, "-q") > 0 THEN quotemode = 1 IF infomode = 1 THEN STDOUT "Quotes: On" ELSE quotemode = 0 'quotes? IF infomode = 1 THEN STDOUT "Quotes: Off" END IF arraysize = 50000 IF INSTR(cmd$, "-l") > 0 THEN arraysize = 250000 IF INSTR(cmd$, "-g") > 0 THEN arraysize = 1000000 IF infomode = 1 THEN STDOUT "Array size: " & FORMAT$(arraysize, ",") IF MID$(rootdir$, 2, 1) = ":" THEN CHDRIVE LEFT$(rootdir$, 1) END IF CHDIR rootdir$ SHELL "cmd /c dir /s > regren.tmp" DIM filelist$(1 TO arraysize) numfiles = 0 potfiles = 0 numdir = 0 currentdir$ = "" OPEN "regren.tmp" FOR INPUT AS #1 DO UNTIL EOF(1) LINE INPUT #1, x$ x$ = TRIM$(x$) IF LEFT$(x$, 12) = "Directory of" THEN INCR numdir currentdir$ = MID$(x$, 14) IF filemode = 2 THEN 'dir mode INCR potfiles eodir$ = MID$(currentdir$, INSTR(-1, currentdir$, "\")+1) REGEXPR regex$ IN eodir$ TO posvar, lenvar IF posvar = 1 AND lenvar = LEN(eodir$) THEN 'full match INCR numfiles filelist$(numfiles) = currentdir$ END IF END IF ELSEIF LEN(x$) > 39 THEN IF filemode = 1 THEN INCR potfiles tim$ = MID$(x$, 19, 2) IF (tim$ = "AM" OR tim$ = "PM") AND MID$(x$, 25, 5) <> "" THEN eofile$ = MID$(x$, 40) IF LCASE$(eofile$) = "regren.tmp" THEN EXIT IF REGEXPR regex$ IN eofile$ TO posvar, lenvar IF posvar = 1 AND lenvar = LEN(eofile$) THEN 'full match INCR numfiles filelist$(numfiles) = currentdir$ & "*" & eofile$ END IF END IF END IF END IF IF potfiles => arraysize THEN EXIT DO LOOP CLOSE #1 KILL "regren.tmp" FLUSH IF numfiles = 0 THEN STDOUT "Error: No matches on the regex! (" & FORMAT$(potfiles, ",") & " files/dirs checked)" EXIT FUNCTION END IF IF infomode = 1 THEN STDOUT "Pre-match array size: " & FORMAT$(numfiles, ",") arraysize = arraysize + numdir DIM batchlist$(1 TO arraysize) batchlines = 0 thisdir$ = "" FOR r = 1 TO numfiles IF filemode = 1 THEN 'files proc$ = filelist$(r) IF includechdir = 1 THEN 'chop down proc$ = MID$(proc$, INSTR(-1, proc$, "*")+1) tproc$ = proc$ ELSE tproc$ = MID$(proc$, INSTR(-1, proc$, "*")+1) REPLACE "*" WITH "\" IN proc$ END IF tempquote = quotemode IF INSTR(proc$, " ") > 0 THEN tempquote = 1 SELECT CASE thisaction CASE 1 'ucase IF UCASE$(tproc$) <> tproc$ THEN GOSUB idir INCR batchlines IF tempquote = 1 THEN batchlist$(batchlines) = "ren """ & proc$ & """ """ & UCASE$(proc$) & """" ELSE batchlist$(batchlines) = "ren " & proc$ & " " & UCASE$(proc$) END IF END IF CASE 2 'lcase IF LCASE$(tproc$) <> tproc$ THEN GOSUB idir INCR batchlines IF tempquote = 1 THEN batchlist$(batchlines) = "ren """ & proc$ & """ """ & LCASE$(proc$) & """" ELSE batchlist$(batchlines) = "ren " & proc$ & " " & LCASE$(proc$) END IF END IF CASE 3 'custom GOSUB idir thiscmd$ = cmdstring$ IF tempquote = 1 THEN REPLACE "*file*" WITH """" & proc$ & """" IN thiscmd$ ELSE REPLACE "*file*" WITH proc$ IN thiscmd$ END IF INCR batchlines batchlist$(batchlines) = thiscmd$ CASE 4 'regrepl newname$ = proc$ & CHR$(255) 'workaround for regex bug? resser = 0 ctr = 0 DO INCR ctr IF ctr > 500 THEN EXIT DO REGREPL reg1$ IN newname$ WITH reg2$ TO resser, newname$ LOOP UNTIL resser = 0 IF LEN(newname$) > 1 THEN newname$ = LEFT$(newname$, LEN(newname$)-1) ELSE newname$ = "" END IF IF newname$ <> proc$ THEN GOSUB idir IF INSTR(newname$, " ") > 0 THEN tempquote = 1 INCR batchlines IF tempquote = 1 THEN batchlist$(batchlines) = "ren """ & proc$ & """ """ & newname$ & """" ELSE batchlist$(batchlines) = "ren " & proc$ & " " & newname$ END IF END IF END SELECT ELSE 'dirs thisdir$ = filelist$(r) tthisdir$ = MID$(thisdir$, INSTR(-1, thisdir$, "\")+1) tempquote = quotemode IF INSTR(thisdir$, " ") > 0 THEN tempquote = 1 SELECT CASE thisaction CASE 1 'ucase IF UCASE$(tthisdir$) <> tthisdir$ THEN INCR batchlines IF tempquote = 1 THEN batchlist$(batchlines) = "ren """ & thisdir$ & """ """ & UCASE$(thisdir$) & """" ELSE batchlist$(batchlines) = "ren " & thisdir$ & " " & UCASE$(thisdir$) END IF END IF CASE 2 'lcase IF LCASE$(tthisdir$) <> tthisdir$ THEN INCR batchlines IF tempquote = 1 THEN batchlist$(batchlines) = "ren """ & thisdir$ & """ """ & LCASE$(thisdir$) & """" ELSE batchlist$(batchlines) = "ren " & thisdir$ & " " & LCASE$(thisdir$) END IF END IF CASE 3 'custom thiscmd$ = cmdstring$ IF tempquote = 1 THEN REPLACE "*file*" WITH """" & thisdir$ & """" IN thiscmd$ ELSE REPLACE "*file*" WITH thisdir$ IN thiscmd$ END IF INCR batchlines batchlist$(batchlines) = thiscmd$ END SELECT END IF NEXT r IF batchlines = 0 THEN STDOUT " Error: No matches inside file list" ELSE IF infomode = 1 THEN STDOUT "Lines in batch: " & FORMAT$(batchlines, ",") & $CRLF FOR r = 1 TO batchlines STDOUT batchlist$(r) NEXT END IF EXIT FUNCTION idir: 'this is so ugly! newdir$ = MID$(filelist$(r), 1, INSTR(-1, filelist$(r), "*")-1) IF newdir$ <> thisdir$ THEN IF includechdir = 1 THEN INCR batchlines tempquote2 = quotemode IF INSTR(newdir$, " ") > 0 THEN tempquote2 = 1 IF tempquote2 = 1 THEN batchlist$(batchlines) = "chdir """ & newdir$ & """" ELSE batchlist$(batchlines) = "chdir " & newdir$ END IF END IF thisdir$ = newdir$ END IF RETURN END FUNCTION