Solved

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

Posted on 2013-05-10
6
414 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 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
Technology Partners: 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!

 
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

Industry Leaders: 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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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