Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2022, 11:08 AM
ranjan ranjan is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Advanced Beginner
Bulk File Rename
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default 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.
Attached Images
File Type: jpg Rename.jpg (40.5 KB, 32 views)
Reply With Quote
  #2  
Old 05-11-2022, 10:48 AM
ranjan ranjan is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Advanced Beginner
Bulk File Rename
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 05-13-2022, 02:38 PM
ranjan ranjan is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Advanced Beginner
Bulk File Rename
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default

Hi,

Please anyone look into the above code and do the needful.
Reply With Quote
  #4  
Old 05-13-2022, 09:50 PM
Peterson Peterson is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 05-16-2022, 02:50 AM
ranjan ranjan is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Advanced Beginner
Bulk File Rename
 
Join Date: May 2021
Posts: 76
ranjan is on a distinguished road
Default

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...
Reply With Quote
  #6  
Old 05-16-2022, 09:45 PM
Peterson Peterson is offline Bulk File Rename Windows 10 Bulk File Rename Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

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.
Attached Files
File Type: dotm BulkRename.dotm (33.0 KB, 11 views)
Reply With Quote
Reply

Tags
bulk, rename file

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bulk File Rename 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
Bulk File Rename 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft