-1

I'm having a problem with using FileSystemObject.CopyFile. I think I'm using it correctly, from the forums I've read, but I am still getting the following compiler error:

ArgumentException was unhandled: Value does not fall within expected range

Here is the code:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fso As New Scripting.FileSystemObject
    Dim testfolderchk
    testfolderchk = Dir("C:\Test\")
    Dim inforeader As System.IO.FileInfo
    Dim filedestinationcheck = Dir("C:\Test2\")

  If testfolderchk <> "" Then 
        If Microsoft.VisualBasic.Left(testfolderchk, 4) = "test" Then
            inforeader = My.Computer.FileSystem.GetFileInfo("C:\Test" & testfolderchk)
            filetime = (inforeader.LastWriteTime)
            If testfolderchk = filedestinationcheck Then GoTo skipfile
            If testfolderchk = filedestinationcheck2 Then GoTo skipfile

        Else : GoTo skipfile
        End If
    End If

fso.CopyFile(testfolderchk, filedestinationcheck, True)
Bugs
  • 4,491
  • 9
  • 32
  • 41
user2644085
  • 157
  • 1
  • 2
  • 9
  • 3
    The `System.IO` namespace has all sorts of file related methods which are better suited to NET code than `FileSystemObject`. – Ňɏssa Pøngjǣrdenlarp Mar 03 '17 at 16:23
  • Suggestions? Anyone? – user2644085 Mar 03 '17 at 16:43
  • `Suggestions?` Yes, dont use FSO and dont use `GoTo`. And with just a little research you could find hundreds of file copy applets here – Ňɏssa Pøngjǣrdenlarp Mar 03 '17 at 16:44
  • As @Plutonix has suggested. Look into the `System.IO` namespace. This has `Directory`, `DirectoryInfo`, `File`, `FileInfo` which should be all you need to get information and to copy files. Don't use `FileSystemObject`. – Bugs Mar 03 '17 at 16:45
  • I'm unsure as well what you are doing here `("C:\Test" & testfolderchk)`. Wouldn't that effectively be `("C:\TestC:\Test\")`? Since `testfolderchk` has been assigned "C:\Test" – Bugs Mar 03 '17 at 16:49
  • Suggesting I haven't researched the issue isn't helpful, because I have done that. Thank you for your suggestions. – user2644085 Mar 03 '17 at 16:50
  • I was suggesting you could easily find .NET code written to show how to use the NET File IO methods and doesnt use `GoTo` – Ňɏssa Pøngjǣrdenlarp Mar 03 '17 at 17:02

1 Answers1

2

As suggested in the comments you should scrap the use of FileSystemObject and use instead the System.IO namespace, specifically FileInfo:

Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

With FileInfo you can use the method CopyTo (String, Boolean):

Copies an existing file to a new file, allowing the overwriting of an existing file.

I have drafted up a bit of code to show you what I mean:

Dim folderToCheck As String = "C:\Test"
Dim destinationFolder As String = "C:\Test2"

Dim file As IO.FileInfo = New IO.FileInfo(IO.Path.Combine(folderToCheck, "test.txt"))

Dim filetime As Date = file.LastWriteTime

file.CopyTo(IO.Path.Combine(destinationFolder, file.Name), True)

Note the use of Path.Combine:

Combines two strings into a path.

Bugs
  • 4,491
  • 9
  • 32
  • 41