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.
Hello,
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)
aPaths=Split(sCurFileSys,"\")
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()
GlobalScope.BasicLibraries.loadLibrary("Tools")
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.
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”
I like this …
Dear @anon73440385,
In Code Snippet 1
:
I have edited :
dim sCurFolderSys As String
And added:
dim i%
Why is the result of sCurFolderSys
""
?
[ Fedora 31 Worksatation
+ Cinnamon Desktop Spin
+ LibreOffice 6.2.8.2-2
]
@lonk - please tell me the name of the path or could it be, that you you run the macro before you have saved the file and thus there is no path yet?
If the program starts from a link (symlink), the GetURL returns a path to that link, not to the document’s location. (I mean Linux/Ubuntu symlink). In addition, the file name has no extension.
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."
Else
MsgBox FolderName
End If
End Sub