Solved

Move files to a network share

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 81
how to open Waze.com/livemap from address saved in DB? 26 152
java constructor error 8 86
count8 challlenge 13 85
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
This is an explanation of a simple data model to help parse a JSON feed
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now