Ask Your Question

How to check folder's name by macro

asked 2019-09-09 12:34:16 +0100

Nina34 gravatar image

Hi all, I need to check the name of folder, if the name of folder is not "NOW", I need to create the new folder which has "NOW" Name. How am I able to do this? ps. I try to use DIR but this code just check there are file or not but cannot check name, or I misunderstand? Thank you everyone for help.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2019-09-09 13:32:39 +0100

Opaque gravatar image

updated 2019-09-09 16:43:09 +0100


if you want the folder of the current document, use ThisComponent.getURL() and ConvertFromURL

Code Snippet 1:

Sub CurrentFolder1()

 dim sCurFileURL As String
 dim sCurFileSys As String
 dim sFolderSys As String
 dim aPaths as Variant

 sCurFileURL = ThisComponent.getURL()
 sCurFileSys = ConvertFromURL(sCurFileURL)

 For i = Lbound(aPaths) to Ubound(aPaths)-1
   sCurFolderSys = sCurFolderSys & aPaths(i)  & "\"
 Next i

 MsgBox "Current Pathname: " & sCurFolderSys
End Sub

Code Snippet 2: (shorter solution using "Tools" Library )

Sub CurrentFolder2()

 dim sPath as String

 sPath = DirectoryNameoutofPath(ThisComponent.getURL(),"/")
 sPath = ConvertFromURL(sPath)
 MsgBox sPath

End Sub

Note: There is also a function CurDir, but I couldn't figure out, how to make a meaningful use of it, since on Windows I got the path to the installation of LibreOffice and on Linux I got my $HOME, regardless of the path to the current open document.

edit flag offensive delete link more


Thank you for your help, and sorry for not clear about answer. I need to check folder's name in folder A not folder's name that I current use. Ex. There are "AAAAA", "BBBBB", "CCCCC" folders in "Main" folder, and I need to check that there are "BBBBB" folder in "MAIN" or not. The file ("TEST") that I use have the same path of "Main" folder.

ps. I use "NOW" word because I want to create folder's name like this "20190910"

Nina34 gravatar imageNina34 ( 2019-09-10 05:03:47 +0100 )edit

I like this ...

Opaque gravatar imageOpaque ( 2019-09-10 07:56:42 +0100 )edit

answered 2019-09-10 05:23:47 +0100

Nina34 gravatar image

updated 2019-09-10 05:30:40 +0100

I use this code and it's work

Sub Check_folder_name()

Dim FolderName As Stringenter

    FolderName = Dir("Pathname\folder name")

       If FolderName = vbNullString Then
             MsgBox "Folder does not exist."
             MsgBox FolderName
       End If

End Sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-09-09 12:34:16 +0100

Seen: 61 times

Last updated: Sep 10 '19