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")
x=1
For Each objFile In colFileList
strname = "Sac " & x
strNewName = objFile.Drive & objFile.Path & strname & "." & objFile.Extension
errResult = objFile.Rename(strNewName)
x=x+1
Next

source:http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1109.mspx

To know more about Windows Management Instrumentation go here.
http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx