WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

Script to rename files in a folder.

Summary: Script to rename files in a folder. Written as a VBScript file.
 
Keywords: FILE
FILESYSTEMOBJECT
FSO
MOVEFILE
RENAME
VBS
Applicable Software:
 
Body:

I recently used a shareware program to create thumbnails of images. It created them with filenames that I just didn't care for. I needed a script to loop through a folder and rename all of the files it finds.

Dim sInputFolder
Dim oFs: Set oFs = CreateObject("Scripting.FileSystemObject")
	Main()
Set oFs = Nothing

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Main()
	GetArguments()

	RenameFiles(sInputFolder)
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetArguments()

Dim oArgs: Set oArgs = WScript.Arguments

	If oArgs.Count <> 1 Then
		WScript.Echo(INVALID_ARG_ERR)
		WScript.Quit(INVALID_ARG_RET)
	End If

	sInputFolder = oArgs(0)

	Set oArgs = Nothing

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RenameFiles(ByVal psFolderPath)

Dim iCnt
Dim oFolder
Dim oFile
	If oFs.FolderExists(psFolderPath) Then
		Set oFolder = oFs.GetFolder(psFolderPath)
		For Each oFile In oFolder.Files
			'### *** ### CHANGE THIS CODE TO CHECK FOR FILES THAT NEED TO BE RENAMED
			If InStr(1, oFile.Name, ".jpg_Resized") > 0 Then
				RenameFile(oFile.Path)
			End If
		Next
	Else
		WScript.Echo(Replace(FOLDER_MISSING_ERR, "$FolderName", psFolderPath))
		WScript.Quit(FOLDER_MISSING_RET)
	End If

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RenameFile(ByVal psFileName)

Dim aTemp
Dim sFilePath, _
	sFileName, _
	sFileName_Target

	aTemp = Split(psFileName, "\")
	sFileName = aTemp(UBound(aTemp))

	'### *** ### CHANGE THIS CODE TO END UP WITH THE TARGET FILENAME THAT YOU WANT
	sFileName_Target = Replace(sFileName, ".jpg_Resized.jpg", "_Resized.jpg")
	
	sFilePath = Left(psFileName, Len(psFileName) - Len(sFileName))

	On Error Resume Next
	If oFs.FileExists(psFileName) Then
		If oFs.FileExists(sFilePath & sFileName_Target) Then
			oFs.DeleteFile(sFilePath & sFileName_Target)
		End If
		
		oFs.MoveFile psFileName, sFilePath & sFileName_Target
	Else
		WScript.Echo(Replace(FILE_MISSING_ERR, "$FileName", psFileName))
		WScript.Quit(FILE_MISSING_RET)
	End If
	On Error Goto 0

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const INVALID_ARG_RET = 1
Const INVALID_ARG_ERR = "You must provide a path of the files you wish to rename."

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FILE_MISSING_RET = 51
Const FILE_MISSING_ERR = "File Does Not Exist: $FileName"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FOLDER_MISSING_RET = 2
Const FOLDER_MISSING_ERR = "Folder Does Not Exist: $FolderPath"

To use it, you would do save the code to a folder on your hard-drive. For this example, I'll be saving it to: "C:\fileRenamer.vbs". Then, to run the script, go to Start --> Run. At the prompt, type in:

C:\fileRenamer.vbs "C:\Path\To\Files\That\Need\Renamed\"

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 9/23/2003 10:38:26 AM

Rate this article: Average: 9
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.