Are you sure this is a LibreOffice question? LO is an office suite (document, spreadsheet, drawing, presentation and more).
Your question looks rather like an OS scripting question. In addition, you didin’t mention your OS name. I guess it is Windows because of the filename syntax but which one?
You should ask on a site dedicated to W$.
I want a Macro Code For Rename File or Remove the First Portion Words …
Is there such a thing? I only can see one-line-essays.
If similar semantics is coded as "User Parameters Part 1-..."
for one item, but as "User Parameter Part 2-..."
(Without the plural-s!) for the next one I would assume this even exceeds MS’s bungling capabilities or those of a youtube downloader app.
Hi Lupp… I want to get All FileName of MP4 list out in LibreOffice Calc Sheet and Rename through Macro ?
Citing OP:
E: and reverse solidus is typical of Window$.
- How do you know iot’s
MP4
? - @elmau gave you links to help texts (explanations) concerning the file related commands you need to use in LibreOffice Basic.
- To get the new name based on the old name you can use the Basic statement
newName = Mid(oldName, Len("SaveTube.App-") + 1, 65535)
if the left part is assured to be unchanged.
I don’t think you can get a Basic tutorial or readymade code for any arbitrary task here.
Yes I missed you were talking of the path.
(I also was in the mood to joke a bit.)
You’re welcome.
Youe need use function dir(1) for get names in folder, and function name(2) for rename.
from pathlib import Path
def main():
PATH_VIDEOS = '/home/elmau/Projects/test/files'
for p in Path(PATH_VIDEOS).glob('*.mp4'):
p.rename(Path(p.parent, f'{p.name[13:]}'))
return
Macro Code Please …
Since this is a matter of renaming files, it is better done outside LO. There are even utilities to mass rename files available in any OS; some of them are provided by the OS.
yes, but for that we also have support of several languages in macros, if you want to do it from LO
→→
p.rename( p.with_name( f'{p.name[13:]}'))
the whole thing with OP folderpath:
from pathlib import Path
video_folder = Path("e:/","Learning Videos",
"DataBase","LibreOffice - Base",
"TheFrugalComputerGuy","New folder")
def rename(*_):
for p in video_folder.glob("*.mp4"):
p.rename(p.with_name(f"{p.name[13:]}"))
How to Write it in LibreOffice BASIC Macro ?
of course on linux Commandline:
cd "E:\Learning Videos\DataBase\LibreOffice - Base\TheFrugalComputerGuy\New folder\"
rename 's/^.{12}(.*\.mp4)/$1/' *
but you on $windows, so you may google for »rename files windows-commandline«
Hi karolus,
Thanks… Finally I Write the Code in LibreOffice - BASIC Macro… i got Some reference from Internet Including " ask.LibreOffice.org " … and Combine them into Macro…
1), First ListOut or get -All File Names in Calc Sheet
In Calc Sheet Column F3 → has Folder Path → E:\New folder
Column A2 → have All The File Names , that is Source FileNames are ListedOut through Macro
If already FileNames are there then, Macro Clears The All File Names in Column A2 To LastRow Of Column A and then Macro getting All the FileNames in the E:\New folder again in the Calc Sheets.
In Column B , Row 2 ( B2 ) to End Of Column B -have the New File Names are Listed Out …
Here is the Screen Shots
Here is the BASIC Macro Codes…
Sub Rename_AllFiles
Dim oSheet As Variant, oCursor As Variant, nEndRow As Long, i As Long
Dim oCellRangeByPosition As Variant, oDataArray As Variant
Dim sPath As String, sSourceName As String, sTargetName As String
oSheet = ThisComponent.getCurrentController().getActiveSheet()
sPath = oSheet.getCellRangeByName("F3").getString()
If Right(sPath, 1) <> GetPathSeparator() Then sPath = sPath + GetPathSeparator()
Call GetFileNames_In_CalcSheets ' get FillNames or List Of FileNames in Calc Sheet
oCursor = oSheet.createCursor()
oCursor.gotoEndOfUsedArea(True)
nEndRow = oCursor.getRangeAddress().EndRow
oCellRangeByPosition = oSheet.getCellRangeByPosition(0, 1, 1, nEndRow)
oDataArray = oCellRangeByPosition.getDataArray()
Dim oFromFile
Dim oToFile
Dim oFromURL
Dim oToURL
For i = 0 To UBound(oDataArray)
sSourceName = oDataArray(i)(0)
sTargetName = oDataArray(i)(1)
oFromFile = sPath + sSourceName
oToFile = sPath + sTargetName
oFromURL = ConvertToUrl(oFromFile)
oToURL = ConvertToUrl(oToFile)
If FileExists(oFromURL) Then
If NOT FileExists(oToURL) then
Name oFromURL As oToURL
Else
MsgBox(oToFile & "Already Exists", 0, "Caution !!")
Exit Sub
End If
Else
MsgBox( oFromFile & " Does Not Exists ", 0, "Caution !!")
Exit Sub
End If
'If Not FileExists(sPath + sSourceName) Then
'MsgBox("File " + sSourceName + " not found",0,"Skipped")
'ElseIf FileExists(sPath + sTargetName) Then
'MsgBox("File " + sSourceName + " already exists",0,"Skipped")
'Else
'Name sPath + sSourceName As sPath + sTargetName
'EndIf
Next i
End Sub
Sub GetFileNames_In_CalcSheets
Dim oSheet As Object, i As Long, iCounter As Long, stFileName As String, stPath As String
Doc = ThisComponent
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(5,2) ' F3 --> File Fath--> E:\New folder
oSheet=ThisComponent.CurrentController.ActiveSheet
Print Cell.String ' E :\New folder
Print getPathSeparator() ' \
stPath = Cell.string & GetPathSeparator() ' E:\New folder\
stFileName = Dir(stPath, 0) ' Accessing 0 For File : 16 For Folder
'This "For loop"" Clears Column A,Row 2 through 15'
For i = 1 to 15
'Change here if Column,Row of files is different - i is Row'
oSheet.getCellByPosition(0,i).String = ""
Next i
i = 0
' This Do While Loop will help getFille Names from Fol
Do While (stFileName <> "")
iCounter = iCounter + 1
'File Names will start in Column 'A' (Signified by 0) Row 2 (iCounter)'
oSheet.getCellByPosition(0,iCounter).String = stFileName
stFileName = Dir()
Loop
End Sub
Here is the Screen Shots Of Calc Sheet and Column A , Colum B and Column F
Here is the Screen Shot Of E:\New folder\ ( All the Video Files ) Before (Re-Naming or RunningMacro)
Here, is the Screen Shot Of - After Running Macro… Now, All The Files are Renamed
Here is the Calc Sheet For learning Purpose …
Re-Naming Files.ods (32.3 KB)
For Learning Purpose …Here is the ListOut or Getting All the FileNames in Calc Sheet Macro Codes…
Sub list_files_Method_1() ’ Do While Loop
Dim i, strFile
path ="E:\New folder\"
strFile = Dir(path,0)
i = 1
Do While strFile <> ""
my_cell = ThisComponent.Sheets(0).getCellbyPosition(1,i)
my_cell.String = strFile
strFile = Dir ' returns next entry
i = i + 1
Loop
End Sub
'===========================================================================
Sub list_files_Method_2() ’ Do Until Loop
Dim i, strFile
path ="E:\New folder\"
strFile = Dir(path,0)
i = 1
Do Until strFile = ""
my_cell = ThisComponent.Sheets(0).getCellbyPosition(1,i)
my_cell.String = strFile
strFile = Dir ' returns next entry
i = i + 1
Loop
End Sub
'===========================================================================
Sub list_files_Method_3() ’ While Wend …
Dim i, strFile
path ="E:\New folder\"
strFile = Dir(path,0)
i = 1
While strFile <> ""
my_cell = ThisComponent.Sheets(0).getCellbyPosition(1,i)
my_cell.String = strFile
strFile = Dir ' returns next entry
i = i + 1
Wend
End Sub
'===========================================================================
Macro Code For Re-Name Files
Sub oFileRename
Dim oFromFile : Dim oToFile : Dim oFromURL : Dim oToURL
oFromFile = "E:\New folder\SaveTube.App-LibreOffice Base (26) Limit Distinct Calculations and Constants-(1080p).mp4"
oToFile = "E:\New folder\LibreOffice Base (26) Limit Distinct Calculations and Constants-(1080p).mp4"
oFromURL = ConvertToUrl(oFromFile)
oToURL = ConvertToUrl(oToFile)
If FileExists(oFromURL) then
If NOT FileExists(oToURL) then
Name oFromURL As oToURL
else
MsgBox(oToFile & "Already Exists", 0, "Caution !!")
Exit Sub
End If
else
MsgBox( oFromFile & " Does Not Exists ", 0, "Caution !!")
Exit Sub
End If
End Sub
That’s fine … and you only needed about 70 lines of BASIC, although it could just as well be done in 5 lines of python.
Nevertheless, I suspect that you didn’t write a single line of it yourself, but copied and pasted everything from someone else? may be @sokol92 ?
Hi Karolus,
No , Some I Write it my Own…Remember I am not a Computer Science or Engineering or Technologies Background … I am a Arts that is Commerce Background … But, I adapted both …
For Python…It require need to Install another IDE … But, For BASIC No Need … LibreOffice It self.
No, as python is already available in LibreOffice you could paste this from any text-editor and I guess APSO is now included by default.