Can't delete files in both root and folder, just one of them

I have a problem with file deletion. It's on this row 8. When the "\" is there I can delete files in any folder, but not in the root. (C:\\). If I remove the "\" I can delete files in the root directory, but then it doesn't work to delete files in folders. Can anyone help me to solve how to delete files in folders and in the root directory at the same time?

Here are the codes I use:

1. Dim r As Long
2. Dim file2Delete As String
3. Dim fPath As String
4. Dim ActionFlag As Long

5. fPath = Dir1.Path
6. file2Delete = File1.filename

7. file2Delete = File1.List(File1.ListIndex)
8. file2Delete = fPath & "\" & file2Delete

9. ActionFlag = 0&
10. If optDeleteOption(0).Value Then ActionFlag = FOF_ALLOWUNDO

11. ShellDeleteOne file2Delete, ActionFlag

It will mean a lot to me if anyone solve this.
Who is Participating?
halschConnect With a Mentor Commented:
Between lines 5 and 6 add:
If Right(fPath,1)="\" then fPath = Left(fPath, Len(fPath)-1))

This will assure that the Directory does NOT end with a "\".  You can then concatenate a "\" without problems in line 8.

The conditional code will be executed only in the root directory.

Alternatively, you can remove the '& "\"' from line 8 and after line 5, add:
If Right(fPath,1)<>"\" then fPath = fPath & "\"

Either way should work.

Good Luck!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.