|
-
May 23rd, 2005, 03:19 AM
#1
Thread Starter
Member
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:
Function isFile(ByVal sFileName As String) As Integer
On Error Resume Next
Dim lFileLength As Long
Const ATTR_NORMAL = 0
isFile = False
If Dir$(sFileName, ATTR_NORMAL) <> "" Then
lFileLength = FileLen(sFileName)
If (lFileLength > 0) Then isFile = True
End If
End Function
VB Code:
Private Sub cmd_ok_Click()
Dim prof As String
Select Case Me.Caption
Case "Save Profile"
txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
If isFile(txt_profilepath.Text) Then
txt_plprofile.Text = frm_loadfile.var1
prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
If MsgBox("File " & txt_profilepath & " exists. Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
SaveProfile txt_profilepath.Text, pl1
Else
txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & "-02" & ".plp"
SaveProfile txt_profilepath.Text, pl1
End If
Else
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pl1
End If
profilepath = txt_profilepath.Text
frm_field.cmd_next.Enabled = True
Case "Load Profile"
txt_profilepath.Text = file1.Path & "\" & file1.FileName
MsgBox "Loading " & txt_profilepath & " profile."
LoadProfile txt_profilepath.Text, pl1
profilepath = txt_profilepath.Text
frm_field.cmd_next.Enabled = True
End Select
Me.Hide
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! 
-
May 23rd, 2005, 03:54 AM
#2
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:
Private Sub cmd_ok_Click()
Dim prof As String
[COLOR=Red]Dim x%, xs$
[/COLOR]Select Case Me.Caption
Case "Save Profile"
txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
If isFile(txt_profilepath.Text) Then
txt_plprofile.Text = frm_loadfile.var1
prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
If MsgBox("File " & txt_profilepath & " exists. Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
SaveProfile txt_profilepath.Text, pl1
Else
[COLOR=Red] x = 1
xs = Format(x, "00") ' check '-01'
Do While Dir(file1.Path & file1.FileName & "\" & prof & "-" & xs & ".plp")
x = x + 1
xs = Format(x, "00")
Loop
txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & "-" & xs & ".plp"
[/COLOR] SaveProfile txt_profilepath.Text, pl1
End If
Else
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pl1
End If
profilepath = txt_profilepath.Text
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|