Library command to move a binary file from one Dir to anothe

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
alf50
Posts: 129
Joined: Sun Jun 13, 2010 2:55 pm

Library command to move a binary file from one Dir to anothe

Post by alf50 »

Is there an UNO or Tool Library Command to move a binary file from one Directory to another? All the "SIMPLE" solutions I have found will transfer a Text file, but not a binary file from one folder to another.
OpenOffice 4.1.14 on Mac Catalina(10.15.7), RasPi4B (TwisterOS-8/2023update) & MS Wnds10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Library command to move a binary file from one Dir to an

Post by Villeroy »

Every programming language can do this. Even StarBasic can.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

Filecopy

Then Kill to delete the old file

https://wiki.openoffice.org/wiki/Docume ... e_Library)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Library command to move a binary file from one Dir to an

Post by karolus »

Hallo

Any command_promt on every Linux-OS:

Code: Select all

> mv /some/file/path  /some/other/file/path
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Library command to move a binary file from one Dir to an

Post by Sébastien C »

If I may...

@JeJe
Copy and delete the old file is a bad way if the file is big.

@karolus
alf50 specifies that he is under M$W10...

The right method in StarBasic for move a file is to use the instruction "Name"
An example :

Code: Select all

Sub exampleMoveFile()
 On Error GoTo myError
 Name "C:\myFile.txt" As "C:\temp\myFile.txt"
 msgbox "Process OK."
 Exit Sub

 myError:
 Select Case err
  Case 53 : msgbox "The file do not exist."
  Case 58 : msgbox "The file already exists."
 End Select
End Sub
See also: https://help.libreoffice.org/3.5/Basic/ ... _Functions
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
alf50
Posts: 129
Joined: Sun Jun 13, 2010 2:55 pm

Re: Library command to move a binary file from one Dir to an

Post by alf50 »

As I said in my original post, using the name command works fine if the file is xx.txt, fails for anything else, i.e.. "short cut, alias, .xls, .docx, etc"
OpenOffice 4.1.14 on Mac Catalina(10.15.7), RasPi4B (TwisterOS-8/2023update) & MS Wnds10
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Library command to move a binary file from one Dir to an

Post by Sébastien C »

This works very well by me with any file, binary or not. Now it may be time to learn how your operating system REALLY notes file names in its file system ... For example, a shortcut named "myfile" is, under M$-W-XP called "myfile.lnk". I suggest you to go into the directory that interests you with a Windows command line and type "dir" ("ls" on Mac) ... You may be a little surprised ...
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Library command to move a binary file from one Dir to an

Post by musikai »

To get the full path of the file you can also shift+right click the file in Windows explorer and "copy as path".
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

"@JeJe
Copy and delete the old file is a bad way if the file is big."

Interesting. Assuming you have enough space... why would that be? Nothing in the documentation you linked to?

There are two other options.

- write your own procedure using the get and put statements that loads the file into a byte array (in chunks if big) and then save it.

- use the windows api functions which you can declare at the top of an OOBasic module. Try this one.

Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" ( _
ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal dwFlags As Long _
) As Long

https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

That address didn't paste as a link... if you google MoveFileEx, msdn is the first result.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

SHFileOperation is another Windows API method. Have a look at this discussion:

http://www.vbforums.com/showthread.php? ... code-files
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Library command to move a binary file from one Dir to an

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Library command to move a binary file from one Dir to an

Post by Sébastien C »

The correct page you quote first is here. The second one is for Microsoft VisualBasic ; not StarBasic. The syntax is SO often different.

But Good Lord why do you make it so complicated when StarBasic make the job so simple ???

I repeat that the instruction

Code: Select all

Name "C:\myFile.bin" As "C:\temp\myFile.bin"
work perfectly, on Windows and, at least, on Linux (I have tried too). The instruction "Name" can be received url notation for compatibility between OS, for example withe the function "ConvertToURL". But the problem is NOT a fact of binary or text format. It is seem that the problem is just real files's names.
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

I was just giving options. Something as simple as an MSDN function declare and calling it will probably copy fine from VB6 and work in OOBasic.

VB6 was the world's most popular programming language for a long time - so there is a huge body of code out there much of which will work in OO - some with a little tweaking, some without. Something to bear in mind - people are far far more likely to find an algorithm for something if they search for it in VB6 than if they hope to find an OOBasic version.

Villeroy's xsimplefileaccess, your name thing if it works, Filecopy if its just efficiency that means its bad - spoiled for choice here.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Library command to move a binary file from one Dir to an

Post by Sébastien C »

So following the example of Villeroy since not everyone knows how to use a service...

Code: Select all

Sub exampleMoveFile1()
 Dim mySFA As Object
 Dim mySource As String, myDestination As String

 mySource = convertToURL("C:\myFile.bin")
 myDestination = convertToURL("C:\temp\myFile.bin")
 mySFA = createUNOService ("com.sun.star.ucb.SimpleFileAccess")

 mySFA.move(mySource, myDestination)
