Rename all files in a folder

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ozirisra
Posts: 1
Joined: Mon Mar 11, 2013 11:18 am

Rename all files in a folder

Post by ozirisra »

 Edit: Split from [Writer] Save with first line as file name 
Hi,

This works really great and helped A LOT !!! I'm mainly dealing with VBA, but have to do it with OO this time, so I'm quite a beginner in this.
I'm trying to modify the code to rename ALL files from a certain folder. My problem is that I cannot assign the NextFile that I get to work with the ThisComponent statement.
So, how can I make the NextFile to be the active file and work with ITS first line?

Here's the code that I have so far:

Code: Select all

Sub FirstLineFileName_Writer
Dim NextFile As String
NextFile = Dir("C:\...\", 0)

     While NextFile  <> ""
        oDoc = ThisComponent
        oVC = oDoc.CurrentController.getViewCursor
        Mark = oDoc.Text.CreateTextCursorByRange(oVC)
        oTC = oDoc.Text.CreateTextCursor 
        While oTC.isEndOfParagraph 
        oTC.gotoNextParagraph(false)
        Wend
        oVC.gotoRange(oTC,false) 
        oVC.gotoEndOfLine(true)  
        filename = oVC.String
        url = ConvertToURL("C:\" & filename & ".odt") 
        oDoc.StoreAsURL(url,Array())
        oVC.gotoRange(Mark,false)
        oDoc.Modified = false
         
NextFile = Dir
Wend

   End Sub
Thanks for any help
LibreOffice 3.6.4.3 on windows 7
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Writer] Save with first line as file name.

Post by FJCC »

The problem is that you do not open the document whose name is returned by the Dir() function. Here is some code that opens a document and then closes it. I think you can use it to modify the appropriate parts of your code.

Code: Select all

DirString = "C:\users\public\databases\"
NextFile = DirString & Dir(DirString,0)
NextURL = convertToURL(NextFile)
oDoc = StarDesktop.loadComponentFromURL(NextURL, "_blank", 0, Array())
Print oDoc.URL
oDoc.Close(False)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply