Splitting & Comparing 2 Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimmelsd33
    New Member
    • Dec 2009
    • 29

    Splitting & Comparing 2 Strings

    I am having a problem trying to compare a file string to see if the save as file string needs to be overwritten. The logic, I believe, is to compare the 'save as string' with the already open 'file string'. Does this make sense? When the Save File dialog comes up, I need to compare the save as filename with the already open filename, and if its the same name, then ask to be overwritten. I'm not sure where to start. I believe the file extension, '.las' for example, will need to be removed, and then a comparison made. Can anyone help? Thanks in advance
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    When user opens a file via your code and the common dialog, the file name is retrieved for you and thus you should save that into a variable. From there if user alters the file and selects save, the file name will be the same and the file contents overwritten with the new information. However, if user selects save as, as you have pointed out, then once again the common dialog returns you a path/file name and all you need to do is to compare the two...
    Code:
    Dim Ans As Integer
    If OrigFileName = SaveAsFileName Then
      Ans = MsgBox("Overwrite file?", vbYesNoCancel,"")
      If Ans = vbYes Then
        'save code here
      ElseIf Ans = vbNo Then
        'reshow saveas dialog here or do a recursive call to whatever function this is
      ElseIf Ans = vbCancel Then
        'do nothing
      End If
    Else
      'Save Code goes here
    End If


    Good Luck

    Comment

    • kimmelsd33
      New Member
      • Dec 2009
      • 29

      #3
      Thanks for the help. Just what I was looking for.

      Comment

      • kimmelsd33
        New Member
        • Dec 2009
        • 29

        #4
        I keep getting a runtime error '70', access denied. What does this mean? Code is as follows:
        Code:
        Dim Ans As Integer
                    'check if originalfile matches saveasfilename
                If tFile = fSave Then
                    Ans = MsgBox(fSave & " already exists. Do you want to overwrite file?", vbquestion+vbYesNoCancel, "Save As")
                        If Ans = vbYes Then
                            'save code here
                            Kill tFile '<<<this is the problem>>>
                            If LCase(Right(fSave, 4)) = ".las" Then
        '                       'copy temp file
                                FileCopy "C:\Temp.dat", fSave
                            Else
                                FileCopy "C:\Temp.dat", fSave & ".las"
                            End If
                        Else If Ans = vbNo Then
                            'reshow saveas dialog here 
                            With CommonDialog1
                                .Filter = "All Files|*.las*|"
                                .InitDir = fSave
                                .DialogTitle = "Save File As"
                                .ShowSave
                            End With
                            fSave = CommonDialog1.FileName
                        Else If Ans = vbCancel Then
                        'do nothing
                        End If
                Else
                  'Save Code goes here
                  FileCopy "C:\Temp.dat", fSave & ".las"
                End If
        I'm not really sure how to approach this. The savefile dialog pops up to save the file. If the user selects the same filename, it should overwrite the file, but it doesn't. If the user enters a different name, it saves properly. Any suggestions? Thanks again

        Comment

        • vb5prgrmr
          Recognized Expert Contributor
          • Oct 2009
          • 305

          #5
          Okay, when you first open the file, how do you do it? Open Statement?...
          Code:
          Open SomeFilePathName For Input As #1
          Do you close your connection to the opened file?
          Code:
          Close #1
          If not, there is your error as you still have the file open and therefor cannot delete it.



          Good Luck

          Comment

          • kimmelsd33
            New Member
            • Dec 2009
            • 29

            #6
            I am using the code below to reference the file:
            Code:
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set objstream = fso.opentextfile(tFile, 1, False, 0)
            tFile is referencing a file such as "C:\Temp\Temp.d at"
            Is there any specific way to close the file, if using filesystemobjec t? I've tried just "Close" so it would close anything opened, but it didn't work. Thanks again.

            Comment

            • kimmelsd33
              New Member
              • Dec 2009
              • 29

              #7
              I had resolved the issue. I just needed to close the fso.objstream:
              Code:
              fso = objstream.Close
              Thanks for all your help. My software is now 100% completed, thanks to you.

              Comment

              • vb5prgrmr
                Recognized Expert Contributor
                • Oct 2009
                • 305

                #8
                Not a problem, glad you got it solved...

                Comment

                Working...