0

I've many VBA scripts and functions where I read a manually inserted (in Excel cell) local disk paths and I'm still confused about whether to normalize the inserted paths by adding or trimming the trailing backslash.

What do you use as the general standard? with or without the trailing backslash?

My confusion is fueled by the fact that for example ActiveWorkbook.path returns a path without trailing backslash while the CopyFile (FileSystemObject) method for the "Destination" parameter wants the trailing backslash otherwise it considers it as a file instead than a directory (and can give unexpected Permission denied errors)

6diegodiego9
  • 503
  • 3
  • 14
  • It doesn't matter what you use as long as your code checks and inserts the trailing backslash where needed. Or use the scripting.Filesystem's `BuildPath` method if you're on Windows. https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/buildpath-method – Tim Williams Aug 03 '20 at 16:19

1 Answers1

0

The general idea is to always end a directory's name with a backslash. This is advised in the URL you mention, but there are quite some other situations as well (check my answer on a regularly occuring xcopy problem).

As mentioned: when you don't put a backslash, the question might arise "Is it a file or a directory?". Putting the backslash solves that question.

In top of that, while programming, regularly you might have following situation:

complete_filename = path + filename

Obviously, if you have forgotten to put the backslash at the end, this might cause problems (e.g. you don't want to create a file, called "C:\Tempoutput.txt" instead of "C:\Temp\output.txt", I presume? :-) )

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • application.path and activeworkbook.path returns paths without the trailing backslash. Wouldn't it be risky to remember to use "path & filename" if path is one inserted by me and "path & "\" & filename" if path is one returned by the path property of some VBA object? – 6diegodiego9 Aug 03 '20 at 19:22