-
Mar 6th, 2024, 06:05 PM
#1
Thread Starter
New Member
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
-
Mar 6th, 2024, 07:17 PM
#2
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).
-
Mar 6th, 2024, 07:24 PM
#3
Thread Starter
New Member
Re: Remove special characters from all file names within a folder
 Originally Posted by jdc2000
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.
-
Mar 7th, 2024, 03:24 AM
#4
Re: Remove special characters from all file names within a folder
 Originally Posted by JoinintTheDots
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
-
Mar 7th, 2024, 04:55 AM
#5
Thread Starter
New Member
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 "."
-
Mar 7th, 2024, 10:38 AM
#6
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.
-
Mar 11th, 2024, 06:30 PM
#7
Thread Starter
New Member
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!
-
Mar 12th, 2024, 10:06 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|