Hi everyone.
Not sure if this is the appropriate forum but I thought I saw the ability to do something similar with Access/VBA.
I have a folder that contains >100 files with file names that include multiple # characters. Is it possible to remove the # from all the file's filenames in the directory with VBA?
I thought I would start here.
Thank you for any assistance.
-Scott
Remove characters from filenames in a folder
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove characters from filenames in a folder
I have moved this thread from the Access forum to the VB/VBA forum since it is a general VBA question, not specific to Access.
I'll post a reply soon.
I'll post a reply soon.
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove characters from filenames in a folder
Try this. The macro can be run from any Office application.
Code: Select all
Sub RemoveHash()
Dim strFolder As String
Dim strFile As String
' Modify as needed, keep trailing backslash
strFolder = "C:\MyFiles\"
strFile = Dir(strFolder * "*.*")
Do While strFile <> ""
If InStr(strFile, "#") Then
Name strFolder & strFile As strFolder & Replace(strFile, "#", "")
End If
strFile = Dir
Loop
End Sub
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
Thanks Hans I will give it a try.
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
Hi Hans,
I updated the path and tried to run in Excel. I received a run time error 13 type mismatch.
Sub RemoveHash()
Dim strFolder As String
Dim strFile As String
' Modify as needed, keep trailing backslash
strFolder = "C:\Users\OIK6179\Documents\Project Services\Aurora-Backup\"
strFile = Dir(strFolder * "*.*")
Do While strFile <> ""
If InStr(strFile, "#") Then
Name strFolder & strFile As strFolder & Replace(strFile, "#", "")
End If
strFile = Dir
Loop
End Sub
Thanks.
I updated the path and tried to run in Excel. I received a run time error 13 type mismatch.
Sub RemoveHash()
Dim strFolder As String
Dim strFile As String
' Modify as needed, keep trailing backslash
strFolder = "C:\Users\OIK6179\Documents\Project Services\Aurora-Backup\"
strFile = Dir(strFolder * "*.*")
Do While strFile <> ""
If InStr(strFile, "#") Then
Name strFolder & strFile As strFolder & Replace(strFile, "#", "")
End If
strFile = Dir
Loop
End Sub
Thanks.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
It doesn't flag any line.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove characters from filenames in a folder
Try clicking Debug in the error message.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
In the error message dialog box there is only ok and help. When I debug/step into from the menu the first line Sub RemoveHash() is highlighted.
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove characters from filenames in a folder
In the Visual Basic Editor, select Tools > Options...
Activate the General tab.
In the Error Trapping section, select 'Break in Class Module'.
Click OK.
Now try to run the code again. Does the error message now have a Debug button?
Activate the General tab.
In the Error Trapping section, select 'Break in Class Module'.
Click OK.
Now try to run the code again. Does the error message now have a Debug button?
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
Yes it did and stopped on:
strFile = Dir(strFolder * "*.*")
strFile = Dir(strFolder * "*.*")
-
- Administrator
- Posts: 78629
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove characters from filenames in a folder
Thanks - my mistake. That line should have been:
Sorry about that!
Code: Select all
strFile = Dir(strFolder & "*.*")
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Remove characters from filenames in a folder
Thank you very much for your help Hans. This is going to save many hours of tedious work.