Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

i wrote the below code to create a pdf named "new.pdf" at desktop

VB
Dim strPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
           .WritePDF(strPath + "\new.pdf")


Now i want to check if the file "new.pdf" is already present then it should create file name like "new_1.pdf","new_2.pdf" and so on else it will create "new.pdf" and the next files

Please tell me how to do this
Posted

there is a function that may be of some use.

VB

VB
file.exists( strpath & "\new.pdf" )


http://msdn.microsoft.com/en-us/library/system.io.file.aspx[^]

so you could use it similar to

VB

VB
if file.exists( strpath & "\new.pdf" ) then
  'Your code to change filename here
end if
 
Share this answer
 
How about this, for generating your fileName
VB
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim fileName As String = "new.pdf"
Dim count As Integer = 1

While System.IO.File.Exists(System.IO.Path.Combine(path, fileName))
    count = (count + 1)
    fileName = ("new_"  _
                + (count.ToString + ".pdf"))
    
End While


Then you can simple use:
VB
Dim strPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
           .WritePDF(strPath + fileName)
 
Share this answer
 
v2
Comments
[no name] 4-Jan-13 3:58am    
It's not showing any error but the file is not creating..while debugging the code it's showing the file name as new_1,new_2 but it's not creating them
Agent__007 4-Jan-13 4:01am    
Well, I thought this is being done by your extension method WritePDF(), and you only have problem with generating the fileName. May I know what is WritePDF(string filePath) then?
[no name] 4-Jan-13 4:10am    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

con = New MySqlConnection("Server=localhost; Database=demo;Uid=root; password=1234")
con.Open()
Dim cmd As MySqlCommand = New MySqlCommand("select * from demodata", con)
Dim dt As DataTable = New DataTable()

Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
da.Fill(dt)
'ConvertDataTableToString(dt)
Dim s As String
s = ConvertDataTableToString(dt)

'DataGridView1.DataSource = dt
Dim myPDFClass As New clsPdfWriter
With myPDFClass
'-- Comment the file for learning
.CommentFile = True
.pdfTitle = "Hello World"
.pdfSubject = "First PDF File"
.pdfCreator = "Visual Basic dot Net"
.pdfAuthor = "Charles Cope"
.pdfProducer = "pdfWritter"
.PageCount = 1
.PaperSize = clsPdfWriter.pdfPaperSize.pdfLetter
.ShowingText(1, 100, 720, s, clsPdfWriter.pdfStandardFonts.Helvetica, 8, Color.Black, clsPdfWriter.pdfTextAlign.pdfAlignLeft, 0)
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim fileName As String = ""
Dim count As Integer = 1

While System.IO.File.Exists(System.IO.Path.Combine(path, fileName))
count = (count + 1)
fileName = ("new_" _
+ (count.ToString + ".pdf"))
' Dim strPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
.WritePDF(path + fileName)
End While

MsgBox("File has been created ", MsgBoxStyle.Information)
End With
Well this is my button click code..u can check it out..or else if u have any other option that also pls do tell...
Agent__007 4-Jan-13 4:18am    
Try changing your line
.WritePDF(path + fileName)
to
.WritePDF(System.IO.Path.Combine(path, fileName))

and also move this line out of and after while loop.
[no name] 4-Jan-13 5:19am    
When i m moving the line out of the while loop it giving error saying access denied to that path

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900