End Sub
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Library command to move a binary file from one Dir to an

Post by karolus »

Hallo
JeJe wrote:VB6 was the world's most popular programming language for a long time
May be or not … but anyway that was in the Stone Age in Terms of IT
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

The point is there is a huge codebase in VB6. If you want some code that's a good place to look. If you want to use a Windows api and find the declare for it - you'll almost certainly find it on a VB6 site and most probably not find it on an OO one.

Going by the tiny number of people here - there're still a lot more people coding in VB6 than OOBasic.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Library command to move a binary file from one Dir to an

Post by Villeroy »

This is not a Windows application. Basic "Declare Function" is the only Windows specific feature I know. It breaks platform independency.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

Declaring Api functions is just an example of what you can get from all the VB6 source code out there. A lot of the code will work cross platform... dunno why people are so resistant to me telling them about it.

If you're prepared to have Windows only code then using the Windows Api you can do an awful lot of things that you can't do with OOBasic. It greatly extends what you can do on the Windows platform. You lose platform independence - but you gain the ability to do things you can't otherwise do.

Automation is Windows specific feature. There could be others - I don't know OO very well but OpenOffice when it does its xSimpleFileAccess on Windows will call the Windows Api.. Movefile or MoveFileEx.. the same functions I'm telling you you can Declare and call directly. They don't reinvent the wheel.

https://www.openoffice.org/udk/common/m ... ation.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

If you look at XSimpleFileAccess What are the options for moving a file? None at all.

Now look at SHFileOperation and see that you use different flags for that function, connect or not connect html files and folders and so on. Much more for you - if that's what you happened to want to do.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

Also if you compare the functions - XSimpleFileAccess raises a CommandAbortedException.
While below is the list of error codes for SHFileOperation. A robust function written use the Windows Api, that handles all those errors appropriately - would appear to be the best solution for moving a file on Windows. I'm not advocating it as a solution for this thread - there'd be a big cost in terms of writing it - though it might be available already somewhere in VB6 ha.

If handling the errors isn't critical there's no point. But it illustrates why using the API might be worthwhile to someone.



DE_SAMEFILE 0x71 The source and destination files are the same file.
DE_MANYSRC1DEST 0x72 Multiple file paths were specified in the source buffer, but only one destination file path.
DE_DIFFDIR 0x73 Rename operation was specified but the destination path is a different directory. Use the move operation instead.
DE_ROOTDIR 0x74 The source is a root directory, which cannot be moved or renamed.
DE_OPCANCELLED 0x75 The operation was canceled by the user, or silently canceled if the appropriate flags were supplied to SHFileOperation.
DE_DESTSUBTREE 0x76 The destination is a subtree of the source.
DE_ACCESSDENIEDSRC 0x78 Security settings denied access to the source.
DE_PATHTOODEEP 0x79 The source or destination path exceeded or would exceed MAX_PATH.
DE_MANYDEST 0x7A The operation involved multiple destination paths, which can fail in the case of a move operation.
DE_INVALIDFILES 0x7C The path in the source or destination or both was invalid.
DE_DESTSAMETREE 0x7D The source and destination have the same parent folder.
DE_FLDDESTISFILE 0x7E The destination path is an existing file.
DE_FILEDESTISFLD 0x80 The destination path is an existing folder.
DE_FILENAMETOOLONG 0x81 The name of the file exceeds MAX_PATH.
DE_DEST_IS_CDROM 0x82 The destination is a read-only CD-ROM, possibly unformatted.
DE_DEST_IS_DVD 0x83 The destination is a read-only DVD, possibly unformatted.
DE_DEST_IS_CDRECORD 0x84 The destination is a writable CD-ROM, possibly unformatted.
DE_FILE_TOO_LARGE 0x85 The file involved in the operation is too large for the destination media or file system.
DE_SRC_IS_CDROM 0x86 The source is a read-only CD-ROM, possibly unformatted.
DE_SRC_IS_DVD 0x87 The source is a read-only DVD, possibly unformatted.
DE_SRC_IS_CDRECORD 0x88 The source is a writable CD-ROM, possibly unformatted.
DE_ERROR_MAX 0xB7 MAX_PATH was exceeded during the operation.
0x402 An unknown error occurred. This is typically due to an invalid path in the source or destination. This error does not occur on Windows Vista and later.
ERRORONDEST 0x10000 An unspecified error occurred on the destination.
DE_ROOTDIR | ERRORONDEST 0x10074 Destination is a root directory and cannot be renamed.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Library command to move a binary file from one Dir to an

Post by Villeroy »

The original poster runs WIndows and Mac.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library command to move a binary file from one Dir to an

Post by JeJe »

So my first suggestion would have worked then. It wouldn't be very efficient as someone said... unless the paths were on different drives in which case it would be fine... but its about the simplest and the one given in the documentation.

Then I posted another couple of options that just occurred to me... why would I hold back.... they're options...
by far the worst of the two was write your own function using put and get... I don't know why I wasn't leapt upon for that one... I really don't...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply