Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows VB.NET
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 21:41pm
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 at 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 at 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 at 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 at 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 at 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 at 4-Jan-13 5:59am
   
when u got the error access denied filename is " " or null thats why getting that error.
Sanjay_007 at 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 at 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 at 4-Jan-13 7:18am
   
@Lakshmimsridhar: Mam it's giving error for the path not for the file.please read the error properly
Lakshmimsridhar at 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
0 Sergey Alexandrovich Kryukov 450
1 OriginalGriff 230
2 DamithSL 150
3 Dave Kreskowiak 110
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,740
1 DamithSL 5,644
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,011
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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