Results 1 to 8 of 8

Thread: Remove special characters from all file names within a folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    4

    Question Remove special characters from all file names within a folder

    Hello brains trust, I hope you can help me, please.

    I have a number of documents to upload into a system that does not permit upload where file names contain special characters, being: / : * ? " <> | # { } % ~ &"."

    Is there a VB script I can use to cycle through all the files in a folder and remove all of the above characters from the file names, but otherwise leave them the same? The characters do not necessarily appear in the same position in each file name, so functions using specific character positions won't work.

    Thanks in advance for your assistance!

    Cheers
    Dots

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,488

    Re: Remove special characters from all file names within a folder

    Possibly useful link:

    https://superuser.com/questions/1224...and-subfolders

    The script could easily be modified to remove any character NOT in a string of characters that you supply (upper and lower case letters, digits, and a space).

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    4

    Re: Remove special characters from all file names within a folder

    Quote Originally Posted by jdc2000 View Post
    Possibly useful link:

    https://superuser.com/questions/1224...and-subfolders

    The script could easily be modified to remove any character NOT in a string of characters that you supply (upper and lower case letters, digits, and a space).
    Thank you for the suggestion. I am not very experienced with VB, but attempting to run the code referred to in the linked article produces the following error message:

    Compile error: Invalid outside procedure.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,021

    Re: Remove special characters from all file names within a folder

    Quote Originally Posted by JoinintTheDots View Post
    Hello brains trust, I hope you can help me, please.

    I have a number of documents to upload into a system that does not permit upload where file names contain special characters, being: / : * ? " <> | # { } % ~ &"."

    Is there a VB script I can use to cycle through all the files in a folder and remove all of the above characters from the file names, but otherwise leave them the same? The characters do not necessarily appear in the same position in each file name, so functions using specific character positions won't work.

    Thanks in advance for your assistance!

    Cheers
    Dots
    ... and i really doubt, that all of those characters appear in your filenames, since with the exception of the tilde ("~") i'm pretty sure, they are ILLEGAL in Filenames on Windows (except the dot "." with the caveat that a dot is allowed, but the filename is not allowed to start with it)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    4

    Re: Remove special characters from all file names within a folder

    Thanks @Zvoni, however the area that is doing the work is also dealing with historical files, not just current ones and the system to which they are uploading is a bespoke application.

    It doesn't change the nature of the question, however - the code will look pretty similar aside from the string of special characters it needs to filter. I know for a fact that some file names do have a # in them and others have one or more "."

  6. #6
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,488

    Re: Remove special characters from all file names within a folder

    To eliminate the error, try this:

    Code:
    Option Explicit
    
    Dim sNewFile
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Browsefolder objFso.GetFolder("C:\Users\user\Desktop\test")
    
    Sub Browsefolder(Folder)
        For Each Subfolder In Folder.Subfolders
            Browsefolder Subfolder
        Next
        For Each File In Folder.Files
            sNewFile = File.Name
            sNewFile = Replace(sNewFile, "ç", "+")
            If (sNewFile <> File.Name) Then
                File.Move (File.ParentFolder & "\" & sNewFile)
            End If
        Next
    End Sub
    Note that you would need to add code to make sure that you do not remove the rightmost "." for the file extension if you update this to remove the "." characters.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    4

    Re: Remove special characters from all file names within a folder

    Hi @jdc2000 - thank you for responding. Unfortunately, I am still getting the "Compile error: Invalid outside procedure" error for the first "Set" command when I run the code.

    Also, any advice on retaining the right-most "." before the file extension would be appreciated.

    Thanks again for your help!

  8. #8
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,488

    Re: Remove special characters from all file names within a folder

    Try this:

    Code:
    Option Explicit
    
    Dim sNewFile, objFso
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Browsefolder objFso.GetFolder("C:\Users\user\Desktop\test")
    
    Sub Browsefolder(Folder)
        For Each Subfolder In Folder.Subfolders
            Browsefolder Subfolder
        Next
        For Each File In Folder.Files
            sNewFile = File.Name
            sNewFile = Replace(sNewFile, "ç", "+")
            If (sNewFile <> File.Name) Then
                File.Move (File.ParentFolder & "\" & sNewFile)
            End If
        Next
    End Sub
    To avoid replacing the rightmost ".", you could use InStrRev to get the position (assuming that there is a "."), and then not replace the "." at that position.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width