Solved

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

Posted on 2013-05-10
6
398 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 69

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 69

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 69

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now