Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET How to move files from one directory to another?

Posted on 2013-05-10
6
Medium Priority
?
442 Views
Last Modified: 2013-05-10
Morning,

I am using Visual Studio Express 2012.

I am trying to move "only" files from one directory to another in VB.Net but can't get my code to work.

'create directory in input folder with time stamp

        Dim destdir As String = [String].Format("C:\test3\{0}", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.Directory.CreateDirectory(destdir)

        'move files from source to destination

        For Each srcfile As String In System.IO.Directory.GetFiles(TextBox1.Text)
            System.IO.File.Move(srcfile, destdir)
        Next

Open in new window


My error is:



An exception of type 'System.IO.IOException' occured in mscorlib.dll but was not handled in user code.

Additional information: Cannot create a file when that file already exists.


The srcfiles files do not exist in the destdir, so why is it telling me they already exist?

Any ideas?

N
0
Comment
Question by:nobushi
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39155166
there is something wrong with destdir. you first create a folder with that name and then you try to move srcfile to that name again. The syntax of Move requires a filename as the second parameter, not a folder name.
0
 
LVL 1

Author Comment

by:nobushi
ID: 39155179
OK so what syntax would I use to move the source files to that destination directory name?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 39155195
For Each srcfile As String In System.IO.Directory.GetFiles(TextBox1.Text)
            System.IO.File.Move(srcfile, path.combine(destdir, path.getfilename(srcfile)))
        Next
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:nobushi
ID: 39155216
Thanks.

But I dont understand what the path would be as I have already declared the paths as variables (srcfiles and destdir) right? So what would go in place of "Path"?

System.IO.File.Move(srcfiles, PATH.combine(destdir, PATH.getfilename(srcfiles)))

PS. I'm new to programming in general so please bare with me.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39155228
Path is a class name that contains some methods like GetFileName which will return the filename from a variable containing a full path and Combine which will concatenate a folder and a filename with an appropriate delimiter.

Maybe you need to write System.IO.Path if you do not have the appropriate Imports statement
0
 
LVL 1

Author Comment

by:nobushi
ID: 39155229
Nevermind I see what you mean.

For Each srcfiles As String In System.IO.Directory.GetFiles(TextBox1.Text)
            System.IO.File.Move(srcfiles, System.IO.Path.Combine(destdir, System.IO.Path.GetFileName(srcfiles)))
        Next

Open in new window


Thank you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question