Solved

Move files to a network share

Posted on 2011-03-04
5
533 Views
Last Modified: 2012-05-11
I am trying to copy files from a local drive to a network drive. In my code drive h:\ is a mapped drive. Actually moving the files to a folder that has the same name as the file. This works fine if I move files locally but when I attempt to move to a mapped drive it fails. Can someone take a look and give me some advice?
Sub Main()
        Dim iPOS As Integer
        Dim strFileOnly As String

        For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    "C:\jktemp\")

            Dim foundFileInfo As New System.IO.FileInfo(foundFile)

            'get just file name without extension
            iPos = 0
            iPos = InStr(foundFileInfo.Name, ".")
            If iPos <> 0 Then
                strFileOnly = Left(foundFileInfo.Name, iPos - 1)
            End If

            If My.Computer.FileSystem.DirectoryExists("C:\jktemp\" & strFileOnly & "\") Then

                My.Computer.FileSystem.MoveFile(foundFile, "C:\jktemp\" & strFileOnly & "\" & foundFileInfo.Name)

            End If
        Next

    End Sub

Open in new window

0
Comment
Question by:jckingcpa
[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
  • 2
5 Comments
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 35039406
The first thing you should do with problems like this is look at the Exception to find out the error message. Guessing at the problem is not productive.

The most common cause of the problem you describe is a mismatch between admin/non-admin on Vista or Windows 7. The issue is that, if you map the drive in Explorer, it will be mapped as a non-admin. If you run Visual Studio as an Administrator, then the mapped drive won't appear.

Try using a UNC path instead of the H: drive letter.

You also have a bug in your code. You are using InStr() to search for the period, but a filename can have multiple periods and even the directory names can have periods. You need to search for the last period. (Actually, even that's not completely reliable, but it's usually good enough.)
0
 

Author Comment

by:jckingcpa
ID: 35039508
I have tried the UNC path as well.

Description:
Application: TBM-CopyStatements.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
   at System.IO.__Error.WinIOError(Int32, System.String)
   at System.IO.__Error.WinIOError()
   at System.IO.File.Move(System.String, System.String)
   at Microsoft.VisualBasic.FileIO.FileSystem.CopyOrMoveFile(CopyOrMove, System.String, System.String, Boolean, UIOptionInternal, Microsoft.VisualBasic.FileIO.UICancelOption)
   at TBM_CopyStatements.Module1.Main()

Thanks for pointing out the issue with the periods. Fortunately I will be controlling the creation of the files but will keep that in mind for the future.
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 35039566
There should another exception inside the "Unhandled" exception. That's the exception you want. It's often easiest to see it in the debugger.

What I would try in this case would be to try to programatically try to open a file (for write) with the desired filename and see what happens. This simplifies the problem so you don't have to worry about all the other stuff that MoveFile does.
0
 

Author Comment

by:jckingcpa
ID: 35039619
Unfortunately I can't use the debugger. The computer with Visual Studio on it does not have access to the network so I have to copy the .exe to another computer to test it.  What I pasted was from the Application log on the network workstation.  I will try to open the file for write as you suggested. The code works when I copy from c:\jktemp\ to a folder within c:\jktemp\ with the same name as the file which is the confusing part. I have full access to the network share.
0
 
LVL 7

Accepted Solution

by:
JimBeveridge earned 500 total points
ID: 35039654
Ideally you should capture the exception in your own application, then you can display all of the nested error messages. Uncaught exceptions are "bad".
0

Featured Post

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!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

737 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