0

I would like to move up from my workbook directory to the folder above, or even two folders above, in the tree.

I found some hint here:
Go up one folder level
https://www.mrexcel.com/board/threads/up-directory-level-in-vba.594371/

I developed this code:

Sub folder()
 Dim ParentPath As String
 Dim Path As String
 Path = ThisWorkbook.Path
 ParentPath = Left$(Path, InStrRev(Path, "\"))
 ChDir ".."
End Sub

But, it still opens the folder at the level where my workbook is based.

Jim Simson
  • 2,774
  • 3
  • 22
  • 30
Geographos
  • 827
  • 2
  • 23
  • 57
  • Why are you using this? `ChDir ".."`? Your `ParentPath` variable should already have the path you need, although it does end with a backslash and it is not clear that is intentional. All your code does is get the parent path of `ThisWorkbook` - It does not "move" anything. Is that what you expected it to do? – braX Jul 30 '20 at 10:18
  • It does not move anything exactly. I press the code, but folder is not going up. – Geographos Jul 30 '20 at 10:23
  • What we need to know is, what is your intention with this code? Open a folder with the parent folder of your workbook? Save the workbook to the parent folder? Your code does nothing, just get the parent folder into a variable then that's it. – Damian Jul 30 '20 at 10:25
  • I want to open the folder which is above the parent folder of my workbook. – Geographos Jul 30 '20 at 10:26
  • 1
    Change your `ChDir` line for: `Shell "explorer.exe" & " " & ParentPath, vbNormalFocus` – Damian Jul 30 '20 at 10:28
  • Forget about `ChDir`. It does not open anything. Try `Debug.Print ParentPath`. It will return correctly. Open the folder in a different way... Or use the returned path being sure that it is correct. – FaneDuru Jul 30 '20 at 10:31
  • 2
    Maybe explain what you mean by "open" as it is very unclear, at least to me. – braX Jul 30 '20 at 10:33
  • 1
    If you want it to "move" the file, then you just save it to the parent folder using your `ParentPath` variable, and then delete the original copy. You aren't actually "moving" anything because you cannot "move" an open file. – braX Jul 30 '20 at 10:34
  • @Damian it works. How about the upper folders? If I would go up i.e. 3 folder levels, what should I do? I tried: Shell "explorer.exe" & " " & " " & " " & ParentPath, vbNormalFocus but it opens still the same folder – Geographos Jul 30 '20 at 10:37
  • @MKR: What do you mean? The 'upper folder' is correctly returned by the way you have in your code. – FaneDuru Jul 30 '20 at 10:39
  • @MKR The same way you got the parent folder, but instead giving the workbook path, give the parent folder path, and so on... You could build a function and loop until your parent folder was the root folder. – Damian Jul 30 '20 at 10:40
  • So it means, that instead of Path = ThisWorkbook.Path should I set Path = ParentPath? – Geographos Jul 30 '20 at 10:41
  • Yes, but that is a way, you could use a loop to go up to X folders and loop reseting the variable each time. – Damian Jul 30 '20 at 10:42
  • How can I code this? – Geographos Jul 30 '20 at 10:42
  • @FaneDuru the code I provided didn't work - I couldn't see the reaction. Now the Shell command proposed by Damian works great. However, I need something which can move me 2 levels above instead 1. – Geographos Jul 30 '20 at 10:46
  • Your problem code was not the way of 'Parent' definition. It was only saying "The 'upper folder' is correctly returned by the way you have in your code", which is correct... – FaneDuru Jul 30 '20 at 10:49

2 Answers2

0

You can create GetParentFolder(path, level) function to return path on defined levels up.

Usage:

Sub Test()
    Dim sFilePath As String, sParentPath As String
    Dim sPaths As Variant, i As Integer
    sPaths = Array("D:\AAA\BBB\CCC\DDD\", "D:\AAA")
    For i = LBound(sPaths) To UBound(sPaths)
        sFilePath = sPaths(i)
        sParentPath = GetParentFolder(sFilePath, 2)
        MsgBox sFilePath & vbCr & vbCr & sParentPath
    Next i
End Sub

Function GetParentFolder(initialPath As String, Optional levelUp As Integer = 1)
    Dim pf As String, i As Integer, j As Integer
    
    On Error GoTo Exit_GetParentFolder
    
    pf = initialPath
    If Right$(pf, 1) = "\" Then pf = Left(pf, Len(pf) - 1)
    i = 0
    Do While i < levelUp
        j = InStrRev(pf, "\") - 1
        If j < 3 Then j = 3
        pf = Left$(pf, j)
        i = i + 1
    Loop

Exit_GetParentFolder:
    GetParentFolder = pf
End Function

Note:

Function returns the drive letter when there's no way to move level up.

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
0

If you're on Windows:

Sub tester()
    Debug.Print MoveUp("C:\Users\tim\Desktop", 2)
End Sub


Function MoveUp(ByVal f As String, Optional levels As Long = 1)
    Dim n As Long
    For n = 1 To levels
        f = CreateObject("scripting.filesystemobject") _
                   .getfolder(f).ParentFolder.Path
    Next n
    MoveUp = f
End Function
Tim Williams
  • 154,628
  • 8
  • 97
  • 125