Monday, February 16, 2009

QTP Script 29 - How to rename all the files in a folder?

Type the below code in a notepad, save it with .vbs extension and run it from command prompt. You have to change the folder name (C:\abc) and the strname to suit your requirements.

Below code renames all the files in a given folder to Sac 1, Sac 2....and so on.

strComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_Directory.Name='C:\abc'} Where " & "ResultClass = CIM_DataFile")
For Each objFile In colFileList
strname = "Sac " & x
strNewName = objFile.Drive & objFile.Path & strname & "." & objFile.Extension
errResult = objFile.Rename(strNewName)


To know more about Windows Management Instrumentation go here.