Solved

Move files to a network share

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 97
Image decoding from Camera 3 87
Is there any way to copy all SSRS reports/datasets/data sources  to new server? 3 62
Cygwin - GNU GPL License 1 24
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
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 …

773 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