Tuesday, September 8, 2009

mp3 Woes Part 1

I don't like things that are hard. I really don't.

I also don't like things that are manual. I mean, really. Shouldn't technology be all about making life easier, not adding layers of abstraction to manually processes? Shouldn't it get rid of the manual processes, and free us up to review log files and tweak the underlying processes?

Or have I been a DBA for too long? :)

So I wanted to rename a bunch of files. At once, automagically, but with some manual oversight. After all, I might come up with some files that I didn't know about, didn't like the renaming of, etc.

So here's what I came up with as requirements:

  1. I want a script that will traverse all subdirectories
  2. However, I have no interest in passing parameters (see above comment on "automatic"), so it needs to run starting in whatever directory it wakes up in
  3. It should produce a text file that I can edit before running
  4. It should not try to use iTunes API

So here's the full text of the script that I came up with, that satisfies these requirements.

' This script will recurse a directory tree, from the location the file is run from,
' and will produce a batch file to run that will automatically un-url-encode all files
' it runs into

Function ShowFileAccessInfo(filespec)
' This function will get the details of the file it's given
' and return the details as a formatted string, intended to be
' output to the report file later
Dim fso, f, myString
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
myString = "REM Original File: " & f.Path & vbcrlf
myString = myString & "REM New Name: " & unescape(f.Name) & vbcrlf
myString = myString & "REM on Drive " & UCase(f.Drive) & vbcrlf
myString = myString & "REM Last Modified: " & f.DateLastModified & vbcrlf
myString = myString & "move "".\" & replace(f.Name, "%", "%%") & """ "".\" & unescape(f.Name) & """ "
myString = myString & vbcrlf & vbcrlf
ShowFileAccessInfo = myString
End Function

Function ShowFolderList(folderspec)
' This function takes a folder it's given, and loops through
' the collection of objects, calling the file describer
' function for each one
' At the end, it returns the formatted string back
' to the calling function

Dim fso, f, sf1, f1, fdc, fc, myString
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
set fdc = f.SubFolders

' loop any found subdirectories
for each sf1 in fdc
myString = myString & ShowFolderList(sf1)

' loop any found files
For Each f1 in fc
myString = myString & ShowFileAccessInfo(f1)

' because if you don't return what you find, this whole
' write it to disk thing is for naught...
ShowFolderList = myString

End Function

' so here we begin the main portion of the script
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")

' Open the file for output.
FileName = ".\FileList.txt"
Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)

' Write the result of the function directly to the file
MyFile.WriteLine showfolderlist(".")

' Because you have to close every door you open