Greymeister.net

Fighting With Filenames

I was playing a little Sega CD the other day (actually a CDX but who’s keeping track), but I was having some problems with a disc which was a bit scuffed up. I figured now that it is mostly readable I’d better back it up while I had the chance. Sega CD discs were sort of tricky for noobs like me back in the day because they are multi-track discs, with one data track and a number of audio tracks. Usually, even today, I resort to something like Nero Burning ROM when simple programs like Disk Utility do not properly handle these discs. If you back them up as MP3s and ISO you get the benefit of data compression, as well as playability with emulators. I prefer to play games that way sometimes because I don’t have to mess with component/composite cables on a receiver that isn’t easy to get to.

The only frustrating thing about this process is that the naming scheme of the MP3s and the ISO are required to follow a specific format, or this won’t work. This is from the GENS readme:

Gens.txt
1
2
3
4
5
6
7
8
9
10
For Gens to correctly identify any .mp3 audio tracks for your CD images they
have to be placed in the same directory as the CD image file with the same
file name as the CD image followed by a track number. For example if you are
using a CD image called Blaster.iso with three .mp3 files you would need to
put all the files in the same directory and rename them like this:

     Blaster.iso
     Blaster 02.mp3
     Blaster 03.mp3
     Blaster 04.mp3

While dealing with four or five files like the above example wouldn’t be that bad, one game in particular had over 74 audio files! I was using my HTPC because it was the only Windows machine that Nero could run on, and I needed a quick solution to prefix the MP3 files properly, as well as the remove the “Unknown Artist - Unknown Title” crap Nero was appending to the extracted MP3 files. They aren’t pretty or anything but here’s a couple of VBScript files I used to do this. The first was adding the prefix (like “Blaster ” in the example). The second removed a substring, “Unknown Arist - Unknown Title”, by replacing it with an empty string. I could have used a better language but didn’t want to bother with installing anything else on the machine. Thanks goes to this site for the majority of the code.

prefix.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dim StdIn:  Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso:    Set fso = CreateObject("Scripting.FileSystemObject")

 'get the parameter list
dim objArgs: Set objArgs = WScript.Arguments
dim path: path = objArgs(0)  'path
 dim news: news = objArgs(1)  'new string
 dim file, newFileName

dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)
For Each file In CurrentFolder.Files
    newFileName = news  & file.Name
    fso.MoveFile file.Path,file.ParentFolder.Path & "\" & newFileName
Next
rename.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Option Explicit

Dim StdIn:  Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso:    Set fso = CreateObject("Scripting.FileSystemObject")

Dim FilesRenamed:   FilesRenamed = 0
Dim FilesSkipped:   FilesSkipped = 0
Dim intAnswer

Main

set fso = nothing

Sub ProcessFolder (ByVal folder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
   Dim Files: Set Files = folder.Files

   Dim File
   For Each File In Files

      If inStr(1,File.Name,oldTag) > 0 Then

         if (extOld <> "" and extNew <> "") then
            StdOut.Echo Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
            File.Move Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
         else
            StdOut.Echo Replace(File.Path,oldTag,newTag)
            File.Move Replace(File.Path,oldTag,newTag)
         end if

         FilesRenamed = FilesRenamed + 1
      Else
         FilesSkipped = FilesSkipped + 1
       End If
   Next
End Sub

Sub ProcessSubFolders (ByVal crfolder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
  Dim Folders: Set Folders = crfolder.SubFolders
  Dim Folder 'process the current folder
  ProcessFolder crfolder , oldTag, newTag, extOld,extNew
  For Each Folder in FoldersNext
      ProcessSubFolders Folder , oldTag, newTag, extOld,extNew
    Next
End Sub

Sub Main

   'get the parameter list
   dim objArgs: Set objArgs = WScript.Arguments

   if objArgs.Count > 2 then

    dim path: path = objArgs(0)  'path
    dim olds: olds = objArgs(1)  'string to replace
    dim news: news = objArgs(2)  'new string

    dim ext1: ext1 = ""
    dim ext2: ext2 = ""
    if objArgs.Count > 3 then ext1 = objArgs(3)  'old extension
    if objArgs.Count > 4 then ext2 = objArgs(4)  'new extension

    dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)
    intAnswer = _
    Msgbox("Warning: All files within the directory """ & _
                CurrentFolder.Path & """ will be renamed.", _
        vbYesNo, "Continue?")

If intAnswer <> vbYes Then
    Exit Sub
End If

    ProcessSubFolders CurrentFolder , olds, news, ext1,ext2

    StdOut.Echo "Files renamed :" & FilesRenamed
    StdOut.Echo "Files Skipped :" & FilesSkipped

   else
       StdOut.Echo "Usage: rename.vbs [folder path] [string to replace]" & _
                   " [new string] [old extension] [new extension] "
   end if

End Sub