Solved

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

Posted on 2013-05-10
6
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

734 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