Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-10
6
Medium Priority
?
431 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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