Click here to Skip to main content
13,193,048 members (64,461 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi all

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

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 3-Jan-13 20:41pm
Code-Hunt7.1K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

there is a function that may be of some use.

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


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

so you could use it similar to

if file.exists( strpath & "\new.pdf" ) then
  'Your code to change filename here
end if
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

How about this, for generating your fileName
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:
Dim strPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
           .WritePDF(strPath + fileName)
  Permalink  
v2
Comments
Snehasish Nandy 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
Sanjay_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?
Snehasish Nandy 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...
Sanjay_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.
Snehasish Nandy 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
Lakshmimsridhar 4-Jan-13 5:59am
   
when u got the error access denied filename is " " or null thats why getting that error.
Sanjay_007 4-Jan-13 6:25am
   
I suspect you didnot see my updated answer, please check this: I have updated my line as:
Dim fileName As String = "new.pdf"
Snehasish Nandy 4-Jan-13 7:13am
   
at first i gave the file name but that time also it was only creating the new.pdf, not creating files after that..(new_1,new_2,....)
Snehasish Nandy 4-Jan-13 7:18am
   
@Lakshmimsridhar: Mam it's giving error for the path not for the file.please read the error properly
Lakshmimsridhar 4-Jan-13 8:06am
   
file is going to save in path itself sir. that path itself not coming there properly thats why giving that error

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171017.2 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100