Computer Science Canada

Mass File Extension Converter

Author:  Zren [ Sun Mar 08, 2009 12:39 pm ]
Post subject:  Mass File Extension Converter

Should you ever come accross a heptillion of files with the wrong file extension.
Contains a hacky slashy version of Java's split(). For some reason I couldn't get it too work as a flexible when used as a flexible array.

Turing:
View.Set ("graphics:480;max,nobuttonbar,title:Mass File Extension Converter")
colourback (black)
colour (brightgreen)
cls

var streamNumber : int
var fileName : string
streamNumber := Dir.Open (Dir.Current)
assert streamNumber > 0
var fileProbe := false

var file : flexible array 1 .. 0 of string

var t : flexible array 1 .. 0 of string

procedure split (p : string)
    var l := 1
    for i : 1 .. length (p)
        if p (i) = "." then
            new t, upper (t) + 1
            t (upper (t)) := p (l .. i - 1)
            l := i + 1
        elsif i = length (p) then
            new t, upper (t) + 1
            t (upper (t)) := p (l .. i)
        end if
    end for
end split

function fileExt (s : string) : string
    split (s)
    result t (upper (t))
end fileExt

function fileStruct (s : string) : string
    var w : string := ""
    split (s)
    for i : 1 .. upper (t) - 1
        w += t (i)
        if i < upper (t) - 1 then
            w += "."
        end if
    end for
    result w
end fileStruct

process getFiles
    loop
        fileName := Dir.Get (streamNumber)
        exit when fileName = ""
        new file, upper (file) + 1
        file (upper (file)) := fileName
    end loop
    fileProbe := true
end getFiles


var EXT1 : string := ""
var EXT2 : string := ""

put "Mass File Extension Changer"
put "Current Extension (No .) [Eg: html]", skip, "  > " ..
get EXT1 : *
put "Wanted Extension", skip, "  > " ..
get EXT2 : *

fork getFiles
var d := 0
loop
    cls
    put "Probing Files" ..
    for z : 1 .. d
        put "." ..
    end for
    put ""
    d += 1
    if d > 3 then
        d := 1
    end if
    exit when fileProbe = true
end loop
cls

var total : int := 0
var q : int := Time.Elapsed
for i : 3 .. upper (file)
    color (white)
    if Str.Lower (fileExt (file (i))) = Str.Lower (EXT1) then
        new t, 0
        color (red)
        var newf : string := fileStruct (file (i)) + "." + Str.Lower (EXT2)
        File.Rename (file (i), newf)
        new t, 0
        total += 1
        put file (i), " -> ", newf ..
    else
        put file (i) ..
    end if
    put ""
    new t, 0
end for
Dir.Close (streamNumber)

colour (brightgreen)
put skip, "Renamed ", total, " files from ", EXT1, " to ", EXT2
put "Took ", (Time.Elapsed - q) div 1000, " seconds."

Author:  Tallguy [ Wed Mar 11, 2009 7:57 am ]
Post subject:  RE:Mass File Extension Converter

interesting concept, i like how you did it, how long did it take?

Author:  blankout [ Mon Apr 27, 2009 7:03 pm ]
Post subject:  Re: Mass File Extension Converter

good, the only problem i found is that it doesn't say how long it took to convert the files, i only ever get a 0. Maybe its just me but thought I'd point it out anyway

Author:  bbi5291 [ Mon Apr 27, 2009 8:21 pm ]
Post subject:  Re: Mass File Extension Converter

Another shameless post in an effort to raise my post count
Zren @ Sun Mar 08, 2009 12:39 pm wrote:
Should you ever come accross a heptillion of files with the wrong file extension.

It's "septillion", not "heptillion" (short scale 10^24, long scale 10^42)

Author:  tjmoore1993 [ Tue Apr 28, 2009 7:08 pm ]
Post subject:  RE:Mass File Extension Converter

I like the idea but there isn't much use for it because converting process only works with files written in ASCII format basically. Correct me if I am wrong. Anyways I tried it and just so I can say I used it.


I converted some project files to a new extension. Thanks! Smile


: