• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

Move files to a network share

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
jckingcpa
Asked:
jckingcpa
  • 3
  • 2
1 Solution
 
JimBeveridgeCommented:
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
 
jckingcpaAuthor Commented:
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
 
JimBeveridgeCommented:
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
 
jckingcpaAuthor Commented:
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
 
JimBeveridgeCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now