#1
|
|||
|
|||
Bulk File Rename
Hi
I want to rename a all files in a folder. Existing name: 1234556_Audited.docx, 3243243_Audited.rtf, 12324_Audited.docx I want to remove “_followed text from a file name” I want to choose a folder location by a browse option as a Input folder & rename all file names as “removed “_” & followed text” (i.e 1234556.docx, 3243243.rtf, 12324.docx) Document format type is “ .docx & .rtf” If anyone helps in this regard, your help is highly appreciated. For more information, please look into the attachment. |
#2
|
|||
|
|||
Hi,
Below code is not working, could anyone help me in this regards... Your Help is Highly Appreciated. I want to remove the _Audited from a all files in the path. Ex: Input: 12345_Audited.docx, 455678_Audited.rtf Output: 12345.docx, 455678.rtf Code:
Sub BulkRename() Dim strPath As String Dim objFile As file Dim objFolder As Folder Dim objFSO As Scripting.FileSystemObject strPath = InputBox("Enter Files Path") strPath = strPath & "\" Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FolderExists(strPath) Then Set objFolder = objFSO.GetFolder(strPath) ' Loop through all files in the folder: For Each objFile In objFSO.GetFolder(strPath).Files If InStr(objFile.Name, "_Audited") = 1 And InStr(objFile.Name, ".docx" ".rtf") <> 0 Then '...then copy it with the I removed from the name, overwriting ' an existing version, if necessary: objFSO.CopyFile objFolder.path & "\" & objFile.Name, objFolder.path & "\" & Mid(objFile.Name, 8), True objFSO.DeleteFile (objFile.path) End If Next objFile End If End Sub Last edited by ranjan; 05-12-2022 at 02:11 AM. Reason: Need to add comment |
#3
|
|||
|
|||
Hi,
Please anyone look into the above code and do the needful. |
#4
|
|||
|
|||
Try this:
Code:
Sub BulkFileRename() ' 05/13/2022 ' This macro loops through all Word (.docx) and .rtf files in a user-selected ' folder. If a file contains a specified string, then the file is renamed, by ' removing the string. (Technically, a copy of the file is made with the new ' name, and the original is deleted.) If a file with that name already exists, ' then the old file is overwritten with the renamed file. Dim strToRemove As String, strPath As String, strNewName As String Dim objFile As File, objFolder As Folder Dim objFSO As Scripting.FileSystemObject ' String (case-sensitive) to remove from file names: strToRemove = "_Audited" ' Get the folder via function: strPath = fcnFolderPicker ' If the user didn't choose a folder, then exit sub: If strPath = "" Then Exit Sub End If Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strPath) ' Loop through all files in the folder: For Each objFile In objFSO.GetFolder(strPath).Files ' If the file has the string to be removed: If InStr(objFile.Name, strToRemove) <> 0 Then ' ...and the file is either a .docx or .rtf: If InStr(objFile.Name, ".docx") <> 0 Or InStr(objFile.Name, ".rtf") <> 0 Then ' ...then create a new file name by replacing the string to remove with nothing: strNewName = Replace(objFile.Name, strToRemove, "") End If ' Copy file with the string removed from the name; overwrite existing version, if necessary: objFSO.CopyFile objFolder.path & "\" & objFile.Name, objFolder.path & "\" & strNewName, True ' Delete the original file: objFSO.DeleteFile (objFile.path) End If Next objFile End Sub Function fcnFolderPicker() As String ' 05/13/2022 Dim FolderPicker As FileDialog Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker) With FolderPicker .Title = "Choose the folder containing your files" .AllowMultiSelect = False ' If the user cancels, then exit function: If .Show <> -1 Then Exit Function Else fcnFolderPicker = .SelectedItems(1) & "\" End If End With End Function |
#5
|
|||
|
|||
Hi Peterson,
Its working like a charm and you saved a lot of time in file renaming. I am so very grateful for your time. I had one more doubt regarding this, in the same way if we want to ADD "_Audited" to all the filenames in a folder, please provide a solution for this... I had basic knowledge on VBA, i tired to change " strToRemove" this to "strToAdd" Please help me... |
#6
|
|||
|
|||
The attached template contains a macro that will add and remove text from file names. There's a small input box to enter text -- to make it easier to specify the string to remove or add -- hence the template file.
|
Tags |
bulk, rename file |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Rename file when sending | emmanpelayo | Word VBA | 1 | 09-28-2016 10:05 PM |
Rename visio file & hyperlink | O'Neill | Visio | 0 | 05-08-2016 11:43 PM |
Save and rename attachments from ZIP FILE | AndyDDUK | Outlook | 1 | 03-03-2016 12:32 AM |
Rename File | cksm4 | Word VBA | 2 | 02-25-2011 09:29 AM |
How would I bulk select and copy highlights and then bulk paste them automatically? | copywriterpro | Word | 0 | 04-19-2010 05:21 AM |