"Silent Save" (No Dialog Box) PDF File to Hard Drive

I have a situation where I need the user to

  1. Select a PDF file from a source (could be the hard drive, USB flash drive, etc.) and then

  2. Immediately have the program “silently save” (no dialog box) it to a specific location on the hard drive.

Here’s the code I’m using to allow the user to select the PDF file (Part 1 above):

  Dim dlg As OpenDialog
  Dim f As FolderItem
  dlg = New OpenDialog
  dlg.InitialDirectory = SpecialFolder.Documents
  
  dlg.Title = "Select a PDF file"
  dlg.Filter = pdfType.Pdf
  f = dlg.ShowModal
  If f <> Nil Then
    // Proceed
  Else
    // User Cancelled
  End If  

NOTE: I created the File Type “Pdf” in the Menubar

I have read everything I could (Forum and Google) but can’t seem find the way to accomplish #2 above.

Let’s say the PDF file selected by the user is “Example.pdf” and the destination on the hard drive for the silent save is “C:/XProject_TargetSurvey”. How do I do Part 2 above?

Thanks in advance,
… Don

FolderItem.CopyTo?

3 Likes

Thanks, Bill. I’ll give it a whirl in the morning.

BTW … I always forget:
Xojo 2016 r1.1
Windows 10 OS

Alas, the help says that CopyTo was new to 2019r2.1.

It looks like the predecessor was Xojo.IO.FolderItem.CopyTo. From what I read, the method should start with ‘Using Xojo.IO’, and the destination parameter should be a Xojo.IO.Folderitem.

Thanks for the response, Jerry. The usage of “.IO” came after 2016 r1.1.

Well, here’s a 2015 thread I found, so it should be safe :slight_smile:https://forum.xojo.com/t/making-a-copy-of-a-folder/25491/4

Edit: the original poster asks about copying an entire folder, not a file, but this seems good either way.

2 Likes

and BinaryStream?
“New in 2009r3”
open read and create write

1 Like

In Xojo 2015 code, I would use this:

 f = dlg.ShowModal
    If f<> nil and f.exists Then

dim trgt as folderitem
trgt = getfolderitem("C:/XProject_TargetSurvey")
//now, assuming trgt is a FOLDER
        f.CopyFileTo trgt.child(f.name)
//add your own error trapping!
end if
1 Like
Var dlg As OpenDialog
Var f As FolderItem
Var folderDestination As FolderItem
// Assign folderDestination the "specific location" (enclosing folder) that you want things to be saved.
folderDestination = SpecialFolder.Documents // for testing purposes

dlg = New OpenDialog
dlg.InitialDirectory = SpecialFolder.Documents

dlg.Title = "Select a PDF file"
dlg.Filter = pdfType.Pdf
f = dlg.ShowModal
If f <> Nil Then
  // Proceed
  If folderDestination <> Nil And folderDestination.Exists Then
    f.MoveTo(folderDestination) // note that after this point, f will not longer "exist"
    // to restore its existance (if that is important to you)
    f = folderDestination.Child(f.Name)
  Else
    MessageBox("No valid destination enclosing folder")
  End If 
  
Else
  // User cancelled
  MessageBox("User cancelled")
End If

You need to establish a “specific location” which effectively has to be a folder that exists on your system. For testing above, I have made the destination folder the Documents folder.

Once you establish that the “specific location” exists you simply issue the MoveTo method of a FolderItem. One little quirk is that after the move occurs, in my experience, the f FolderItem is now considered not to “exist” although it still has a name. It may not be important to you, but if you need to maintain a reference to the move file, the line

f = folderDestination.Child(f.Name)

will do the job.

In any case, with the code above, I can select a file on my desktop and it silently gets move to the Documents folder after the user selects it (Clicks on Open button in the dialog)

1 Like

Shucks. I did not notice all the answers after the original post.

I missed:

BTW … I always forget:
Xojo 2016 r1.1
Windows 10 OS

MoveTo, like CopyTo, are new in 2019 r2.1

The slight difference in their behavior is as implied in their names.

Perhaps this is an incentive for you to upgrade to more recent versions of the Xojo although I of course do not know your specific circumstances. There is a lot to like in the more modern Xojo.

Thanks again for your response, Jerry. Thanks for your input too, Bill. Looks like you were thinking the same thing here.

Well, I slightly modified what James Redway had in his 2015 post and added it to what I already had as follows:

  Dim dlg As OpenDialog
  Dim f As FolderItem
  dlg = New OpenDialog
  dlg.InitialDirectory = SpecialFolder.Documents
  
  dlg.Title = "Select a PDF file"
  dlg.Filter = pdfType.Pdf
  f = dlg.ShowModal
  
  If f <> Nil Then
    
    // Proceed
    
    dim source as folderitem = f
    
    if source <> nil and source.Exists then
      
      dim destination as FolderItem 
      
      destination=(getfolderitem("").child("Store Drawings"))
      
      source.CopyFileTo(destination)
      
    end if
    
  Else
    
    // User Cancelled
    
  End If
  

The desired goal is to get the PDF file the user grabbed with the file browser and save it to the folder “Store Drawings” (which is a child folder of the application folder). When I run this, nothing happens. No file copy to Store Drawings, no error, etc. If I stick a MsgBox after the copy command and look at the “destination.Name” property, it does indeed show it correctly as “Store Drawings”. So, I’m at a loss for why it doesn’t actually copy the file to Store Drawings. Can you see any overt mistakes I made (obviously there’s one there somewhere :wink:)

I’m going to try some of the other suggestions and see if I have any better luck. Thanks again guys for your time and efforts here to help me.

Thanks for the time you spent here trying to help me, Robert! I do intend on upgrading at some point in 2022. My problem with changing over now is twofold:

  1. I’m in up to my eyeballs in current projects that don’t afford me the luxury of stopping to learn API2

  2. Many (if not most) of my projects are “follow-ons” or “expansions” of a previous project. Therefore I reuse code often. The thought of refactoring all that previous code from API1 to API2 is not a task I relish … particularly when I think of all the errors it may induce with things that previously worked.

I’m hoping that a “unique” project comes my way that doesn’t rely heavily on any past coding I’ve done. That way, I can work in tandem … continue to develop with API1 for projects with tight due dates while I’m creating the “new unique” project from ground up in API2 and learning the API2 ropes.

Thanks for taking the time to respond, Markus. In all the searching I did for an answer to my needs, I found numerous examples of using binary stream but they were all with memory blocks processing text files. Not really sure and could well be wrong (I’ll have to try it to see), but I think PDF files won’t work with that approach. Do you have any examples of using binary stream with a PDF file?

BINGO!!! We have a winner! Works flawlessly, Jeff. Thanks so much to you and all the folks that responded here … once again, a testament to the selfless offering of time and effort demonstrating the power of this community!

You don’t necessarily have to upgrade everything to API2 to use a more recent version of Xojo…