SS12TT

Get filename (pdf) from command line and promt to save to a new location

4 posts in this topic

Hi Everyone,

I'm slightly stuck since I don't know exactly what I'm searching for.  I'm using a scanner that has only two options: save to a folder, or open in an application.  I want to write a small script that would allow me to get the file name from the argument and offer the user the option to resave it somewhere else.

It defaults to saving it in My Documents.  I can verify the the PDF file was saved and the full filename with path argument passed using the following script:

 

#include <MsgBoxConstants.au3>

$file_resave = $CmdLine[1]

MsgBox($MB_SYSTEMMODAL, "Title", $file_resave, 10)

Now I have the full path to file.  Is there any example I can use to figure out how to go about prompting the user to choosing a new location and saving the file again?  Ideally I would like to delete the old file if they resave it to a new location as well.

 

Thanks!!!

 

Share this post


Link to post
Share on other sites



1 minute ago, SS12TT said:

Is there any example I can use to figure out how to go about prompting the user to choosing a new location and saving the file again?

The helpfile: look for function: FileSaveDialog()

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Perfect!  Thanks so much for the hint.  I will post the code once I have it working incase someone else finds it useful.

Share this post


Link to post
Share on other sites

Here's what I just cooked up.  I needed it quickly and the code is very very ugly.  Please don't judge too harshly :(  Or do.  I'm okay with my occasional uselessness :(

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <File.au3>

; Get the file name from the scanner software
Local $originalFileNameandLocation = $CmdLine[1]

; User sets the default starting location
Local $defaultSaveDir = $CmdLine[2]

; Show confirmation of save at the end?
Local $showConfirmations = $CmdLine[3] ;(0,1)

; Variable to find out if a filename was chosen - to exit from while loop
Local $newFileNameChosen = False
Local $userChoseToNotSave = False

; Setup variables to capture component parts of original and new filenames
Local $originalDrive = "", $originalDir = "", $originalFileName = "", $originalExtension = ""
Local $newDrive = "", $newDir = "", $newFileName = "", $newExtension = ""

; Capture component parts of original file name
Local $originalPathSplit = _PathSplit($originalFileNameandLocation, $originalDrive, $originalDir, $originalFileName, $originalExtension)

; Setup file filter based on the extension of file scanned (PDF, JPEG, TIF etc)
Local $fileDialogFilter = StringUpper(StringTrimLeft($originalExtension,1)) & " (*" & StringUpper($originalExtension) & ")" & "|All (*.*)"


; To choose a valid file name
While 1
   ; No need to check for existing file with overwrite protection here as we will check that later.  Change to BITOR($FD_PATHMUSTEXIST, $FD_PROMPTOVERWRITE)
   ; if you need explorer to handle overwriting files.
   $newFileNameandLocation = FileSaveDialog("Save Scanned File", $defaultSaveDir, $fileDialogFilter, $FD_PATHMUSTEXIST, $originalFileName&$originalExtension)

   ; If they hit cancel, leave.
   If @error Then
      $userChoseToNotSave = True
      ExitLoop

   ; They chose a file
   Else
      ; Capture component parts of new file name
      Local $newPathSplit = _PathSplit($newFileNameandLocation, $newDrive, $newDir, $newFileName, $newExtension)

      ; Change the default save directory to the new location.  If we have to reopen the dialog, it will return to the last folder.
      $defaultSaveDir = $newDrive & $newDir

      ; If the user chose a file name with an extension, we're okay.
      If $newExtension Then

         ; If the file exists, ask the user if they want to save over it.
         If(FileExists($newFileNameandLocation)) Then
            $response = MsgBox(4, "File Exists", "Do you want to overwrite the existing file?")
            If $response = 6 Then
               ; This variable is used to move the file later
               $newFileNameChosen = True
               ExitLoop
            Else
               ; If they choose not to overwrite, then reopen the save dialog and ask them to choose again.
               ContinueLoop
            EndIf
         Else
            ; the file chosen does not exist.  We are good to go.
            $newFileNameChosen = True
            ExitLoop
         EndIf

      Else
         ; User chose a filename with no extension.
         ; Add the missing extension to the file name from the old file.
         $newExtension = $originalExtension

         ; Update the new filename
         $newFileNameandLocation = $newFileNameandLocation&$newExtension

         ; Check for existance and do the same thing as before.
         If(FileExists($newFileNameandLocation)) Then
            $response = MsgBox(4, "File Exists", "Do you want to overwrite the existing file?")
            If $response = 6 Then
               $newFileNameChosen = True
               ExitLoop
            Else
               ContinueLoop
            EndIf
         EndIf

      EndIf
   EndIf
WEnd



; User did not choose a file to save to.  Tell them where the original scanned file is and the option to open the file
If $userChoseToNotSave = True Then
   $response = MsgBox(4, "Scanned File", "File was not moved to client folder." & @CRLF & "Original scan can be found at: " & $originalFileNameandLocation & @CRLF & "Open file location?")
   If $response = 6 Then
      ShellExecute($originalDrive & $originalDir)
   EndIf

; User chose a file.  Attempt to move
ElseIf $newFileNameChosen = True Then
   $FileMoveStatus = FileMove($originalFileNameandLocation, $newFileNameandLocation, $FC_OVERWRITE)
   ; Verify if the move was successful.
   If($FileMoveStatus = 1) Then
      ; Show dialog to open new file location
      If ($showConfirmations = 1) then
         $response = MsgBox(4, "Scanned File", "File saved successfully to: " & $newFileNameandLocation & @CRLF & "Open file location?")
         If $response = 6 Then
         ShellExecute($newDrive & $newDir)
         EndIf
      EndIf
   Else
      $response = MsgBox(4, "ERROR", "There was an error moving the file to it's new location." & @CRLF & "Original scan can be found at: " & $originalFileNameandLocation & @CRLF & "Open file location?")
      If $response = 6 Then
         ShellExecute($originalDrive & $originalDir)
      EndIf
   EndIf
EndIf

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now