Results 1 to 2 of 2

Thread: How to increment if file exists?

  1. #1

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Exclamation How to increment if file exists?

    Hello!! Hoping to get some more help here..
    The code below can save a profile.

    If the same filename exists, and the user chooses not to overwrite the file, I want to add -nn (-01, then if still the same filename will be -02, and so on and so forth) to the end of the file before the extension .plp.

    example output:
    filename.plp
    filename-01.plp
    filename-02.plp
    filename-03.plp ~ filename-99.plp

    This is my current code:

    VB Code:
    1. Function isFile(ByVal sFileName As String) As Integer
    2.   On Error Resume Next
    3.   Dim lFileLength As Long
    4.   Const ATTR_NORMAL = 0
    5.  
    6.   isFile = False
    7.   If Dir$(sFileName, ATTR_NORMAL) <> "" Then
    8.   lFileLength = FileLen(sFileName)
    9.   If (lFileLength > 0) Then isFile = True
    10.   End If
    11. End Function

    VB Code:
    1. Private Sub cmd_ok_Click()
    2. Dim prof As String
    3. Select Case Me.Caption
    4.     Case "Save Profile"
    5.     txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
    6.         If isFile(txt_profilepath.Text) Then
    7.             txt_plprofile.Text = frm_loadfile.var1
    8.             prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
    9.                 If MsgBox("File " & txt_profilepath & " exists.  Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
    10.                     SaveProfile txt_profilepath.Text, pl1
    11.                 Else
    12.                     txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & "-02" & ".plp"
    13.                     SaveProfile txt_profilepath.Text, pl1
    14.                 End If
    15.         Else
    16.             MsgBox "Saving " & txt_profilepath & " as new profile."
    17.             SaveProfile txt_profilepath.Text, pl1
    18.         End If
    19.  
    20.         profilepath = txt_profilepath.Text
    21.         frm_field.cmd_next.Enabled = True
    22.  
    23.     Case "Load Profile"
    24.         txt_profilepath.Text = file1.Path & "\" & file1.FileName
    25.         MsgBox "Loading " & txt_profilepath & " profile."
    26.             LoadProfile txt_profilepath.Text, pl1
    27.         profilepath = txt_profilepath.Text
    28.         frm_field.cmd_next.Enabled = True
    29. End Select
    30.     Me.Hide
    31. End Sub

    HELP!!..
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to increment if file exists?

    I couldn't really test this, but it should work. It will make thousands of copies and always not overwrite one. The RED code needs to be added or changed. Let me know how it works

    VB Code:
    1. Private Sub cmd_ok_Click()
    2. Dim prof As String
    3. [COLOR=Red]Dim x%, xs$
    4. [/COLOR]Select Case Me.Caption
    5.     Case "Save Profile"
    6.         txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
    7.         If isFile(txt_profilepath.Text) Then
    8.             txt_plprofile.Text = frm_loadfile.var1
    9.             prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
    10.                 If MsgBox("File " & txt_profilepath & " exists.  Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
    11.                     SaveProfile txt_profilepath.Text, pl1
    12.                 Else
    13. [COLOR=Red]                    x = 1
    14.                     xs = Format(x, "00") ' check '-01'
    15.                     Do While Dir(file1.Path & file1.FileName & "\" & prof & "-" & xs & ".plp")
    16.                       x = x + 1
    17.                       xs = Format(x, "00")
    18.                     Loop
    19.                     txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & "-" & xs & ".plp"
    20. [/COLOR]                    SaveProfile txt_profilepath.Text, pl1
    21.                 End If
    22.         Else
    23.             MsgBox "Saving " & txt_profilepath & " as new profile."
    24.             SaveProfile txt_profilepath.Text, pl1
    25.         End If
    26.  
    27.         profilepath = txt_profilepath.Text
    28.         frm_field.cmd_next.Enabled = True

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width