Tuesday, 15 January 2013

vb.net - How to Rename a Renamed file if already exist in asp.net? -



vb.net - How to Rename a Renamed file if already exist in asp.net? -

i trying rename files before moving subfolder. first thing filenames , extensions of files on location folder. after check whether files exist on active folder. if yes renamed adding incremented integer.

for instance location folder contains 'sample.txt' file , active folder has no file that. on case don't need rename file 'sample.txt', need move active folder. when active folder contains such filename, when moved must renamed sample(1).txt, , when file on location folder has filename 'sample.txt' when moved must sample(2).txt.

below code

protected sub button1_click(byval sender object, byval e eventargs) handles button1.click dim dir new directoryinfo("c:\documents , settings\admin\desktop\location") dim folder1files fileinfo() = dir.getfiles() each nfile fileinfo in folder1files dim filename string = path.getfilenamewithoutextension(nfile.name) dim fileext string = path.getextension(nfile.name) dim newfilename string dim filenumber = 0 if file.exists("c:\documents , settings\admin\desktop\location\active folder\" & filename & fileext) filenumber += 1 newfilename = string.format("{0}({1}){2}", filename, filenumber, fileext) file.move("c:\documents , settings\admin\desktop\location\" & filename & fileext, "c:\documents , settings\admin\desktop\location\active folder\" & newfilename) else file.move("c:\documents , settings\admin\desktop\location\" & filename & fileext, "c:\documents , settings\admin\desktop\location\active folder\" & filename & fileext) end if next end sub

whenever tried debug above code, , 'sample.txt' exist when moved becomes 'sample(1).txt', hence it's correct, when 'sample.txt' exist 1 time again on location folder when moved becomes 'sample(1)(1).txt', in fact must 'sample(2).txt'.

what should expected result?

thanks in advance.

give seek code , excuse if there syntax error i've converted code c# vb.

dim olddir string = "c:\documents , settings\admin\desktop\location" dim newdir string = "c:\documents , settings\admin\desktop\location\active folder\" dim newfilename string = string.empty dim dir new directoryinfo(olddir) dim folder1files fileinfo() = dir.getfiles() each nfile fileinfo in folder1files dim oldfilename string = path.getfilenamewithoutextension(nfile.name) dim fileext string = path.getextension(nfile.name) dim oldpath string = olddir & oldfilename & fileext dim newpath string = newdir & oldfilename & fileext dim index integer = 1 while file.exists(newpath) newfilename = oldfilename & "(" & index & ")" newpath = newdir & newfilename & fileext index += 1 end while file.move(oldpath, newpath) next

i hope gives improve clue.

asp.net vb.net

No comments:

Post a Comment