PDA

View Full Version : Stupid windows script to rename files


garyamort
02-06-2007, 07:47 AM
Had a client who wanted something incredible simple.

He had a spreadsheet with a list of labels and some descriptive text.

The tables would match the 4 charector key of a file name(ie label file is a match for fileA, fileB, and fileC)

He wanted the files renamed with the meaninfull descriptive text from the second column(so fileA becomes descriptionA) and move them to a new folder called with their name.

I could do this in perl or PHP easily, but then I'd have to get on the subway, go to his office, install perl on his machine, configure the modules I want to use(if I'm doing this bother there would be a few extra functions to include)

It struck me that I could do the same thing with visual basic, and Windows has a visual basic interpreter built into it.

This code isn't smart, and it isn't pretty, but it does precisely what I needed and I thought I'd share.

Note to invoke it, you would save the file as a text file(for example, c:\updatenames.vbs') and than invoke it from the command line by using cscript (cscript c:\updatenames.vbs)

' Tell the program where the files are
strPath = "j:\Documents and Settings\gmort"
' Tell the program to place the renamed JPG files are
strNewPath = "j:\documents and settings\gmort3"
' Tell the program the name of the text file, format is
' xxxxYYzzzzzzzzzzzzzzzzzzzzzY
' where xxxx is the current name pattern(4 chars long)
' YY is junk charectors to be ignored(in my file one of them was
' column delimieter. zzzzzzz is the descriptive text, variable length
' but I'm chopping off the last charector only because it was required
' for this project, you can keep the whole by making 1 change
strImageFileData = "j:\Documents and Settings\gmort\Rename_photos_sample.txt"

' Get a Windows FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open the text file with the definitions
Set objFile = objFSO.OpenTextFile(strImageFileData)

Do Until objFile.AtEndOfStream
' Process every line of the text file
szReadLine = objFile.ReadLine

' Get the 4 digit keyword
szPartA = trim(left(szReadLine, 4))
' Find the new name based on position
szPartB = right(szReadLine, len(szReadline) -6)
' Note, I am chopping off the last charector, change below from len(szPartB)-1 to len(szPartB) to use the full name
szPartB = left(szPartB, len(szPartB)-1)

' Get a file system object(is this redudant? Can I use the one already created?)
set fso = createobject("scripting.filesystemobject")

' Make the target directory(stupid function, will error if the new directory exists!)
strNewFolder = strNewPath & "\" & trim(szPartB)
WScript.Echo "Creating directory " & strNewFolder
fso.CreateFolder(strNewFolder)

set oFolder = fso.GetFolder(strPath)
' Process the source files
for each oFile in oFolder.Files
'Check out pattern
set oRegex = new Regexp
oRegex.Pattern = "^" & szPartA
if oRegex.Test(oFile.name) then
set mRegex = new Regexp
' Note, I only wanted jpg files so I did a second filter
mRegex.Pattern = ".jpg$"
if mRegex.Test(oFile.name) then
SavestrNewName = strNewFolder & "\" & szPartB & mid(oFile.name,5)
' Copy to new directory/filename
WScript.Echo "Match copying " & oFile.name & " to " & SavestrNewName
oFile.Copy(SavestrNewName)
end if
end if
next
loop