QTP tutorials & interview questions


Search more on QTP here

QTP Script 30 - How to rename all files in a folder and its subfolder?

At the end of the root procedure, it shows “path and names of files which are renamed”. This msgbox is useful if the number of files is very small. If files are in huge numbers then modify this code if you want to see the names and path of all the files renamed or if you don’t want this you can simply convert that msgbox into a comment.

Write this code on a notepad and save it with .vbs extension. It asks you the path of the folder and the string to be replaced in each filename and the new string by which the old string is to be replaced.

Set fso = CreateObject ("Scripting.FileSystemObject")
Dim uu, uu1
total_files_renamed = 0
total_files_skipped = 0

root
set fso = nothing

Sub root
path = inputbox ("enter path")
old_string=inputbox ("enter string to be replaced")
new_string=inputbox ("enter new string")

Set folder_path = fso.GetFolder(path)

msgbox "Warning: All files within the Folder """ & _
folder_path.Path & """ will be renamed."

procedure_sub_folders folder_path , old_string, new_string

Msgbox "Total Files Renamed :" & total_files_renamed
Msgbox "Total Files Skipped :" & total_files_skipped
msgbox "Path and names of files which are renamed" & uu1
End Sub

Sub procedure_sub_folders (ByVal curr_folder, ByVal old_val, ByVal new_val)
Set Folders = curr_folder.SubFolders
procedure_folder curr_folder , old_val, new_val

For Each Folder in Folders
procedure_sub_folders Folder , old_val, new_val
next
end Sub

Sub procedure_folder (ByVal folder, ByVal old_val, ByVal new_val)
Set Files = folder.Files
For Each File In Files
If inStr(1,File.Name,old_val) > 0 Then
uu= Replace(File.Path, old_val, new_val)
uu1= uu1 & vbcrlf & uu
File.Move Replace(File.Path,old_val,new_val)
total_files_renamed = total_files_renamed + 1
else
total_files_skipped = total_files_skipped + 1
End If
Next
End Sub