Separating file and type in a string that contains filename might work as below:
sFile="FILE.TYPE"
j=Len(sFile)
While ((Mid(sFile,j,1)<>".") And (j>0))=-1
j=j-1
Wend
sFile2=Left(sFile,j-1)
If there is more convenient way to do this, what would that be?
Using InStr() function could work also, but if file has two dots, like:
sFile="FI.LE.TYPE" it could return the position of the "wrong dot", the 1st one and result into wrong separation of file and type.
Is there a way to scan the string from right to left "InStr()" way?
[Solved] Separating file name & file type of a file by macro
[Solved] Separating file name & file type of a file by macro
Last edited by Hagar Delest on Sun Sep 02, 2018 11:52 am, edited 1 time in total.
Reason: tagged solved
Reason: tagged solved
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
LibreOffice 4.0.4.2
Re: Separating file name and file type of a file by macro
There is a library named Tools. It contains functions for handle URL-s and filenames/extensions.
Here are some routines of this library:
ConvertToUrl
ConvertFromUrl
FileNameoutofPath
GetFileNameExtension
GetFileNameWithoutExtension
DirectoryNameoutofPath
...
You need load this library before you use it:
Here are some routines of this library:
ConvertToUrl
ConvertFromUrl
FileNameoutofPath
GetFileNameExtension
GetFileNameWithoutExtension
DirectoryNameoutofPath
...
You need load this library before you use it:
Code: Select all
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Separating file name and file type of a file by macro
Just for completeness:
There is also a couple of an UnoStruct and an UnoService for the purpose. The 'Tools' library is not needed then.
The structure keeps assigned values and results as constants.
The service provides 4 methods applicable to the structure, basically for parsing and assembling.
A simple demo:
(Editing:)
Just switched on my brain. Sorry! Once again I didn't read the question thorougly enoungh and posted an answer before relevant content of the question percolated to consciousness.
As some visitor might nonetheless be interested in my hint I didn't delete the post. Returning to the OQ also:
There is also a couple of an UnoStruct and an UnoService for the purpose. The 'Tools' library is not needed then.
The structure keeps assigned values and results as constants.
The service provides 4 methods applicable to the structure, basically for parsing and assembling.
A simple demo:
Code: Select all
Sub demoUrlTransformerShort()
urlTrans = CreateUnoService("com.sun.star.util.URLTransformer")
structUrl = createUnoStruct("com.sun.star.util.URL")
MsgBox("You may now inspect the ""structUrl"" strucure.")
structUrl.Complete = ConvertToUrl(ThisComponent.URL)
urlTrans.ParseStrict(structUrl)
structUrl.Complete = ""
structUrl.Main = ""
urlTrans.Assemble(structUrl)
End Sub
Sub demoUrlTransformerLong()
srvManager = GetProcessServiceManager()
urlTrans = srvManager.CreateInstance("com.sun.star.util.URLTransformer")
structUrl = CreateUnoStruct("com.sun.star.util.URL")
'To continue as in the short version.
End Sub
Just switched on my brain. Sorry! Once again I didn't read the question thorougly enoungh and posted an answer before relevant content of the question percolated to consciousness.
As some visitor might nonetheless be interested in my hint I didn't delete the post. Returning to the OQ also:
Code: Select all
fName = structUrl.Name
fFirstName = fName
fExt = ""
If InStr(fName, ".")>0 Then
helper = Split(fName, ".")
fExt = helper(Ubound(helper))
fFirstName = Left(fName, Len(fName)-Len(fExt)-1)
End If
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München
-
- Posts: 103
- Joined: Mon Sep 15, 2014 7:34 pm
Re: Separating file name and file type of a file by macro
I can't understand why people do not give complete correct working answers, instead they show where the fault might be. Maybe since this is a very basic issue.
But beginners do not have much ressources to see how OOO/LO basic works.
I was looking for a way to use a file's name to save another modified one into another folder.
But no, these answers do not help.
Here is a code that I've found in spanish forum. http://ooo-forums.apache.org/es/forum/v ... 897#p36033
Maybe someone might need this.
I made this code below modifying the one I've found. It pastes file name without an extension on the cell N1.
But beginners do not have much ressources to see how OOO/LO basic works.
I was looking for a way to use a file's name to save another modified one into another folder.
But no, these answers do not help.
Here is a code that I've found in spanish forum. http://ooo-forums.apache.org/es/forum/v ... 897#p36033
Maybe someone might need this.
I made this code below modifying the one I've found. It pastes file name without an extension on the cell N1.
Code: Select all
Sub RutaYNombre()
Dim oSheet As Object
Dim oCell As Object
dim oDoc as object
oDoc = ThisComponent
oSheet = oDoc.Sheets(0)
GlobalScope.BasicLibraries.loadLIbrary( "Tools" )
If ThisComponent.hasLocation() Then
oCell = Sheet.getCellByPosition(13, 0) 'Beginning point is 0,0, that's why N1 is (13,0) minus 1 for column and row
oCell.String = GetFileNameWithoutExtension( FilenameOutOfPath( ConvertFromURL( oDoc.URL ) ) )
msgbox ocell.string 'you can delete this line, it does not affect the way code works
Else
msgbox "this document is not saved that's why it has not a root directory, so it has not an URL"
End If
End Sub
Win10-OpenOffice 4.1/LibreOffice 7.4
Re: Separating file name and file type of a file by macro
- On the one hand, because it would be impudent to assume that the OP knows nothing.I can't understand why people do not give complete correct working answers,
The question was: "Separating file name and file type of a file by macro"
I supposed that the poster can get the filename string by a macro, and he/she need the name of the relevant API function only.
these answers do not help.
Here is a code that I've found in spanish forum.
- On the other hand because there are many-many example code snippets in the forums (in this Forum too), in Andrew Pitonyak's free macro books, in the StarBasic/API descriptions. The poster can find them. There are name of the the relevant functions in my answer:
ConvertToUrl
ConvertFromUrl
FileNameoutofPath
GetFileNameExtension
GetFileNameWithoutExtension
DirectoryNameoutofPath
...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Separating file name and file type of a file by macro
User "sokolowitzky" (and many others) might consider to read the introduction to this note: https://arachnoid.com/freeware/index.html .
There is also https://ask.libreoffice.org/en/question ... this-site/ .
And:
Anybody thinking he (f/m) would need "macros" should
-1- try to find a way to solve his task without macros,
-2- and, if -1- failed, be aware of the fact that he has to study macro programming from the beginning,
-3- where the API of OpenOffice is the actually relevant topic as compared with the rather simple Basic itself.
Concerning -2- amnd -3- there are guides by the (different over time) developing companies/teams. A good starting point also is the famous book "OpenOffice.org Macros Explained" by Andrew Pitonyak available online (e.g.) from http://www.pitonyak.org/oo.php. There are additional remarks and examples collected by the same author in the "OpenOffice.org Macro document". See the table "My Main Downloads" in the linked page. (.odt and .pdf available.)
Andrew is another person preferring useful contributions over whining about others doing things the wrong way.
There is also https://ask.libreoffice.org/en/question ... this-site/ .
And:
Anybody thinking he (f/m) would need "macros" should
-1- try to find a way to solve his task without macros,
-2- and, if -1- failed, be aware of the fact that he has to study macro programming from the beginning,
-3- where the API of OpenOffice is the actually relevant topic as compared with the rather simple Basic itself.
Concerning -2- amnd -3- there are guides by the (different over time) developing companies/teams. A good starting point also is the famous book "OpenOffice.org Macros Explained" by Andrew Pitonyak available online (e.g.) from http://www.pitonyak.org/oo.php. There are additional remarks and examples collected by the same author in the "OpenOffice.org Macro document". See the table "My Main Downloads" in the linked page. (.odt and .pdf available.)
Andrew is another person preferring useful contributions over whining about others doing things the wrong way.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München
Re: Separating file name and file type of a file by macro
The original question did not mention document's URL. It correctly worried about the usage of Instr() failing if the name itself contained a point. One rather minimal answer to the original question would be:
This assumes that extendedFileName actually is an extended filename in the sense of having appended an extension. A complete PathName or an URL would also be accepted and treated in the expected (by me) way. No checks for the syntax!
Trying to reliably answer the question in the supposed extended form (concerning URL and the like) would surely require some hints in addition to just posting a solution.
BTW: The original questioner had made clear that he basically understood his problem.
Code: Select all
parts = Split(extendedFileName, ".")
extension = parts(Ubound(parts))
fileNameWithoutExtension = Left(extendedFileName, Len(extendedFileName) - Len(extension)-1)
Trying to reliably answer the question in the supposed extended form (concerning URL and the like) would surely require some hints in addition to just posting a solution.
BTW: The original questioner had made clear that he basically understood his problem.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München