Moving files
-
Hi, im moving a .xls file to another directory, but if this file exists i want to rename it and still move it. For example, if i have a ex.xls in a directory and im trying to move another file with the same name, i want to rename it like to ex1.xls and move it. How can i do this? any ideas? Thanks in advance.
-
Hi, im moving a .xls file to another directory, but if this file exists i want to rename it and still move it. For example, if i have a ex.xls in a directory and im trying to move another file with the same name, i want to rename it like to ex1.xls and move it. How can i do this? any ideas? Thanks in advance.
-
Hi, im moving a .xls file to another directory, but if this file exists i want to rename it and still move it. For example, if i have a ex.xls in a directory and im trying to move another file with the same name, i want to rename it like to ex1.xls and move it. How can i do this? any ideas? Thanks in advance.
a simple function as such should do the trick. void MoveMe(string filename) { int x = 1; string newfile = filename; while(System.IO.File.Exists(newfile) == true) { String[] tmp = filename.Split(Convert.ToChar(".")); newfile = tmp[0] + x + "." + tmp[1]; x++; } System.IO.File.Move(filename, newfile); } -- modified at 13:45 Monday 12th September, 2005
-
a simple function as such should do the trick. void MoveMe(string filename) { int x = 1; string newfile = filename; while(System.IO.File.Exists(newfile) == true) { String[] tmp = filename.Split(Convert.ToChar(".")); newfile = tmp[0] + x + "." + tmp[1]; x++; } System.IO.File.Move(filename, newfile); } -- modified at 13:45 Monday 12th September, 2005
-
in this case, yes. The file name is split at the "." in the filename. So if filename = C:\Autoexec.bat then newfile = c:\Autoexec1.bat Because of design, if a file name consists of more than one period, you will receive unexpected results.