Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Moving files

Moving files

Scheduled Pinned Locked Moved C#
questiontutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BECK7
    wrote on last edited by
    #1

    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.

    L K 2 Replies Last reply
    0
    • B BECK7

      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.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      File.Exists() xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

      1 Reply Last reply
      0
      • B BECK7

        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.

        K Offline
        K Offline
        KaptinKrunch
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • K KaptinKrunch

          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

          B Offline
          B Offline
          BECK7
          wrote on last edited by
          #4

          do the variables filename and newfile includes the directory?

          K 1 Reply Last reply
          0
          • B BECK7

            do the variables filename and newfile includes the directory?

            K Offline
            K Offline
            KaptinKrunch
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups