Solved

Move files to a network share

Posted on 2011-03-04
5
529 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

910 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

20 Experts available now in Live!

Get 1:1 Help Now